LCD Interfacing with AT89C51

LCD Interfacing with AT89C51 is very common, and usually applied in every engineering project with AT89C51. So, to interface LCD lets look at some basics:

A 16×2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5×7 pixel matrix. See Pin Configuration:

LCD Pinout
LCD Pinout

This LCD has two registers.

  • 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.
  • 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:

  • 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.
  • 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.

For more instructions see this table:

LCD table for functions
LCD table for functions

Now to display simple HELLO we will first simulate the circuit in Proteus or equivalent software:

Circuit Diagram:

Simulation for LCD interface with AT89C51
Simulation for LCD interface with AT89C51

Now to run this circuit you first need to create HEX file using Keil or other equivalent software:

Code:


#include reg51.h

  //LCD
sbit rs = P3^0;        //register select pin
sbit rw = P3^1;        //read write pin
sbit e = P3^2;        //enable pin


void delay(unsigned int time)    //Function to provide time delay in msec.

{

int i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);

}

void lcdcmd(unsigned char item)    //Function to send command to LCD

{
P2 = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;		
return;
}

void lcddata(double item)    //Function to send data to LCD

{

P2 = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
return;
}


void main()

{ 
lcdcmd(0x0E);    //turn display ON for cursor blinking
lcdcmd(0x01);    //clear screen
lcdcmd(0x06);    //display ON

lcddata('H');
lcddata('E');
lcddata('L');
lcddata('L');
lcddata('O');
lcddata('!');

while(1){}			 //Stuck into infinite loop after displaying HELLO! so to avoid blinking

}

Have a Good Luck! running this simple Project! 🙂