Unit II Programming Using Embedded C Final
Unit II Programming Using Embedded C Final
Unit II
Programming using Embedded C
Differentiate between Assembly Language Programming (ALP) and Embedded
C programming
Sr.
Parameter Assembly Language Embedded C
No.
1 Execution Time Faster(requires Less ET) Slower(requires more ET)
2 Time for coding More time required Less time required
3 Hex file size Less More
4 Debugging/Ease of Not so easy Easy
coding
5 Processor dependency Processor/Controller Processor/Controller
dependent independent
6 Type of Language Low level language High level language
7 Memory required for Less memory required More memory required.
Program coding
the detailed information of as source code part with labels and symbols during
the execution for each single step. Provides the detailed information of the
status of memory RAM and simulated ports, simulated peripheral devices of the
defined target system.
vi. Debugger: - A Debugger allows you to download your code to the emulator's
memory and then control all of the functions of the emulator from a PC. Common
debugging features include the capability to examine and modify the
microcontroller's on-chip registers, data- and program-memory; pausing or
stopping program executing at defined program locations by setting breakpoints;
single-stepping (execute one instruction at a time) through the code; and looking
at a history of executed code (trace).
vii. Integrated Development Environment (IDE):- An IDE is a software application
that provides comprehensive facilities to computer programmers for software
development. An IDE consists of: A source code editor. A compiler and or
interpreter. Build automation tools. A debugger. IDE is dedicated to a specific
programming language, allowing a feature set that most closely matches
the programming paradigms [model] of the language. IDE’s typically present a
single program in which all development is done. This program typically provides
many features for authoring, modifying, compiling, deploying, and debugging
software.
Logical operators in C-
FTCCOERP P a g e 2 | 10
Programming using Embedded C | U-2
FTCCOERP P a g e 3 | 10
Programming using Embedded C | U-2
o Pin Description-
o Timer Modes-
FTCCOERP P a g e 4 | 10
Programming using Embedded C | U-2
FTCCOERP P a g e 5 | 10
Programming using Embedded C | U-2
SERIAL COMMUNICATION-
1. SCON Register-
o PCON Register-
FTCCOERP P a g e 6 | 10
Programming using Embedded C | U-2
O BAUDRATE-
O BAUDRATE CALCULATIONS-
O STANDARD BAUDRATES-
The following Decimal or hexa decimal values will be loaded in TH1 register to
generate standard baud rate.
1. TMOD register is loaded with the value 20H to select 8 bit Auto Reload Mode…
2. TH1 is loaded with Decimal or Hex value to set the baud rate
3. SCON register is loaded to select serial communication mode…
4. TR1 is set to 1 to start Timer 1
5. RI is cleared.
6. RI flag bit is monitored to see if an entire character has been received yet
7. When RI=1 then SBUF has the byte, and its contents are moved into a safe
place
8. To receive the next character, go to Step 5.
FTCCOERP P a g e 7 | 10
Programming using Embedded C | U-2
1. TMOD register is loaded with the value 20H to select 8 bit Auto Reload Mode…
2. TH1 is loaded with Decimal or Hex value to set the baud rate
3. SCON register is loaded to select serial communication mode…
4. TR1 is set to 1 to start Timer 1
5. TI is cleared.
6. The data which is to be transmitted is copied to SBUF register…
7. Now monitor TI flag, when TI=1 it means that data in SBUF has been
transmitted completely.
8. To transmit the next character, go to Step 5.
FTCCOERP P a g e 8 | 10
Programming using Embedded C | U-2
}
}
3. Exchange five bytes from source to destination.
#include<reg51.h>
void main()
{
unsigned char source[5]={0x0A,0x0B,0x0C,0x0D,0x0E}; //source array
unsigned char destination[5]={0X01,0X02,0X03,0X04,0X05};//destination array
FTCCOERP P a g e 9 | 10
Programming using Embedded C | U-2
while(1)
{
for(i=0;i<5;i++) //repeat five times
{
a=source[i]; //copy value from source array
source[i]=destination[i]; //transfer value from destination to source array
#include<reg51.h>
void main()
{
unsigned char source[5]={0x01,0x02,0x03,0x04,0x05}; //source array
unsigned char i,a;//counter
a=0x00;
while(1)
{
for(i=0;i<5;i++) //repeat five times
{
a=a+source[i]; //add value from source array to a
P0=a; //transfer value of a to port P0
}
}
}
FTCCOERP P a g e 10 | 10
Programming using Embedded C|U 2
}
else
{
c=0; //if switch is OFF, make c=0.
}
}
}
8. Write ‘C’ language program to check bit P1.2.
If it is high send 55 H to P0, otherwise send AAH to P2.
/* Embedded C program to check P1.2
if high, send 55H to P0 port
else send AAH to P2 port.
*/
#include<reg51.h>
sbit c=P1^2; //switch is connected to port pin P1.2
void main()
{
while(1)
{
if(c==1)
{
P0=0x55; // if p1.2=1 send 55H to P0 poprt.
}
else
{
P2=0xAA; //if p1.2=0 send AAH to P2 port.
}
}
}
9. Write ‘C’ language program to mask the lower 4 bits of port P0
and upper 4 bits of port P2 using logical operator
/* Embedded C program to mask
lower 4 bits of P0 and
upper 4 bit of P2
using logical operator=> AND.
*/
#include<reg51.h>
void main()
{
P0=0xFF;//make port 0 input port
P2=0xFF;//make port 2 input port
while(1)
{
P0= 0xF0 & P0;//P0=0xFF
// 0XF0
P2=0x0F & P2;// P2=0xFF
// 0x0F
}
}
10. Write C language program to read P1 and store
the two’s complement of P1 to P2.
/* Embedded C program to read P1 and
store 2's complement of P1 to P2.
*/
#include<reg51.h>
FTCCOERP
Programming using Embedded C|U 2
void main()
{
unsigned char c;
while(1)
{
c=P1; //read value from P1 port in c.
c=(~c)+1;//2's compliment=1's compliment+1.
P2=c; //send value of c to port 2.
}
}
11. Write a C language program to read P2 and P3. Shift the
bits of P2 to right by 2 bits and P3 to left by 4 bits.
Store the content of P2 to P0 and P3 to P1
/* Embedded C program to read P2, P3 and
right shift P2 by 2 bits and store in P0.
left shift P3 by 4 bits and store in P1.
*/
#include<reg51.h>
void main()
{
while(1)
{
P0=P2>>2;//right shift data in P2 by 2 bit position
P1=P3<<4;//left shift data in P3 by 4 bit position
}
}
12. Write ‘C’ program for 89C51 to read data from port P1 and P2.
Compare the data and send the bigger data on port 3 continuously
/* Embedded C program to read P1, P2 and
compare the data and
send bigger data to P3 continuosly.
*/
#include<reg51.h>
void main()
{
unsigned char a,b;
while(1)
{
a=P1; //read data from port 1 to a.
b=P2; //read data from port 2 to b.
if(a>b)
{
P3=a; // if P1>P2 send data on P1 to P3.
}
else if(b>a)
{
P3=b; // if P2>P1 send data on P2 to P3.
}
else
{
P3=0x00;// if both P1 & P2 are same make P3=00.
}
}
}
13. Write c language program to toggle all bits of P0, P1, P2 and P3
FTCCOERP
Programming using Embedded C|U 2
{
P1=0x00;//output port
while(1)
{
sq=1;
delay();
sq=0;
delay();
}
}
//delay is generated using timer 0.
void delay()
{
TMOD=0x01; // 0 0 0 0 0 0 0 1 T0 is used in Mode 1
TF0=0;
TH0=0XEE;
TL0=0X00; //EE00 EE01 EE02 upto FFFE FFFF
TR0=1; //start timer.
while(TF0!=1)//wait for timer overflow
{}
TF0=0;//clear overflow flag
TR0=0;//stop timer.
}
17. Write 89C51 ‘C’ language program to generate square wave of 10 KHz
on pin P2-7 using timer 0. Assume crystal frequency as 12 MHz.
/* Embedded C program
to generate square wave of 10kHz on P2.7
Solution:
to generate square wave of 10kHz on port P2.7 pin
for timer delay use T0 in Mode 1
clock freq for timer 1 is = oscialltor freq/12 -->12MHz
i.e. = 12M/12 = 1MHz means 1 uSec.
period of square wave T=1/10k= 100 uSec.
on time= 50usec; off time= 50usec
value to be loaded in timer for 50uSec=65536-(Req. delay/Time for 1 machine cyale)
value to be loaded in timer for 50uSec delay=65536-(50u/1.085u)
value to be loaded in timer for 50uSec delay=65536-46.08 =65489.92=65490(approx)
value to be loaded in timer for 50uSec delay=FFD2
*/
#include<reg51.h>
sbit sq=P2^7; //port pin P2.7 is labelled as sq.
void delay();
void main()
{
P2=0x00;//output port
while(1)
{
sq=1;
delay();
sq=0;
delay();
}
}
//delay is generated using timer 0.
FTCCOERP
Programming using Embedded C|U 2
void delay()
{
TMOD=0x01; // 0 0 0 0 0 0 0 1 T0 is used in Mode 1
TF0=0;
TH0=0XFF;
TL0=0XD2; //FFD2 FFD3 to FFFE FFFF
TR0=1; //start the timer
while(TF0!=1) //wait for timer overflow
{}
TF0=0; //clear timer overflow flag
TR0=0; //stop the timer
}
18. Write ‘C’ language program to generate saw tooth wave continuously.
/* Embedded C program
to generate SAWTOOTH WAVEFORM
Solution:
WAVEFORM FREQUENCY DEPENDS ON CRYSTAL FREQUENCY.
WAVEFORM WILL BE GENERATED ON PORT P2
*/
#include<reg51.h>
sfr sw= 0xA0; //P2 port is labelled as sw. here 0xA0 is address of P2 port.
unsigned char x;
void main()
{
P2=0x00;//output port
}
}
19. Write C language program to generate a square wave of 5 kHz on pin P3.5
by using timer 0 and mode 1. Assume crystal frequency is 24MHz
/* Embedded C program
to generate square wave of 5kHz on P3.5 pin
XTAL frequency= 24
use timer 1 mode 1 to generate delay
Solution:
given square wave freq (f)= 5kHz
Time Periode of given square wave (T)= 1/f=1/5k=200uSec
on time(Ton)=off time (Toff)= T/2= 100uSec
while(1)
{
sq=1;
delay();
sq=0;
delay();
}
}
//delay is generated using timer 1.
void delay()
{
TMOD=0x10; // 0 0 0 1 0 0 0 0 T1 is used in Mode 1
TF1=0;
TH1=0XFF;
TL1=0XA4; //FFA4 FFA5 to FFFD FFFE FFFF
TR1=1; //start the timer 1
while(TF1!=1) //wait for timer overflow
{}
TF1=0; //clear timer overflow flag 1
TR1=0; //stop the timer 1
}
20.
/* develop a program to generate stair-case waveform on port 2...
solution:
P2===>8bit==> 0000 0000 to 1111 1111 ===> 0 to 255
===> 00H to FFH
total steps= 5
so, every step =255/5===>51
*/
#include<reg51.h>
sfr sc=0xA0;//port 2 is labelled as sc, here 0xA0 is address os port 2.
void delay(int);
void main()
{
unsigned char x;
sc=0x00;//output port
while(1)
{
for(x=0;x<5;x++)
{
delay(10);
FTCCOERP
Programming using Embedded C|U 2
*/
#include<reg51.h>
sfr tr=0xA0;
void main()
{
unsigned char x;
tr=0x00;//output port
while(1)
{
x=0;
do
{
tr=x; //to generate rising part
x++;
}while(x!=255);
do
{
tr=x; //to generate falling part
x--;
}while(x!=0);
}
}
22. Write 89C51 ‘C’ language program to generate square wave of 10 KHz on
pin P2-7 using timer 0 interrupt.
Assume crystal frequency as 12 MHz
/* Embedded C program
to generate square wave of 10kHz on P2.7 using interrupt
Solution:
to generate square wave of 10kHz on port P2.7 pin
for timer delay use T0 in Mode 1
FTCCOERP
Programming using Embedded C|U 2
*/
#include<reg51.h>
sbit sq=P2^7;
void timer0() interrupt 1
{
sq=~sq;
}
void main()
{
P2=0x00;//output port
while(1)
{
TMOD=0x01; // 0 0 0 0 0 0 0 1 T0 is used in Mode 1
TH0=0XFF;
TL0=0XCE; //FFFB FFFC FFFD FFFE FFFF 10000
IE=0x82;
TR0=1;
while(1)
{}
}
}
23.
/*
Write a 'C' program to transfer the letter
"M" serially at 9600 baud rate continuously
solution:
data or character==> "M"
baudrate==> 9600
*/
#include<reg51.h>
void main()
{
TMOD=0x20; //timer 1 mode2---> 8bit auto reload mode is selected
TH1=-3;//TH1=0xFD..to set baudrate=9600
SCON=0xc0;//serial mode 0 selected
TR1=1;//to start the timer 1
while(1)
{
TI=0;
SBUF='M';//data copied to SBUF
while(TI!=1)
{}
}
}
24. Write a ‘C’ program to transfer the message “MSBTE” serially
at 9600 baud rate continuously
/*
Write a 'C' program to transfer the message
FTCCOERP
Programming using Embedded C|U 2
for(i=0;i<20;i++)
{
TI=0;
SBUF=msg[i];//get character in SBUF
while(TI!=1)//wait for TI flag.
{}
}
}
}
26. Write 89C51 ‘C’ program to transfer the message “INDIA”,
serially at 9600 band rate continuously. Use 8 bit data and 1 stop bit
/*
Write 89C51 'C' program to transfer the message "INDIA",
serially at 4800 band rate continuously.
Use 8 bit data and 1 stop bit
solution:
data or Message==> "INDIA"
baudrate==> 4800
Use 8 bit data and 1 stop bit===>serial mode 1
*/
#include<reg51.h>
unsigned char msg[5]= "INDIA";
unsigned char i;
void main()
{
TMOD=0x20; //timer 1 mode2---> 8bit auto reload mode is selected
TH1=-6;//TH1=0xFA..to set baudrate=4800
SCON=0x80;//serial mode 1 selected
TR1=1;//to start the timer 1 i.e. 8 bit data and 1 stop bit
while(1)
{
for(i=0;i<5;i++)
{
TI=0;
SBUF=msg[i]; //get character in SBUF
while(TI!=1) //wait for TI flag.
{}
}
}
}
27.
/*
program using INT1 interrupt...
*/
#include<reg51.h>
unsigned char i;
void isrx() interrupt 2 //ISR for INT1 interrupt
{
for(i=0;i<5;i++)
{
P2=0x00;
P2=0xFF;
}
FTCCOERP
Programming using Embedded C|U 2
}
void main()
{
unsigned char x,y;
IE=0x84;//enable INT1 interrupt..
while(1)
{
y=0;
for(x=0;x<10;x++)
{
P1=y++;
}
}
}
28.
/*
program to handle all the interrupts...
*/
#include<reg51.h>
unsigned char i,a,b;
void isr0() interrupt 0 //ISR for INT0 interrupt
{
for(a=0;a<10;a++)
{
P0=0x00;
P0=0xFF;
}
}
void isr1() interrupt 1 //ISR for TF0 interrupt
{
for(a=0;a<10;a++)
{
P1=0x00;
P1=0xFF;
}
}
void isr2() interrupt 2 //ISR for INT1 interrupt
{
for(a=0;a<10;a++)
{
P2=0x00;
P2=0xFF;
}
}
void isr3() interrupt 3 //ISR for TF1 interrupt
{
for(a=0;a<10;a++)
{
P3=0x00;
P3=0xFF;
}
}
void isr4() interrupt 4 //ISR for serial interrupt
{
for(i=0;i<10;i++)
FTCCOERP
Programming using Embedded C|U 2
{
P0=0x00;
P1=0x00;
P2=0x00;
P3=0x00;
P0=0xFF;
P1=0xFF;
P2=0xFF;
P3=0xFF;
}
}
void main()
{
unsigned char x,y;
IE=0x84;//enable INT1 interrupt..
while(1)
{
y=0;
for(x=0;x<10;x++)
{
P1=y++;
}
}
}
FTCCOERP