Sei sulla pagina 1di 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<treg51.h>
#define LED P2
ui i;
void main()
{
while(1)
{
P2=0x03;
for(i=0;i<=3;i++)
P2=P2<<2;
}
}
/***********************************************************************
OUTPUT:We observed that,scrolled the LEDs left side.
***********************************************************************/

/***********************************************************************
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<treg51.h>
#define LED P2
ui i;
void main()
{
while(1)
{
P2=0x03;
for(i=0;i<=3;i++)
P2=P2>>2;
}
}
/***********************************************************************
OUTPUT:We observed that,scrolled the LEDs right side.
**********************************************************************/

/***********************************************************************
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<treg51.h>
#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<1825;j++);
}

/***********************************************************************
OUTPUT:We observed that,blinking the LEDs Green to Red alternately.
***********************************************************************/

/***********************************************************************
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<treg51.h>
#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.
***********************************************************************/

/***********************************************************************
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<treg51.h>
#define LED P2
#define SW P0_4
void main()
{
ui i;
while(1)
{
for(i=0;i<=2;i++)
{
P0_4=0;
_nop_();
P0_4=1;
}
if(i==3)
{
P2=~P2;
}
}
}
/***********************************************************************
OUTPUT:We observed that,switch is pressed 3 times and LEDs are
blinking.
***********************************************************************/

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<treg51.h>
#define LED P2
void Delay_500ms();
void main()
{
while(1)
{
P2=0x55;
//toggle all the bits of P2
Delay_500ms();
P2=0xAA;
//toggle all the bits of P2
Delay_500ms();
}
}
void Delay_500ms()
{
TMOD=0x01;
//timer0,mode 1
TL1=0xFE;
//load TL1
TH1=0xA5;
//load TH1
TR0=1;
//start timer
while(!TF0);
//wait for TF0 to roll over
TR0=0;
//stop timer
TF0=0;
}
/***********************************************************************
OUTPUT:We observed that, all the LEDs are toggling with 500ms delay.
***********************************************************************/

/***********************************************************************
Program 2:Write a program for counter 0,in mode 1 to count the pulses
and display the TH0 and TL0 registers on P2,P1.
***********************************************************************/
Algorithm:
1.To set the values of TMOD.
2.To count the pulses and display the TH0 and TL0 registers
on P2,P1.
Program:
#include<treg51.h>
#define T0 P3_4
void main()
{
TMOD=0x05; //counter 0,mode 1
TL0=0x00;
//set count to 0
TH0=0x00; //set count to 0
while(1)
//repeat forever
{
do
{
T0=1;
P1=TL0;
P2=TH0;
TR0=1;
//start timer
T0=0;
}
while(TF0==0);
TR0=0;
TF0=0;
}
}
/***********************************************************************
OUTPUT:We observed that,counting the pulses and displayed on P1,P2
ports.
***********************************************************************/

/***********************************************************************
Program 3:Write a program for counter 0,in mode 2 to count the switch
pressing 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<treg51.h>
#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.
***********************************************************************/

/***********************************************************************
Program 4:Write a program for 50ms delay using timer 0 mode 1 to toggle
the LEDs?
***********************************************************************/
Algorithm:
1.To set the values of TMOD.
2.Using 50ms delay,toggle the LEDs.
Program:
#define<treg51.h>
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.
***********************************************************************/

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<treg51.h>
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

/***********************************************************************
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<treg51.h>
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

/***********************************************************************
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<treg51.h>
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

/***********************************************************************
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<treg51.h>
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

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<treg51.h>
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

{
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

INTERRUPTS
/***********************************************************************
Program 1: Write a program to get the single bit of data from P1_7 and
sends 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
time
the corresponding wave form is generated.
Program:
#include<treg51.h>
#define SW P1_7
#define LED P1_0
#define WAVE P2_5
void time();
void timer0(void) interrupt 1
{
WAVE=~WAVE;
//toggle
time();
}
void main()
{
time();
while(1)
{
LED=~SW;
}
}
void time()
{
TMOD=0x02;
TL0=0xA4;

//send to switch

//timer 0,mode 2
16

TH0=0xA4;
IE=0x82;
TR0=1;

//enable interrupts for timer 0

}
/***********************************************************************
OUTPUT: We observed that, when you switched ON, the LED is blinking and
Square wave generated with 200 micro seconds.
***********************************************************************/

17

/***********************************************************************
Program 2: Write a program to get the single bit of data from P1_7 and
Sends 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<treg51.h>
#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

IE=0x92;
while(1)
{
P1_3=~P1_3;
}
}
/***********************************************************************
Output: We observe that, generate a square wave and serial
communication using interrupts.
***********************************************************************/

19

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<treg51.h>
#include<intrins.h>
#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

//
}

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<1825;d++);
}

21

/***********************************************************************

Program 2: Write a program to scroll a string to right side


continuously in 2 lines?
**********************************************************************/
#include<LCD.H>
void main()
{
LCD_init();
while(1)
{
unsigned char j,i;
for(j=0xc0;j<=0xcb;j++)
{
LCD_write(j,CMD);
Lprintf("naga");
LCD_write(0x01,CMD);

for(i=0x8b;i>=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

/***********************************************************************
Program 3: Write a program to scroll a string to right side
continuously in 2 lines with some time delay?
***********************************************************************/
#include<LCD.H>
void main()
{
LCD_init();
while(1)
{
{
unsigned char j,i;
for(j=0xc0;j<=0xcb;j++)
{
LCD_write(j,CMD);
TMOD=0x01;
TH1=0xD8;
TL1=0xf0;
TR0=1;
while(!TF0);
TR0=0;
TF0=0;
Lprintf("naga");
while(1);
for(i=0x8b;i>=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

TR0=1;
while(!TF0);
TR0=0;
TF0=0;
}
}
}
}
}

24

/***********************************************************************
Program 4: Write a program to scroll a string to right side
continuously, first line should scroll once for every full scroll of the other
line?
***********************************************************************/
#include<LCD.H>
void main()
{
LCD_init();
while(1)
{
unsigned char j,i;
for(j=0xc0;j<=0xcb;j++)
{
LCD_write(j,CMD);
Lprintf("naga");
LCD_write(0x01,CMD);
for(i=0x8b;i>=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;
}

25

Potrebbero piacerti anche