8051 Practice Programs
-
Upload
bhulakshmi-boppana -
Category
Documents
-
view
221 -
download
0
Transcript of 8051 Practice Programs
-
7/28/2019 8051 Practice Programs
1/25
LEDs
/
**************************************************************************
************Program 1:Write a program to scroll the 2 LEDs left side?
**************************************************************************************/
Algorithm:
1.First,we define P2 to LED.2.We should do left shift operation.
3.Two LEDs are blinking.
Program:#include
#define LED P2
ui i;
void main(){
while(1)
{P2=0x03;
for(i=0;i
-
7/28/2019 8051 Practice Programs
2/25
/***********************************************************************
Program 2:Write a program to scroll the 2 LEDs right side?
***********************************************************************/
Algorithm: 1.First,we define P2 to LED.
2.Initialize the value to P2.
3.We should do right shift operation.4.Two LEDs are blinking.
Program:#include
#define LED P2
ui i;void main()
{while(1)
{
P2=0x03;for(i=0;i>2;
}
}
/***********************************************************************
OUTPUT:We observed that,scrolled the LEDs right side.
**********************************************************************/
2
-
7/28/2019 8051 Practice Programs
3/25
/***********************************************************************
Program 3:Write a program to blink Green to Red alternate way?
***********************************************************************/
Algorithm:
1.First,we define P2 to LED.
2.Initialize the values to P2.
3.LEDs are blinking alternately.
Program:
#include
#define LED P2
void Delay();void main()
{
ui i=5;while(1)
{
P2=0xAA;
Delay(i);P2=0x55;
Delay(i);
}}
void Delay(){
ui j;
for(j=0;j
-
7/28/2019 8051 Practice Programs
4/25
/***********************************************************************
Program 4:Write a program,when switch is pressed LEDs are blinking?
***********************************************************************/
Algorithm:1.First,we define P2 to LED.
2.define a switch for P1_1.
3.Continuously press the switch to toggle LEDs.
Program:
#include#define LED P2
#define SW P1_1
void main()
{P1_1=1;
while(1)
{
if(P1_1==1){
P2=~P2;
}}
}
/***********************************************************************
OUTPUT:We observed that,switch is pressed LEDs are blinking.
***********************************************************************/
4
-
7/28/2019 8051 Practice Programs
5/25
/***********************************************************************
Program 5:Write a program,when switch is pressed 3 times toggle LEDs?
***********************************************************************/
Algorithm:1.First,we define P2 to LED.
2.define a switch for P0_4.
3.Repeat the loop 3 times for pressing the switch.
4.Toggle the LEDs after the switch 3 times pressed.
Program:
#include#define LED P2
#define SW P0_4
void main()
{ui i;
while(1)
{
for(i=0;i
-
7/28/2019 8051 Practice Programs
6/25
TIMERS & COUNTERS
/***********************************************************************
Program 1:Write a program to toggle all the LEDs with 500ms delay using
Timer0, mode1?
***********************************************************************/
Algorithm:
1.To set the values of TMOD.
2.Toggle all LEDs with 500ms delay.
Program:
#include
#define LED P2
void Delay_500ms();
void main()
{while(1)
{
P2=0x55; //toggle all the bits of P2Delay_500ms();
P2=0xAA; //toggle all the bits of P2
Delay_500ms();}
}
void Delay_500ms(){
TMOD=0x01; //timer0,mode 1
TL1=0xFE; //load TL1TH1=0xA5; //load TH1
TR0=1; //start timer
while(!TF0); //wait for TF0 to roll over
TR0=0; //stop timerTF0=0;
}
/***********************************************************************
OUTPUT:We observed that, all the LEDs are toggling with 500ms delay.
***********************************************************************/
6
-
7/28/2019 8051 Practice Programs
7/25
-
7/28/2019 8051 Practice Programs
8/25
/***********************************************************************
Program 3:Write a program for counter 0,in mode 2 to count the switchpressing and display
that on P2?
***********************************************************************/
Algorithm:
1.To set the values of TMOD.2.To count the switch pressing and display that on Port 2.
Program:
#include
#define SW P1_4
ui count;
void main(){
TMOD=0x06;TH0=0x00;
while(1)
{
do{
if(SW==0)
{count++;
P2=count;
SW=1;}
}
while(TF0==0);
TR0=0;TF0=0;
}
}
/***********************************************************************
OUTPUT:We observed that,counting the switch pressing and displayed on
Port 2.
***********************************************************************/
8
-
7/28/2019 8051 Practice Programs
9/25
/***********************************************************************
Program 4:Write a program for 50ms delay using timer 0 mode 1 to togglethe LEDs?
***********************************************************************/
Algorithm:
1.To set the values of TMOD.2.Using 50ms delay,toggle the LEDs.
Program:
#define
void Delay_50ms();
void main()
{while(1)
{
P2=0x00;Delay_50ms();
P2=~P2;
Delay_50ms();
}}
void Delay_50ms()
{TMOD=0x01; //timer 0,mode 1
TH0=0x3C;
TL0=0xB0;TR1=1; //timer start
while(!TF0);
TR0=0; //stop timer
TF0=0;}
/***********************************************************************OUTPUT:We observed that,to toggle all the LEDs with 50ms delay.
***********************************************************************/
9
-
7/28/2019 8051 Practice Programs
10/25
SERIAL COMMUNICATION
/***********************************************************************
Program 1:Write a program to transfer the single letter serially?
***********************************************************************/
Algorithm:1.To set the values of TMOD and SCON.
2.To send the data serially.
Program:
#include
void Sconfig();void Send_char(uc);
void main()
{
Sconfig();Send_char('A');
}
void Sconfig(){
SCON=0x50;TMOD=0x20;TL1=0xFD;
TH1=0xFD;
TR1=1;}
void Send_char(uc dt)
{SBUF=dt;
while(!TI);
TI=0;
}
/***********************************************************************
OUTPUT:We observed that,transmitted a single letter only.
***********************************************************************/
10
-
7/28/2019 8051 Practice Programs
11/25
/***********************************************************************
Program 2:Write a program to transfer the word serially?
***********************************************************************/Algorithm:
1.To set the values of TMOD and SCON.
2.To send the data serially.
Program:
#include
void Sconfig();
void Send_char(uc);
void main(){
Sconfig();
Send_char('W');
Send_char('E');Send_char('L');
Send_char('C');Send_char('O');
Send_char('M');
Send_char('E');
}
void Sconfig()
{SCON=0x50;
TMOD=0x20;
TL1=0xFD;TH1=0xFD;
TR1=1;
}void Send_char(uc dt)
{
SBUF=dt;
while(!TI);TI=0;
}
/***********************************************************************OUTPUT:We observed that,transmitted the word serially.
***********************************************************************/
11
-
7/28/2019 8051 Practice Programs
12/25
/***********************************************************************
Program 3:Write a program to transfer the string serially?
***********************************************************************/Algorithm:
1.To set the values of TMOD and SCON.
2.To send the data serially.
Program:
#include
void Sconfig();
void Send_char(uc);void Send(uc *);
void main()
{ Sconfig();
Send("RAGHU");while(1);
}
void Sconfig(){
TMOD=0x20; //timer 1,mode 2
TH1=0xFD;
TL1=0xFD;SCON=0x50;
TR1=1; // start timer
}void Send(uc *str)
{
while(*str){
Send_char(*str);
str++;
}}
void send_char(uc *mybyte)
{SBUF=*mybyte;
while(!TI);
TI=0;}
/***********************************************************************
OUTPUT:We observed that,the data can be send serially.
***********************************************************************/
12
-
7/28/2019 8051 Practice Programs
13/25
/***********************************************************************
Program 4:Write a Program to receive the data serially?
***********************************************************************/
Algorithm: 1.To set the values of TMOD and SCON.
2.To receive the data serially.
Program:
#include
void Sconfig();
void Read_char();
void main()
{
Sconfig();Read_char();
}
void Sconfig()
{TMOD=0x20; //timer 1,mode 2
TH1=0xFD;
TL1=0xFD;
SCON=0x50;TR1=1; // start timer
}
void Read_char()
{
uc dt;while(!RI);
dt=SBUF;
RI=0;
}
/***********************************************************************
OUTPUT:We observed that,the data canbe received serially.
***********************************************************************/
/***********************************************************************
13
-
7/28/2019 8051 Practice Programs
14/25
Program 5: Write a program to send the data and as well as receive the
data serially?
***********************************************************************/
Algorithm:
1.To set the values of TMOD and SCON.
2.To send the data and receive the data serially.
program:
#include
void Read_char(void);
void Send_char(uc);
void Send(uc *);void Sconfig();
void main(void)
{
Sconfig();Send("Raghu");
Read_char();
while(1);
}
void Sconfig()
{SCON=0x50;
TMOD=0x20;
TL1=0xFD;TH1=0xFD;
TR1=1;
}
void Send(uc *str)
{while(*str)
{
Send_char(*str);str++;
}
}
void send_char(uc *mybyte)
14
-
7/28/2019 8051 Practice Programs
15/25
{
SBUF=*mybyte;
while(!TI);TI=0;
}
void Read_char(void)
{
uc dt;while(!RI);
dt=SBUF;
RI=0;
}
/***********************************************************************
OUTPUT:We observed that,the data canbe send and received serially.
***********************************************************************/
15
-
7/28/2019 8051 Practice Programs
16/25
INTERRUPTS
/***********************************************************************
Program 1: Write a program to get the single bit of data from P1_7 andsends it to P1_0 continuously. While simultaneously create a square wave of
200 micro second on P2_5, using timer 0, mode 2?
***********************************************************************/
Algorithm:1.To set the value of TMOD, and enable the interrupt, start the
timer.
2.When you switched ON, the LED is blinking and at the same
timethe corresponding wave form is generated.
Program:
#include
#define SW P1_7
#define LED P1_0#define WAVE P2_5
void time();
void timer0(void) interrupt 1
{
WAVE=~WAVE; //toggletime();
}
void main()
{
time();
while(1){
LED=~SW; //send to switch
}}
void time(){
TMOD=0x02; //timer 0,mode 2
TL0=0xA4;
16
-
7/28/2019 8051 Practice Programs
17/25
TH0=0xA4;
IE=0x82; //enable interrupts for timer 0
TR0=1;}
/***********************************************************************
OUTPUT: We observed that, when you switched ON, the LED is blinking and
Square wave generated with 200 micro seconds.
***********************************************************************/
17
-
7/28/2019 8051 Practice Programs
18/25
/***********************************************************************
Program 2: Write a program to get the single bit of data from P1_7 andSends it to P1_0
***********************************************************************/
Algorithm:
1.Set the values of TMOD and SCON and Initialize the interrupts.2.Continuously toggling the LED, when the time is completed the
the square wave will be generated.
3.When the serial interrupt is enabled with TI or RI, Transmitting
and Receiving is done.
Program:
#include
#define WAVE P1_2
void timer0(void) interrupt 1{
WAVE=~WAVE; //toggle
}
void serial0(void) interrupt 4
{
if(TI==1){
SBUF='D';
TI=0;}
else
{
P2=SBUF;RI=0;
}
}
void main(){
TMOD=0x22;
TH0=0xA4;
SCON=0x50;TR0=1;
TR1=1;
18
-
7/28/2019 8051 Practice Programs
19/25
IE=0x92;
while(1)
{P1_3=~P1_3;
}
}
/***********************************************************************
Output: We observe that, generate a square wave and serial
communication using interrupts.
***********************************************************************/
19
-
7/28/2019 8051 Practice Programs
20/25
LCD
/***********************************************************************
Program 1: Write a program to get the single bit of data from P1_7 and
sends it to P1_0
***********************************************************************/
Algorithm:
1.Initialize the
2.Set the command
#include
#include
#define DATA P2#define EN P1_2
#define RW P1_1#define RS P1_0
#define cmd 0
#define dat 1#define EN2 P1_7
void LCD_INIT();void LCD_CMD(uc,uc);
void lprintf(uc *st);
void Delay(ui);
void main(void)
{
uc *ijk="RAGHU";LCD_INIT();
lprintf( ijk);
while(1);}
void LCD_INIT()
{LCD_CMD(0x38,cmd);
LCD_CMD(0x01,cmd);
LCD_CMD(0x06,cmd);LCD_CMD(0x0e,cmd);
LCD_CMD(0x80,cmd);
Delay(1);}
void LCD_CMD(uc c,uc k)
20
-
7/28/2019 8051 Practice Programs
21/25
{
DATA=c;
RS=k;RW=0;
EN=1;
_nop_();EN=0;
EN2=0;
_nop_();EN2=1;
// Delay(1);
}
void lprintf(uc *r)
{
while(*r){
LCD_CMD(*r,dat);r++;
}
}
void Delay(ui n)
{ui d;
while(n--)
for(d=0;d
-
7/28/2019 8051 Practice Programs
22/25
/***********************************************************************
Program 2: Write a program to scroll a string to right side
continuously in 2 lines?**********************************************************************/
#include
void main(){
LCD_init();
while(1)
{
unsigned char j,i;
for(j=0xc0;j=0x80;i--)
{
LCD_write(i,CMD);Lprintf("kisi");
LCD_write(0x01,CMD);
}}
}
//*LCD_write(i,CMD);*//
//* LCD_write(0x01,CMD);*//
TMOD=0x01;TH1=0xD8;
TL1=0xf0;
TR0=1;while(!TF0);
TR0=0;
TF0=0;}
22
-
7/28/2019 8051 Practice Programs
23/25
/***********************************************************************
Program 3: Write a program to scroll a string to right side
continuously in 2 lines with some time delay?***********************************************************************/
#includevoid main()
{
LCD_init();
while(1)
{
{
unsigned char j,i;
for(j=0xc0;j=0x80;i--)
{
LCD_write(i,CMD);
TMOD=0x01;TH1=0xD8;
TL1=0xf0;
TR0=1;while(!TF0);
TR0=0;
TF0=0;Lprintf("kisi");
LCD_write(0x01,CMD);
while(1);
TMOD=0x01;TH1=0xD8;
TL1=0xf0;
23
-
7/28/2019 8051 Practice Programs
24/25
TR0=1;
while(!TF0);
TR0=0;TF0=0;
}
}}
}
}
24
-
7/28/2019 8051 Practice Programs
25/25
/***********************************************************************
Program 4: Write a program to scroll a string to right sidecontinuously, first line should scroll once for every full scroll of the other
line?
***********************************************************************/
#includevoid main()
{
LCD_init();
while(1)
{
unsigned char j,i;
for(j=0xc0;j=0x80;i--)
{
LCD_write(i,CMD);Lprintf("kisi");
LCD_write(0x01,CMD);
}}
}
//*LCD_write(i,CMD);*//
//* LCD_write(0x01,CMD);*//
TMOD=0x01;TH1=0xD8;
TL1=0xf0;
TR0=1;while(!TF0);
TR0=0;
TF0=0;
}