LCD interfacing 8051 Microcontrollers 4-bit

August 16, 2009 by admin · 3 Comments
Filed under: Microcontrollers 

Interfacing Standard Alphanumeric LCD using 4 bit mode in 8051 MCU

flag

In this article i will explain how to interface a standard 16×2 or 20×4 alphanumeric LCD to 8051 microcontrollers using 4 bit data mode. “LCD.h” file is available for free download from here.

LCD is the most common display device used by the designer for testing purpose and for displaying real time or stored data. It is very easy to drive a LCD module using a microcontroller.

Table below shows the pin details of LCD (16 pins)

lcd pins electroons.com

Circuit Diagram shows Connection of LCD to any PORT of Microcontroller.

8051lcd

While using LCD in 4 bit data mode it saves 4 bits of our total GPIO lines, that’s why it is most commonly used. MSB of any data or commnad is sent first over 4 bits and then 4 LSB sent by shifting the data byte 4 times left.

Below is the code which is compiled usiong Keil uVision3 Compiler.

/****************************************************************

File name : LCD.H for driving LCD in 4bit mode

Auther: Anupam Dubey (dubey.anupam@gmail.com)

*****************************************************************/

#define LCDPORT P2

#define RS P2_0

#define RW P2_1

#define E  P2_2

bit  status=0
#define lcd_delay 60

void delay(unsigned int j)

{

unsigned int i,k;

for(i=0;i<j;i++)

{

for(k=0;k<100;k++);

}

}

void _lcd_init_write(unsigned char a)

{

RS = 0;

RW = 0;

LCDPORT=a;

E=1;

delay(lcd_delay);

E=0;

}

void lcd_com(unsigned char a){

unsigned char temp;

if(status){

status=0;

 goto a;

 }

RS=0;

a:

RW=0;

temp=a;

temp&=0xF0;

LCDPORT&=0x0F;

LCDPORT|=temp;

E=1;

delay(lcd_delay);

E=0;

temp=a<<4;

temp&=0xF0;

LCDPORT&=0x0F;

LCDPORT|=temp;

E=1;

delay(lcd_delay);

E=0;

}

void lcd_data(unsigned char a){

status=1;

RS=1;

lcd_com(a);

}

void lcd_init(void){

delay(lcd_delay);

_lcd_init_write(0x30);

delay(lcd_delay);

_lcd_init_write(0x30);

delay(lcd_delay);

_lcd_init_write(0x30);

delay(lcd_delay);

_lcd_init_write(0x20);

delay(lcd_delay);

lcd_com(0x28);

delay(lcd_delay);

lcd_com(4);

delay(lcd_delay);

lcd_com(0x85);

delay(lcd_delay);

lcd_com(6);

delay(lcd_delay);

lcd_com(1);

delay(lcd_delay);

}

void lcd_puts(char *aaa)

{

unsigned int i=0;

for(;aaa[i]!=0;i++)lcd_data(aaa[i]);

}

#define LCDPORT P2
#define RS P2_0
#define RW P2_1
#define E  P2_2
bit  status=0;
#define lcd_delay 60
void delay(unsigned int j)
{
unsigned int i,k;
for(i=0;i<j;i++)
{
for(k=0;k<100;k++);
}
}
void _lcd_init_write(unsigned char a)
{
RS = 0;
RW = 0;
LCDPORT=a;
E=1;
delay(lcd_delay);
E=0;
}
void lcd_com(unsigned char a){
unsigned char temp;
if(status){
status=0;
goto a;
}
RS=0;
a:
RW=0;
temp=a;
temp&=0xF0;
LCDPORT&=0x0F;
LCDPORT|=temp;
E=1;
delay(lcd_delay);
E=0;
temp=a<<4;
temp&=0xF0;
LCDPORT&=0x0F;
LCDPORT|=temp;
E=1;
delay(lcd_delay);
E=0;
}
void lcd_data(unsigned char a){
status=1;
RS=1;
lcd_com(a);
}
void lcd_init(void){
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x30);
delay(lcd_delay);
_lcd_init_write(0x20);
delay(lcd_delay);
lcd_com(0x28);
delay(lcd_delay);
lcd_com(4);
delay(lcd_delay);
lcd_com(0x85);
delay(lcd_delay);
lcd_com(6);
delay(lcd_delay);
lcd_com(1);
delay(lcd_delay);
}
void lcd_puts(char *aaa)
{
unsigned int i=0;
for(;aaa[i]!=0;i++)lcd_data(aaa[i]);
}

Download lcd.h Here