0% found this document useful (0 votes)
63 views15 pages

Embedded

The document contains examples of 8051 microcontroller programs using counters, serial communication, interrupts and timers. It provides code snippets to count clock pulses, generate delays, transmit and receive data serially, toggle pins using interrupts and timers, and monitor sensor inputs using interrupts.

Uploaded by

Khushi Mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views15 pages

Embedded

The document contains examples of 8051 microcontroller programs using counters, serial communication, interrupts and timers. It provides code snippets to count clock pulses, generate delays, transmit and receive data serially, toggle pins using interrupts and timers, and monitor sensor inputs using interrupts.

Uploaded by

Khushi Mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

//COUNTERS:

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()

T0pin=1; // External input

TMOD=0x05; // Counter mode, mode 1, timer 0

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()

T1pin=1; // External input

TMOD=0x05; // Counter mode, mode 1, timer 0

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>

void delay(void) // delay function produces 50 msec delay

TMOD=0x01; // Timer 0 mode 1

TL0=0xFD; // Load TL0 & TH0 as per the time delay

TH0=0x4B;

TR0=1; // Start Timer 0

while(TF0==0); // Run here till TF0=0

TR0=0; // Stop the timer 0

TF0=0; // Reset the overflow timer 0 flag

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)

TMOD=0x61; // Timer 0 mode 1

TL0=0xFD; // Load TL0 & TH0 as per the time delay

TH0=0x4B;

TR0=1; // Start Timer 0

while(TF0==0); // Run here till TF0=0

TR0=0; // Stop the timer 0

TF0=0; // Reset the overflow timer 0 flag

void main()

{ int i,j;

T1pin=1;

TMOD=0x61; // Counter mode, mode 2, timer 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()

char i, name[]="SRINIVASA RAO";

TMOD=0x20; //Timer 1, 8-bit auto reload mode

TH1=0XFD; //Load value for 9600 baud rate bps

SCON=0x50; /* Mode 1, reception enable */

TR1=1; //Start timer 1

while(1)

for(i=0;i<13;i++)

SBUF=name[i]; //Load ‘H' in SBUF to transmit

while(TI==0); //Wait till TI flag is set or data transmission ends

TI=0; //Clear TI flag

}
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;

TMOD=0x20; //Timer 1, 8-bit auto reload mode

TH1=0XFA; //Load value for 4800 baud rate bps

SCON=0x50; /* Mode 1, reception enable */

TR1=1; //Start timer 1

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:

Switch = 0: send your first name

Switch = 1: send your last name

Assume XTAL = 11.0592 MHz, baud rate of 2400, 8-bit data, 1 stop bit.

#include <reg51.h>

sbit sw= P2^0;

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:

Switch = 0: “Normal Speed” with a baud rate of 28800

Switch = 1: “High Speed” with a baud rate of 57600

Assume XTAL = 11.0592 MHz

#include <reg51.h>

sbit MYSW=P2^0; //input switch

void main(void)

char z;

char Mess1[]="Normal Speed";

char Mess2[]= "High Speed";

TMOD=0x20; //use Timer 1, mode 2

TH1=0xFF; //28800 for normal

SCON=0x50;

TR1=1; //start timer

while(1)
{

if(MYSW==0)

for (z=0;z<12;z++) {

SBUF=Mess1[z]; //place value in buffer

while(TI==0); //wait for transmit

TI=0;

}}

else {

for (z=0;z<10;z++) {

PCON=0x80;

SBUF=Mess2[z]; //place value in buffer

while(TI==0); //wait for transmit

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;

void itpro(void) interrupt 1

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 swi= P3^2;

sbit led=P0^0;

void extint() interrupt 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;

void itpro(void) interrupt 4


{

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>

unsigned char count=0;

void int0() interrupt 0

count++;

void int1() interrupt 2

count--;

void main()

{
TCON=0x05;

IE=0x85;

while(1)

P0=count;

5) Write a C program using interrupts to do the following:

(a) Receive data serially and send it to P0

(b) Read port P1, transmit data serially, and give a copy to P2

(c) Make timer 0 generate a square wave of 5 kHz frequency on P0.1

Assume that XTAL = 11.0592 MHz. Set the baud rate at 4800.

#include <reg51.h>

sbit WAVE =P0^1;

void timer0() interrupt 1 {

WAVE=~WAVE; //toggle pin

void serial0() interrupt 4 {

if (TI==1) {

TI=0; //clear interrupt

else {

P0=SBUF; //put value on pins

RI=0; //clear interrupt

void main() {

unsigned char x;

P1=0xFF; //make P1 an input


TMOD=0x22;

TH1=0xF6; //4800 baud rate

SCON=0x50;

TH0=0xA4; //5 kHz has T=200us

IE=0x92; //enable interrupts

TR1=1; //start timer 1

TR0=1; //start timer 0

while (1) {

x=P1; //read value from pins

SBUF=x; //put value in buffer

P2=x; //write value to pins

6)

Write a C program using interrupts to do the following:

(a) Generate a 10 KHz frequency on P2.1 using T0 8-bit auto-reload

(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>

sbit WAVE =P2^1;

Unsigned char cnt;

void timer0() interrupt 1 {

WAVE=~WAVE; //toggle pin

void timer1() interrupt 3 {

cnt++; //increment counter

P0=cnt; //display value on pins

void main() {

cnt=0; //set counter to 0


TMOD=0x42;

TH0=0x-46; //10 KHz

IE=0x86; //enable interrupts

TR0=1; //start timer 0

while (1); //wait until interrupted

You might also like