INTERFACING LCD 16*2 WITH 8051
Blog Contents
INTERFACING LCD 16*2 WITH 8051
SUMMERY:
Connecting LCD 16*2 display to the 8051 microcontroller in 8 bitration method and also passing two commands.
COMPONENTS RECQURIED:
o 8051 Microcontroller
o 16*2 LCD Display
o Switch
o Binary Wires
o Power Supply
PIN DIAGRAM:
SOURCE CODE:
#include
# define lcd P0
sbit rs=P1^0;
sbit e=P1^1;
sbit b=P1^2;
void dealy50ms(int a);
void intial(char b);
void delay1s(unsigned char c);
void write1();
void write2();
void write3();
void write4();
void button()
{
int k=1;
while(k==1)
{
if(b==0)
{
intial(0x01);
k=0;
}
}
}
void display(char *d);
int main()
{
b=1;
while(1)
{
intial(0x38);
intial(0x0c);
intial(0x01);
intial(0x80);
write1();
intial(0xc0);
write2();
button();
intial(0x80);
write3();
intial(0xc0);
write4();
}
}
void display(char *d)
{
for(*d;d!=NULL;d++)
{
lcd=*d;
rs=1;
e=1;
delay1s(1);
e=0;
}
}
void write1()
{
display("Done LCD16*2 ");
}
void write2()
{
display(" SUCESS ");
}
void write3()
{
display("Thank");
}
void write4()
{
display("YOU");
}
void intial(char b)
{
lcd=b;
rs=0;
e=1;
dealy50ms(2);
e=0;
}
void dealy50ms(int a)
{
inti=0;
for(i; i < a ; i++)
{
TMOD=0x01;
TH0=0xff;
TL0=0xfc;
TR0=1;
while(!TF0);
TF0=0;
TR0=0;
}
}
void delay1s(unsigned char c)
{
int i,j;
for(i=0;i < c;i++)
{
for(j=0;j<20;j++)
{
TMOD=0x01;
TH0=0xff;
TL0=0xfc;
TR0=1;
while(!TF0);
TF0=0;
TR0=0;
}
}
}
EXPLIANATION:
Now we will go through code and understand how LCD is working. Any C programalways start with main function. In our code main function contains.
|
|
Main function is started | |
intial(0x38); command 0x38 | |
intial(0x0c);Passing 0x0C value for Display ON, Cursor OFF | |
intial(0x01);Passing 0x01 value for Clear the screen | |
intial(0x80);Passing 0x80 value for Force curser to the 1st line in first position | |
Write1();Calling write1 function to pass the “Done LCD 16*2 ” string to display function |
Next how to send the commands through intial function to LCD
|
In Intial function, Passing the command from Microcontroller P0 to LCD through parallel communication from Microcontroller(P0.0-P0.7) to LCD(D0-D7). “rs” register pin used to set 1 or 0 is used to sending commands. “e” enable pin used to given value to LCD as making high to low. Professional Training Institute is highly focus on practical embedded system training in Bangalore, Above post is writtern by our student Joy and Prathavi. We wish bright carrer ahead to both of them. |
Next is interfacing button to the LCD to send different string data
|
In button function, Run the while part to check every time the value of k. b is connected to the switch when it pressed it becomes zero, It is initially high condition(1). Passing the value 0x01 to clear the screen. Once button is clicked the value of “k” changed to 0 to exit the while loop. |
Next to pass the string value to the LCD through display function
|
Display function getting the string’s first address. rs is the register pin used to intimate the LCD as it is a data not a command. e is a enable pin used to given value to LCD as making high to low. |
Next delay function for 50ms
|
Here is the delay function for 50ms you maximize the value through passing number to make higher, this is done trough using Timer concept |
Next is the delay for 1s
|
Here is the same but used delay function for 1s you maximize the value through passing number to make higher, this is done through using Timer concept |
From above the program it is written for 8 bitration of 16*2 LCD display for 2 commands. Here I used common initialisation command for all 16*2 LCD display which is in intial( ); and the strings are written in write1..4( ) functions and the display commands of working procedures also written in the display( ) function and also two different delays are represented as 50ms and 1s in delay50ms( ) and delay1s( ) function which is written in timer concept.
Professoinal Training Institute is focus in Practical embedded system training in Bangalore. This post is written by our student Joy and Prathavi, based on their executed experiments during training. we wish then for their bright career ahead.
Alternet Working Source Code:
#include
#define Display_Port P0
// Selecting a display port P0
sbit RS = P1^0;
// Selecting Resistor Selection
Port P1.0
sbit EN = P1^1;
// Port P1.1 for Enable Pin
sbit button = P1^2;
void delay_ms(int d);
// Function Prototype to generate mili second delay
void lcd_data(char display_data);
// Function Prototype to send information on LCD display
void lcd_int();
// Function Prototype to initilization of Basics LCD Commands
void lcd_command(char command);
// Function Prototype to send commands on LCD Port
/* .................Begining of Main Function .....................*/
void main()
{
inti=0;
int j=0;
int k=0;
int l=0;
int check = 1;
unsigned char a[9]="EMBEDDED";
unsigned char b[8]="DESIGN";
unsigned char c[6]="THANK";
unsigned char d[4]="YOU";
button =1;
lcd_int();
// LCD Initilization
// button = 1;
while( a[i] != '\0' )
{
lcd_data(a[i]);
i++;
delay_ms(50);
}
lcd_command(0xC0); // Command to Move Cursor to next line
while(b[j] != '\0')
{
lcd_data(b[j]);
j++;
delay_ms(50);
}
lcd_command(0x0C); // Command to set Display ON Curser OFF
//delay_ms(2000); // Delay for 2sec
while(check==1)
{
if(button == 0)
{
lcd_command(0x01); // Command To clear screen
check = 0;
}
}
while(c[k]!='\0')
{
lcd_data(c[k]);
k++;
delay_ms(50);
}
delay_ms(20);
lcd_command(0xC0); // Move cursor to nest line
while(d[l]!='\0')
{
lcd_data(d[l]);
l++;
delay_ms(50);
}
while(1);
}
/*................. END of Main Function .........................*/
/*................. Delay Sub Routile............................*/
void delay_ms(int d)
{
inti,j;
for(i=0; i<d; i++)
for(j=0; j<1257; j++);
}
/*.....Sub Routine to send information over LCD...............*/
void lcd_data(char display_data)
{
Display_Port = display_data;
RS = 1;
EN = 1;
delay_ms(1);
EN = 0;
}
/*....... Sub Routine to send Commands over LCD Display.............*/
void lcd_command(char command)
{
Display_Port = command;
RS = 0;
EN = 1;
delay_ms(1);
EN = 0;
}
/*........... Sub Routine to initilize the Commands............*/
void lcd_int()
{
lcd_command(0x38);
delay_ms(10);
lcd_command(0x0F);
delay_ms(10);
lcd_command(0x01);
delay_ms(10);
lcd_command(0x80);
delay_ms(10);
}