Basics of Embedded C Program
Basics of Embedded C Program
Now that we have seen a little bit about Embedded Systems and
Programming Languages, we will dive in to the basics of Embedded C
Program. We will start with two of the basic features of the Embedded C
Program: Keywords and Datatypes.
Keywords in Embedded C
bit
sbit
sfr
small
large
The following table lists out all the keywords associated with the Cx51 C
Compiler.
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.
bit 0 or 1 (bit 1
addressable part
of RAM)
sbit 0 or 1 (bit 1
addressable part
of RAM)
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 . . . . .
….
….
}
Comments: Comments are readable text that are written to help us (the
reader) understand the code easily. They are ignored by the compiler and do
not take up any memory in the final code (after compilation).
There are two ways you can write comments: one is the single line comments
denoted by // and the other is multiline comments denoted by /*….*/.
Global Variables: Global Variables, as the name suggests, are Global to the
program i.e., they can be accessed anywhere in the program.
The following image shows the circuit diagram for the example circuit. It
contains an 8051 based Microcontroller (AT89S52) along with its basic
components (like RESET Circuit, Oscillator Circuit, etc.) and components for
blinking LEDs (LEDs and Resistors).
In order to write the Embedded C Program for the above circuit, we will use
the Keil C Compiler. This compiler is a part of the Keil µVision IDE. The
program is shown below.
Sample Code
#include<reg51.h> // Preprocessor Directive
void delay (int); // Delay Function Declaration