Monday 19 March 2012

How to interface 16x2 alphanumeric LCD with AT89c51 Microcontroller


In my previous blog I have explained that how a 16x2 alphanumeric LCD works. So in this blog I am going to explain how we can interface 16x2 alphanumeric LCD with microcontroller AT89c51.

Component required for interfacing LCD with microcontroller :
1.      Microcontroller AT9c51
2.      16x2 alphanumeric LCD
3.      12 MHz crystal oscillator
4.      10 KΩ fixed register
5.      10 KΩ variable register
6.      10 μf (25 V) capcitor

Circuit diagram for interfacing LCD with microcontroller :


Procedure to program LCD :
1.      Connect p2.o to p2.7 of AT89c51 to DB0-DB7 of LCD.
2.      Connect p3.0, p3.1 & p3.6 pin of  AT89c51 to RS, R/W & En pin of LCD respectively.
3.      Connect the rest of the circuit as shown in figure.
4.      To send command to LCD we have to clear RS pin of LCD by sending 0 to it. To send data to LCD
we have to set RS pin of  LCD by sending 1 to it.
5.      To write either command or data to LCD by clearing the R/W pin. To read the data from LCD we have   to set the R/W pin.
6.      First we have to clear the LCD by sending 01h command to LCD.
7.      Then we can send any command to perform any function on LCD.
8.      After latching the data into the DB0-DB7 pin of LCD we have to send high to low pulse on En pin of  LCD. 

Code (in c language) for interfacing LCD with microcontroller :


#include<reg51.h>


#define cmd_port P3


#define data_port P2


#define q 100


sbit rs = cmd_port^0;  //register select pin
sbit rw = cmd_port^1;  // read write pin
sbit e = cmd_port^6;  //enable pin

void delay(unsigned int msec)  // Function to provide time delay in msec.
{
int i,j ;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}


void lcd_cmd(unsigned char item)  //Function to send command to LCD
{
data_port = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
}


void lcd_data(unsigned char item)  //Function to send data to LCD
{
data_port = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
}


void main()
{


lcd_cmd(0x38);  // for using 8-bit 2 row mode of LCD
delay(100);


lcd_cmd(0x0E);  // turn display ON for cursor blinking
delay(100);

lcd_cmd(0x01);  //clear screen
delay(100);


lcd_cmd(0x06);  //display ON
delay(100);


lcd_cmd(0x86);  // bring cursor to position 6 of line 1
delay(100);


lcd_data('A');
}

No comments:

Post a Comment