Friday 5 October 2012

How to interface 16x2 LCD with 8051 microcontroller

How to interface 16x2 LCD with 8051 microcontroller


A 16x2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5x7 pixel matrix. This LCD has two registers.



1.      Command/Instruction Register- stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing, clearing the screen, setting the cursor position, controlling display etc.

 2.      Data Register- stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.


Programming the LCD:

1.      Data pin8 (DB7) of the LCD is busy flag and is read when R/W = 1 & RS = 0. When busy flag=1, it means that LCD is not ready to accept data since it is busy with the internal operations. Therefore before passing any data to LCD, its command register should be read and busy flag should be checked.

2.      To send data on the LCD, data is first written to the data pins with R/W = 0 (to specify the write operation) and RS = 1 (to select the data register). A high to low pulse is given at EN pin when data is sent. Each write operation is performed on the positive edge of the Enable signal.

3.      To send a command on the LCD, a particular command is first specified to the data pins with R/W = 0 (to specify the write operation) and RS = 0 (to select the command register). A high to low pulse is given at EN pin when data is sent.


Displaying single character ‘A’ on LCD

 This program uses the above concepts of interfacing the LCD with controller by displaying the character ‘A’ on it.


#include <REGX51.H>
#define data P3
sbit rs=P2^0;
sbit rw=P2^1;
sbit e=P2^2;

void delay(int d);
void lcommand(unsigned char x);
void ldata(unsigned char x);

void main()
{
lcommand(0x38);
delay(50);
lcommand(0x01);
delay(50);
lcommand(0x06);
delay(50);
lcommand(0x0e);
delay(50);
 lcommand(0x86);
delay(50);

ldata('A');
delay(50);

while(1);
}
void delay(int d)
{

int i,j;
for(i=0;i<d;i++)
for(j=0;j<1000;j++);
}
void lcommand(unsigned char x)
{
data=x;
rs=0;
rw=0;
e=1;
delay(1);
e=0;

}
void ldata(unsigned char x)
{
data=x;
rs=1;
rw=0;
e=1;
delay(1);
e=0;

}

Proteus Simulation:










No comments:

Post a Comment