Embedded
Embedded
1)Assume that an external clock is connected to pin T0(P3.4). Write an 8051 C program in
counter 0 mode 1(16-bit mode) to count the number of clock pulses through TL0 & TH0 and
produce the output at port 1(P1) and port 2 (P2). Start the count at 00H.
#include <reg51.h>
sbit T0pin=P3^4;
void main()
TL0=0x00;
TH0=0x00;
while(1)
TR0=1;
while(TF0==0)
P1=TL0;
P2=TH0;
TR0=0;
TF0=0;
}
2) Assume that an external clock is connected to pin T0(P3.4). Write an 8051 C program in
counter 0 mode 1(16-bit mode) to count 10 number of clock pulses through TL0 & TH0 and
produce the output at port 1(P1) and port 2 (P2). Start the count at 00H.
#include <reg51.h>
sbit T1pin=P3^4;
void main()
while(1)
TL0=0x00;
TH0=0x00;
TR0=1;
while(TL0<=9)
P1=TL0;
P2=TH0;
TR0=0;
TF0=0;
}
3)Writ an 8051 C program to toggle all the bits of P1 with a delay of 1 sec for 5 times (5 clock
pulses). Use timer 0 and mode 1 to create the delay.
#include <reg51.h>
TH0=0x4B;
void main()
{ int i, j;
for(j=0;j<5;j++)
P1=0x00;
for(i=0;i<20;i++)
delay();
P1=0xFF;
for(i=0;i<20;i++)
delay();
}
4) Writ an 8051 C program to generate a clock with 1 Hz square wave(5pulses) using timer 0 and
mode 1and use this clock as input to P3.5 and count the pulses using the counter 1 in mode 2
operation and display the count at port2.
#include <reg51.h>
sbit T1pin=P3^5;
sbit portpin=P1^0;
void delay(void)
TH0=0x4B;
void main()
{ int i,j;
T1pin=1;
TH1=0x00;
TL1=0x00;
TR1=1;
for(i=0;i<5;i++)
P1=0x00;
T1pin=portpin;
for(j=0;j<20;j++)
delay();
P2=TL1;
P1=0xFF;
T1pin=portpin;
for(j=0;j<20;j++)
delay();
P2=TL1;
}
#SERIAL COMMUNICATIONS
1) Write an 8051 C program to transmit your name serially with a baud rate of 9600
continuously. Use 8-bit data and 1-stop bit.
#include <reg51.h>
void main()
while(1)
for(i=0;i<13;i++)
}
2) Write an 8051 C program to receive data serially with a baud rate of 4800 continuously.
Use 8-bit data and 1-stop bit.
#include <reg51.h>
void main()
unsigned char i;
while(1)
while(RI==0);
i=SBUF;
P1=i;
RI=0;
}
3) Write an 8051 C program to transmit data serially with a baud rate of 57200
continuously(Doubling the baud rate). Use 8-bit data and 1-stop bit.
#include <reg51.h>
void main()
{
TMOD=0x20; //Timer 1, 8-bit auto reload mode
TH1=0xFF; //Load value for 57200 baud rate bps
SCON=0x50; /* Mode 1, reception enable */
TR1=1; //Start timer 1
PCON=0x80;
SBUF='S'; //Load 'S' in SBUF to transmit
while(TI==0); //Wait till TI flag is set or data transmission ends
TI=1; //Clear TI flag
while(1);
}
4) Write an 8051 C program to send two different strings to the serial port. Assuming that SW is
connected to pin P2.0, monitor its status and make a decision as follows:
Assume XTAL = 11.0592 MHz, baud rate of 2400, 8-bit data, 1 stop bit.
#include <reg51.h>
void main(void)
char i;
char name[]="Khushi";
char lastname[]="Mittal";
TMOD=0x20;
TH1=-12;
SCON=0x50;
TR1=1;
while(1)
if (sw==0)
for(i=0;i<6;i++)
{
SBUF=name[i];
while(TI==0);
TI=0;
}}
else if(sw==1)
for(i=0;i<6;i++)
SBUF=lastname[i];
while(TI==0);
TI=0;}}}}
5)
Write an 8051 C program to send two different message to the serial port. Assuming that SW is
connected to pin P2.0, monitor its status and make a decision as follows:
#include <reg51.h>
void main(void)
char z;
SCON=0x50;
while(1)
{
if(MYSW==0)
for (z=0;z<12;z++) {
TI=0;
}}
else {
for (z=0;z<10;z++) {
PCON=0x80;
TI=0;}}}}
#INTERRUPTS:
1) Write an 8051 C program to transmit the data continuously from P1.0 to P1.7 and
simultaneously produce a square wave with a period of 400 micro seconds at P2.0. Use
timer 0 mode 2.
#include <reg51.h>
sbit input1=P1^0;
sbit output1=P1^7;
sbit sqwave=P2^0;
sqwave=~sqwave;
void main()
input1=0;
TMOD=0x02;
TH0=0x49;
IE=0x82;
TR0=1;
while(1)
output1=input1;
2) Write an 8051 C program to toggle P1 data continuously and simultaneously toggle the
led connected to P0.0 only when an external input received at P3.2(INT0) pin.
#include <reg51.h>
sbit led=P0^0;
led=~led;
void main()
led=0;
swi=1;
IE=0x81;
while(1)
P1=~P1;
3) Write an 8051 C program to transmit the data continuously from P1.0 to P1.7 and
receive data serially send it to P2 with a baud rate of 9600.
#include <reg51.h>
sbit input1=P1^0;
sbit output1=P1^7;
P2=SBUF;
RI=0;
void main()
input1=0;
TMOD=0x20;
TH1=0xFD;
SCON=0x50;
IE=0x90;
TR1=1;
while(1)
output1=input1;
4) Write an 8051 C program to display the number of people in a room,where entry gate
sesnor is connected to P3.2 and exit gate sensor is connected to P3.3.
#include <reg51.h>
count++;
count--;
void main()
{
TCON=0x05;
IE=0x85;
while(1)
P0=count;
(b) Read port P1, transmit data serially, and give a copy to P2
Assume that XTAL = 11.0592 MHz. Set the baud rate at 4800.
#include <reg51.h>
if (TI==1) {
else {
void main() {
unsigned char x;
SCON=0x50;
while (1) {
6)
(b) Use timer 1 as an event counter to count up a 1-Hz pulse and display it on P0. The pulse
is connected to EX1.
Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600.
#include <reg51.h>
void main() {