- 8051

Interfacing LCD with 8051 using Keil C – AT89C51

 LCD Interfacing-8051 AT89C51xD2
Introduction:
 
 

16*2 Alphanumeric display with backlight is provided on Evaluation Board.

Liquid Crystal Display (LCD) is widely used electronic display module and having a wide range of applications such as calculators, laptops, mobile phones etc.
 
LCD in ALS 8051 Evaluation Board is connected to Port P2. Before we initialise the LCD Let us understand some basic concepts of LCD.
 

1)LCD Pin Descriptions:

Normally LCD has 16 pins. The function of each pin is discussed below in the table.
 
Table 1.1 Pin description of LCD
 
 
2) RS (Register Select):
 
The RS pin is used to select command code register or data register. If RS=0 the command code register is selected which allows us to send the instructions to LCD. If RS=1 the data register is selected which allows us to send data to be displayed o LCD.
 
3) RW(Read/Write):
 
R/W input allows the user to write information to the LCD or read info from it. 
R/W =1 when reading
R/W =0 when writing
 
4) EN (Enable):
when data or command is sent to LCD a high-to-low pulse must applied to the PIN. So Enable pin is toggled by sending first 1 and then 0.
 
5) LCD Commands: 
 LCD Commands sent to LCD are given in table below with description of each command.
Table1.2 LCD Commands

 

 
 Now let us see the Code 
 
CODE :
#include <reg51.h>
#define display_port P2      //Data pins connected to port 2 on microcontroller
void delay(int);
void lcd_comm(void);
void lcd_data(void);
void lcd_init(void);
void lcd_clear(void);


unsigned char temp1,temp2,var,j;
unsigned char *ptr,disp1[]="CodesExplorer",disp2[]="LCD Interfacing";
sbit RS=P2^7;						//RS pin connected to pin 7 of port 2
sbit EN=P2^6;						//E pin connected to pin 6 of port 2
sbit RW=P2^5;						//RW pin connected to pin 5 of port 2

int main()
{
	lcd_init();						//lcd intialisation
	delay(100);
	lcd_clear();					//clear display
	delay(100);
	while(1)
	{
		temp1=0x80;					// Display Starting address of first line
		lcd_comm();
		delay(50000);
		
		ptr=disp1;
		while(*ptr!='\0')		// searching the null terminator in the sentence
		{
			temp2=*ptr;
			lcd_data();
			ptr ++;
			delay(50000);
		}
		
		temp1 = 0xC0;			// Display starting address of second line 1st pos
		lcd_comm();
		delay(50000);
		
		ptr=disp2;
		while(*ptr!='\0')		// searching the null terminator in the sentence
		{
			temp2=*ptr;
			lcd_data();
			ptr ++;
			delay(50000);
		}
		
		temp1=0x01;
		lcd_comm();
		delay(2000);
	}
}

void lcd_init()
{
	unsigned int cmd[]={0x20,0x28,0x0e,0x06,0x80,0x01};
	unsigned int i;
	for(i=0;i<7;i++)
	{
		temp1=cmd[i];
		lcd_comm();
		delay(500);
	}
}

void lcd_comm()			/Function to send command instruction to LCD
{
	var=temp1;
	temp1=temp1&0x0F0;
	temp1=temp1>>4;
	display_port=temp1;
	RS=0;
	EN=1;
	EN=0;
	temp1=var&0x0F;
	display_port=temp1;
	RS=0;
	EN=1;
	EN=0;
	delay(60);
}

void lcd_data()			//Function to send display data to LCD
{
	var=temp2;
	temp2=temp2&0x0F0;
	temp2=temp2>>4;
	display_port=temp2;
	RS=1;
	EN=1;
	EN=0;
	temp2=var&0x0F;
	display_port=temp2;
	RS=1;
	EN=1;
	EN=0;
	delay(600);
}	

void lcd_clear(void)
{
    temp1 = 0x01;
    lcd_comm();
    delay(500);					
}  

void delay(int j)
{
	unsigned int i;
	for(i=0;i<j;i++);
}

Leave a Reply