Saturday 8 September 2018

8051: LCD Interface Code



Description:

16x2 LCD is one of the most used display unit. 16x2 LCD means that there are two rows in which 16 characters can be displayed per line, and each character takes 5X7 matrix space on LCD. In this program we are going to connect 16X2  LCD module to the 8051 microcontroller. Interfacing LCD with 8051 microcontroller might look quite complex, but after understanding the concept it would look very simple and easy. The 16*2 LCD commands are shown in below:




Code:
#include<reg51.h>

/*-------------------------------------------------------------------------------------------------------------------------*/
sbit rs=P1^4;
sbit en=P1^5;
/*-------------------------------------------------------------------------------------------------------------------------*/
void display(char*);
void cmd(int);
void data1(char);
void lcd();
void delay(int);
/*-------------------------------------------------------------------------------------------------------------------------*/
void main()
{
lcd();
display("Hello");
while(1)
{
}
}
/*-------------------------------------------------------------------------------------------------------------------------*/
void lcd()
{
cmd(0x38);
cmd(0x0e);
cmd(0x01);
cmd(0x06);
cmd(0x80);
}
/*-------------------------------------------------------------------------------------------------------------------------*/
void cmd(int a)
{
P3=a;
rs=0; //RS =0 =>Command mode
en=1;
delay(100);
en=0;
}
/*-------------------------------------------------------------------------------------------------------------------------*/
void display(char*s)
{
while(*s!='\0')
{
data1(*s);
s++;
}
}
/*-------------------------------------------------------------------------------------------------------------------------*/
void data1(char x)
{
P3=x;
rs=1; //RS =1 =>Data mode
en=1;
delay(100);
en=0;
}
/*------------------------------------------------------------------------------------------------------------------------*/
void delay(int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<100;j++);
}
/*-------------------------------------------------------------------------------------------------------------------------*/




Circuit Diagram:




No comments:

Post a Comment