Basics of Embedded C Program
Basics of Embedded C Program
Outline
What is an Embedded System?
Programming Embedded Systems
Factors for Selecting the Programming Language
Introduction to Embedded C Programming Language
Difference between C and Embedded C
Basics of Embedded C Program
o Keywords in Embedded C
o Data Types in Embedded C
Basic Structure of an Embedded C Program (Template for Embedded C
Program)
o Different Components of an Embedded C Program
Basic Embedded C Program
o Example of Embedded C Program
We use washing machines almost daily but wouldn’t get the idea that it is an
embedded system consisting of a Processor (and other hardware as well)
and software.
It takes some inputs from the user like wash cycle, type of clothes, extra
soaking and rinsing, spin rpm, etc., performs the necessary actions as per the
instructions and finishes washing and drying the clothes. If no new
instructions are given for the next wash, then the washing machines repeats
the same set of tasks as the previous wash.
Some of the embedded systems in a Car are Anti-lock Braking System (ABS),
Temperature Monitoring System, Automatic Climate Control, Tyre Pressure
Monitoring System, Engine Oil Level Monitor, etc.
All these devices have one thing in common: they are programmable i.e., we
can write a program (which is the software part of the Embedded System) to
define how the device actually works.
Embedded Software or Program allow Hardware to monitor external events
(Inputs / Sensors) and control external devices (Outputs) accordingly. During
this process, the program for an Embedded System may have to directly
manipulate the internal architecture of the Embedded Hardware (usually the
processor) such as Timers, Serial Communications Interface, Interrupt
Handling, and I/O Ports etc.
From the above statement, it is clear that the Software part of an Embedded
System is equally important as the Hardware part. There is no point in having
advanced Hardware Components with poorly written programs (Software).
There are many programming languages that are used for Embedded
Systems like Assembly (low-level Programming Language), C, C++, JAVA (high-
level programming languages), Visual Basic, JAVA Script (Application level
Programming Languages), etc.
There are other high-level programming languages that offered the above
mentioned features but none were close to C Programming Language. Some
of the benefits of using Embedded C as the main Programming Language:
Keywords in Embedded C
A Keyword is a special word with a special meaning to the compiler (a C
Compiler for example, is a software that is used to convert program written
in C to Machine Code). For example, if we take the Keil’s Cx51 Compiler (a
popular C Compiler for 8051 based Microcontrollers) the following are some
of the keywords:
bit
sbit
sfr
small
large
The following table lists out all the keywords associated with the Cx51 C
Compiler.
_at_ alien
bit code
data far
interrupt large
_priority_ reentrant
sfr sfr16
_task_ using
The following are the extra data types in Embedded C associated with the
Keil’s Cx51 Compiler.
bit
sbit
sfr
sfr16
The following table shows some of the data types in Cx51 Compiler along
with their ranges.
o Multiline Comments . . . . . Denoted using /*……*/
o Single Line Comments . . . . . Denoted using //
o Preprocessor Directives . . . . . #include<…> or #define
o Global Variables . . . . . Accessible anywhere in the program
o Function Declarations . . . . . Declaring Function
o Main Function . . . . . Main Function, execution begins here
{
Local Variables . . . . . Variables confined to main function
Function Calls . . . . . Calling other Functions
Infinite Loop . . . . . Like while(1) or for(;;)
Statements . . . . .
….
….
}
o Function Definitions . . . . . Defining the Functions
{
Local Variables . . . . . Local Variables confined to this Function
Statements . . . . .
….
….
}
Before seeing an example with respect to 8051 Microcontroller, we will first
see the different components in the above structure.
In case of 8051, Keil Compiler has the file “reg51.h”, which must be written at
the beginning of every Embedded C Program.
COMPONENTS
#include<reg51.h> // Preprocessor Directive
void delay (int); // Delay Function Declaration
void main(void) // Main Function
{
P1 = 0x00;
/* Making PORT1 pins LOW. All the LEDs are OFF.
* (P1 is PORT1, as defined in reg51.h) */