Sie sind auf Seite 1von 5

1) Program for blinking leds connected to ports P2 and P3.

#include<reg52.h
#include<stdio.h>
void MSdelay(unsigned int);
void main() //main function starts here
{
P2 =0x00; //Clearing P2 and P3
P3 =0x00;
while(1) //Endless loop
{
P2=0xaa; //Togging P2 port leds
P3 =0x00; //ON and OFF leds connected toP3
MSdelay(2000); //Delay
P2=0x55;
P3 =0xFF;
}
}
void MSdelay(unsigned int itime) //delay subroutine
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
2) Program for Transmitting message serially.
#include<reg52.h>
void main()
{
unsigned char i;
unsigned char mesg[]="WELCOME TO NIT WARANGAL 0";
SCON=0X50; //selecting serial communication in mode 1
TMOD=0X20; // use timer-1, 8-bit auto reload mode
TH1=0XFD; //setting the baud rate to 9600
TR1=1; //starting the timer
while(1)
{
for(i=0;mesg[i]!='0';i++)
{
SBUF=mesg[i]; //moving each character to SBUF register
while(TI ==0); //wait for the transmission to be completed
TI=0; //clear transmit interrupt pin (TI) for the next transmission
}
}
}
3) Program for Receiving data serially and display on P2 port.
#include<reg52.h>
//#include<stdio.h>
void main(void)
{
TMOD=0X20; //use timer-1, 8-bit auto reload mode
TH1=0XFD; //9600 baud rate
SCON=0X50; // selecting serial communication in mode 1
TR1=1; //starting the timer
while(1) //continuous loop
{
while(RI==0); // wait to receive data
temp=SBUF; //moving data from SBUF to temp variable
RI=0; //Clearing receive interrupt (RI) flag
P2=temp; //display data on P2
}
}
4) Program for Display Message on LCD
#include <reg51.h>
#include <string.h>
sbit rs = P1^0; // declare P1.0 as rs pin
sbit rw = P1^1; // declare p1.1 as read/write pin
sbit en = P1^2; // declare p1.2 as enable pin
void writecmd(unsigned char a); //function to send command to LCD
void writedat(unsigned char b); //function to send data to LCD
void writestr(unsigned char *s); //function to write string on LCD
void MSdelay(unsigned int); //function for delay to LCD
void main() //main program starts here
{
P2=0x00; //clearing P0 port
writecmd(0x3C); // Initializing LCD
writecmd(0x0E);
MSdelay(20);
writecmd(0x01); // clear memory and home cursor
writestr("Wel-Come to LCD "); // write message in first line
writecmd(0xC4); // move cursor to second line 4th pos
writestr("Program ");
while(1); // continuous loop
}
void writecmd(unsigned char a)
{
rs = 0; // clear rs pin for command
rw = 0; // clear rw pin to write
P2 = a; // send command character
en = 1; // enable LCD
MSdelay(100);
en = 0; //disable LCD
}
void writedat(unsigned char b)
{
rs = 1; // set rs pin for data
rw = 0; // clear rw pin to write
P2 = b; // send data character
en = 1; // enable LCD
MSdelay(100);
en = 0; // disable LCD
}
void writestr(unsigned char *s)
{
unsigned char l,i;
l = strlen(s); // get the length of string
for(i=1;i<l;i++)
{
writedat(*s); // write every char one by one
s++;
}
}
void MSdelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}

Das könnte Ihnen auch gefallen