Interfacing 16*2 LCD display with 8051 8 bit mode



In this post, I am showing that how to connect and code for interfacing 16*2 LCD display with 8051microcontroller.

first, we take a look at 16*2 LCD (liquid crystal display) display Pin configuration.

LCD data sheet can be downloaded from here HD44780


Image result for 16*2 display datasheet

16*2 LCD display 



The RS pin is connected to Port1 pin 0 and Enable pin with Port 1 pin 1
The 8 Bit Data is connected to Port 2 


C code for LCD display interfaces with 8051.

#include<reg51.h>
//////////// PIN DEFINED ///////////////
#define DATA P2
sbit RS=P1^0;
sbit E=P1^1;
//////////////////////////////////////////
#include"lcd.h"


void main()
{
char i;
init_lcd();
while(1)
{
cmd_lcd(0x80);
string_lcd("LCD DISPLAY");
cmd_lcd(0xc0);
string_lcd("** 8051 ** ");

for(i=0;i<5;i++)
{
cmd_lcd(0x1C);
delay(30000);
}
for(i=0;i<5;i++)
{
cmd_lcd(0x18);
delay(30000);
}
}
}

void delay(int num)   // delay function
{
int a;
for(a=0;a<=num;a++);
}

void cmd_lcd (unsigned char dat)    // function to write command at lcd port
{
DATA=dat;
RS=0;   //clear RS (ie. RS=0) to write command
E=1;   // send  H-L pulse at E pin
delay(100);
E=0;
delay(100);
}

void data_lcd (unsigned char dat) // function to write data at lcd port
{
DATA=dat;
RS=1; // set RS=1 to write data
E=1;     // send  H-L pulse at E pin
delay(100);
E=0;
delay(100);
}

void init_lcd()         // function to initialize the LCD at power on time
{
cmd_lcd(0x38); // 2x16 display select
delay(50000);
cmd_lcd(0x0c); // display on cursor off command
delay(1000);
cmd_lcd(0x06); // automatic cursor movement to right
delay(1000);
cmd_lcd(0x01); // lcd clear command
delay(5000);
}

void string_lcd(unsigned char *str)    // function to display string to lcd
{
while(*str!='\0')   // '\0' is null char as last char of pointer is null
{
data_lcd(*str);
str++;
}
 }