This project shows you how to interface 16x2 LCD with 8051 microcontroller, here i have used 89c51 microcontroller, but you can use any microcontroller of 8051.
LCD is connected to port 2 and some pins of port 3 of 89c51 microcontroller.
You can change the data by calling function LCD_Show(), for example by calling function LCD_Show('P');
will show "P" on lcd.
C CODE FOR LCD INTERFACING
#include <AT89X52.H>
sbit RS=P3^5; //RS=0; command register RS=1; Data register
sbit RW=P3^6;
sbit E=P3^7;
sbit BF=P2^7;
//////////////////////////// DELAY/////////////////////////////
void delay(unsigned int z) //For delay (Starts)
{
unsigned char x;
unsigned int y;
for(x=0;x
for(y=0;y<=1270;y++);
}
}
////////////////////////////// LCD PULSE//////////////////////////
void LCD_Pulse_E(unsigned char t)
{
unsigned char i;
E = 1; for(i=0; i
E = 0; for(i=0; i }
//////////////////////////// READY/////////////////////////////
bit ready(void) //For checking that the LCD is ready or not?
{
BF=1;//Busy flag is one
RS=0;// Command register is one
RW=1;//Read the data from LCD
while(BF==1)
{
E=0;
E=1;
}
return 1;
}
///////////////////////////////// LCD SHOW///////////////////////
void LCD_Show(unsigned char CHARACTER)
{
if(ready())
{
P2=CHARACTER;
RS=1; delay(1);//Data register
RW=0; delay(1);//Write on LCD
LCD_Pulse_E(255);
delay(1);
}
}
/////////////////////////// LCD RUN/////////////////////////
void LCD_Run_Command(unsigned char COMMAND)
{
if(ready())
{
P2=COMMAND;
RS=0; delay(1);//command register
RW=0; delay(1);//read from LCD
LCD_Pulse_E(255);//call to LCD_PULSE_E() function
}
}
///////////////////////////////// LCD CURSOR POSITION//////////////
void setcurpos(unsigned char pos)
{
E=1; //Start LCD command
RS=0; //Its a command.
RW=0;
P2=pos;
E=0; //Finish LCD command
}
/////////////// MAIN ////////////////////
void main(void)
{
LCD_Run_Command(0x38);
LCD_Run_Command(0x0C); // Display=ON, Curson=OFF, Cursor Blonking=0ff
LCD_Run_Command(0x01); // clear display
setcurpos(0x80);
while(1)
{
LCD_Show('E'); // char to be displayed
LCD_Show('L'); // char to be displayed
LCD_Show('E');
LCD_Show('C');
LCD_Show('T');
LCD_Show('R');
LCD_Show('O');
LCD_Show('N');
LCD_Show('I');
LCD_Show('C');
LCD_Show(' '); // FOR SPACE
LCD_Show('P');
LCD_Show('R');
LCD_Show('O');
LCD_Show('J');
LCD_Show('E');
setcurpos(0xC0); // switch to next line
LCD_Show('C');
LCD_Show('T');
LCD_Show('S');
LCD_Show('4');
LCD_Show('Y');
LCD_Show('O');
LCD_Show('U');
LCD_Show('.');
LCD_Show('B');
LCD_Show('L');
LCD_Show('O');
LCD_Show('G');
LCD_Show('S');
LCD_Show('P');
LCD_Show('O');
LCD_Show('T');
}
}
LCD is connected to port 2 and some pins of port 3 of 89c51 microcontroller.
You can change the data by calling function LCD_Show(), for example by calling function LCD_Show('P');
will show "P" on lcd.
C CODE FOR LCD INTERFACING
sbit RS=P3^5; //RS=0; command register RS=1; Data register
sbit RW=P3^6;
sbit E=P3^7;
sbit BF=P2^7;
//////////////////////////// DELAY/////////////////////////////
void delay(unsigned int z) //For delay (Starts)
{
unsigned char x;
unsigned int y;
for(x=0;x
for(y=0;y<=1270;y++);
}
}
////////////////////////////// LCD PULSE//////////////////////////
void LCD_Pulse_E(unsigned char t)
{
unsigned char i;
E = 1; for(i=0; i
E = 0; for(i=0; i
bit ready(void) //For checking that the LCD is ready or not?
{
BF=1;//Busy flag is one
RS=0;// Command register is one
RW=1;//Read the data from LCD
while(BF==1)
{
E=0;
E=1;
}
return 1;
}
///////////////////////////////// LCD SHOW///////////////////////
void LCD_Show(unsigned char CHARACTER)
{
if(ready())
{
P2=CHARACTER;
RS=1; delay(1);//Data register
RW=0; delay(1);//Write on LCD
LCD_Pulse_E(255);
delay(1);
}
}
/////////////////////////// LCD RUN/////////////////////////
void LCD_Run_Command(unsigned char COMMAND)
{
if(ready())
{
P2=COMMAND;
RS=0; delay(1);//command register
RW=0; delay(1);//read from LCD
LCD_Pulse_E(255);//call to LCD_PULSE_E() function
}
}
///////////////////////////////// LCD CURSOR POSITION//////////////
void setcurpos(unsigned char pos)
{
E=1; //Start LCD command
RS=0; //Its a command.
RW=0;
P2=pos;
E=0; //Finish LCD command
}
/////////////// MAIN ////////////////////
void main(void)
{
LCD_Run_Command(0x38);
LCD_Run_Command(0x0C); // Display=ON, Curson=OFF, Cursor Blonking=0ff
LCD_Run_Command(0x01); // clear display
setcurpos(0x80);
while(1)
{
LCD_Show('E'); // char to be displayed
LCD_Show('L'); // char to be displayed
LCD_Show('E');
LCD_Show('C');
LCD_Show('T');
LCD_Show('R');
LCD_Show('O');
LCD_Show('N');
LCD_Show('I');
LCD_Show('C');
LCD_Show(' '); // FOR SPACE
LCD_Show('P');
LCD_Show('R');
LCD_Show('O');
LCD_Show('J');
LCD_Show('E');
setcurpos(0xC0); // switch to next line
LCD_Show('C');
LCD_Show('T');
LCD_Show('S');
LCD_Show('4');
LCD_Show('Y');
LCD_Show('O');
LCD_Show('U');
LCD_Show('.');
LCD_Show('B');
LCD_Show('L');
LCD_Show('O');
LCD_Show('G');
LCD_Show('S');
LCD_Show('P');
LCD_Show('O');
LCD_Show('T');
}
}
No comments:
Post a Comment