You are on page 1of 17

Basics of Embedded C Program

Data types:
● The data type refers to an extensive system for declaring variables of different types

like integer, character, etc.


● The embedded C software uses four data types that are used to store data in the

memory.
● The ‘char’ is used to store any single character; ‘int’ is used to store integer value,

and ‘float’ is used to store any precision floating point value.


● Unsingned characters are used to set count value, for toggling port

● Signed characters are used for 2’s complement representation


Keywords
There are certain words that are reserved for doing specific tasks.
These words are known as keywords. They are standard and predefined in the Embedded C.
Keywords are always written in lowercase. These keywords must be defined before writing the main
program.

● sbit: This data type is used in case of accessing a single bit of SFR register.(SFR bit addressable
only)
Syntax: sbit variable name = SFR bit ;
Ex: sbit a=P2^1;
Explanation: If we assign p2.1 as ‘a’ variable,
Size of bit=0ne bit
● bit: This data type is used for accessing the bit addressable memory of RAM
(20h-2Fh).
Syntax: bit variable name;
Ex: bit c;
Explanation: It is a bit sequence setting in a small data area that is used by a
program to remember something.
Size of bit=0ne bit
● sfr: This data type is used for accessing a SFR register by another name. All
the SFR registers must be declared with capital letters.
Syntax: SFR variable name = SFR address of SFR register;
Ex: SFR P0=0x80;
Explanation: If we assign 0x80 as ‘P0’, then we can use 0x80 instead of port0
anywhere in the program, which reduces the complexity of the program.
Size of bit=8 bit
RAM address ranging from 80H-FF H
sfr16 DPTR = 0x82;
// 16-bit special function register starting at 0x82
Way to access SFR and single bit off SFR
Use name of SFR and reg 51.h
#include<reg51.h>
P1=0x55
● Here reg 51.h is necessary
Access bit addressable RAM
sfrP1=0x90
P1=0x55
● Here reg 51.h is not necessary
The Structure of an Embedded C Program

comments
preprocessor directives
global variables
main() function
Comments: In embedded C programming language, we can place
comments in our code which helps the reader to understand the code easily.
C=a+b; /* add two variables whose value is stored in another variable C*/
Preprocessor directives:
All the functions of the embedded C software are included in the
preprocessor library like “#includes<reg51.h>, #defines”.
These functions are executed at the time of running the program.
Global variable
● A global variable is a variable that is declared before the main function, and can
be accessed on any function in the program

Local variable
A local variable is a variable declared within a function, and it is valid only to be
used within that function.

● Main () function
The execution of a program starts with the main function. Every program uses
only one main () function.
Eg1.
MOV R2,#255
ABC: MOV P1,R2
DJNZ R2, ABC
● In C
for(int i=255,i>0,i--)
P1=i
WAP to send values 00 to FF to port 1
#includes<reg51.h>
void main (void)
{
unsigned char z;
for(z=0;z<=255;z++)
P1=z
}
WAP to send the hex values of ASCII character
0,1,2,3,4,5,A,B,C and D to Port 1
#includes<reg51.h>
void main (void)
{
Unsingned char mynum[]=”012345ABCD”; //all values are moved to mynum array
//(store group of data)
unsigned char z;
for(z=0;z<=10;z++)
P1=mynum[z]
}
WAP to send values from –4 to 4 in port 1

//signed numbers

#includes<reg51.h>
void main (void)
{
char mynum[]={-4,-3,-2,-1,1,2,3,4};
unsigned char z;
for(z=0;z<=8;z++)
P1=mynum[z]
}
WAP to toggle 0th bit of port 1(P1.0) 50,000 times
#includes<reg51.h>
sbit mybit=P1^0
void main (void)
{
unsigned int z;
for(z=0;z<=50000;z++)
{
mybit=0
mybit=1
}
}
● P1.0 can be connected to LED, It will blink 50,000 times
● We can make a delay between mybit=0 and mybit =1
Time Delay
● It is obtained
either by using 8051 timer or simple for loop
WAP to to toggle the bits of Port 1 continuously foreever with some
time delay
01010101
● 10101010
//toggle P1 foreever with some time delay in between On and Off
#includes<reg51.h>
void main (void)
{
unsigned int i; //data type
for(;;) //repeat for ever or infinite loop or use while (1)
{
P1=0x55; //send the hexa value to the port0//
for(i=0;i<250;i++); //normal delay//
P1=0xAA; //send the hexa value to the port0//
for(i=0;i<250;i++); //normal delay//
}
}
● In assembly language we are writing 55H but in embedded C hexadecimal
numbers are written as 0x55
WAP to get the status of the bit P1.0 save it and send it to
P2.7 continuously
#includes<reg51.h>
sbit inbit=P1^0
sbit outbit=P2^7
bit c;
void main(void)
{
while (1) //for infinite loop
{
c=inbit
outbit=c
}
}
Assume that external crystal frequency(XTAL)=11.0592 MHz. What value do
we need to load into the timer’s register if we want to have a time delay
of 5ms?write a program to toggle all the bits of Port 1
XTAL=11.0592
fclk=11.0592/12=0.9216MHz
Time period for internal clock=1.085 microseconds
For getting 5msec delay, 5msec/1.085microsec=4608 clocks needed
Mode 1 operation,
Timer value 0000 to FFFF
Total 65536 counts
To achieve 4608 clocks/delay we need to load=65536-4608=60928=EE00h
TH=EE, TL=00

#includes<reg51.h>
void T0Delay (void)
void main (void)
{
while(1)
{
P1=0x55;
T0Delay[];
P1=0xAA;
T0Delay[];
}}
VoidT0Delay[]
{
TMOD=0x01;
TL0=0x00;
TH0=0xEE;
TR0=1;
while(TF0==0); // once this condition fails it comes out of this loop
TR0=0;
TF0=0’
}

You might also like