MEVIHUB

How to Interface 16*2/16*4 LCD with 8051

Introduction

Hi guys, in this article we will see how to interface LCD with 8051.

Components Required

LCD

LCD (Liquid Crystal Display) screen is an electronic display module and find a wide range of applications. A 16×2 LCD display is very basic module and is very commonly used in various devices and circuits. These modules are preferred over seven segments and other multi segment LEDs. The reasons being: LCDs are economical; easily programmable; have no limitation of displaying special & even custom characters (unlike in seven segments), animations and so on.

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. This LCD has two registers, namely, Command and Data.

The command register stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initializing it, clearing its screen, setting the cursor position, controlling display etc. The 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. Click to learn more about internal structure of a LCD.

PIN Diagram

16×2 LCD module

Pin Description

 Pin No Function Name
1Ground (0V)Ground
2Supply voltage; 5V (4.7V – 5.3V) Vcc
3Contrast adjustment; through a variable resistor VEE
4Selects command register when low; and data register when highRegister Select(RS)
5Low to write to the register; High to read from the registerRead/write
6Sends data to data pins when a high to low pulse is givenEnable
78-bit data pinsDB0
8DB1
9DB2
10DB3
11DB4
12DB5
13DB6
14DB7
13AnodeA
14CathodeK

16×2 LCD module commands

16×2 LCD module has a set of preset command instructions. Each command will make the module to do a particular task. The commonly used commands and their function are given in  the  table below.

Command                      Function
0FLCD ON, Cursor ON, Cursor blinking ON
01Clear screen
02Return home
04Decrement cursor
06Increment cursor
0EDisplay ON ,Cursor blinking OFF
80Force cursor to the beginning of  1st line
C0Force cursor to the beginning of 2nd line
38Use 2 lines and 5×7 matrix
83Cursor line 1 position 3
3CActivate second line
08Display OFF, Cursor OFF
C1Jump to second line, position1
OCDisplay ON, Cursor OFF
C1Jump to second line, position1
C2Jump to second line, position2

LCD initialization

The steps that has to be done for initializing the LCD display is given below and these steps are common for almost all applications.

Sending data to the LCD

The steps for sending data to the LCD module is given below. I have already said that the LCD module has pins namely RS, R/W and E. It is the logic state of these pins that make the module to determine whether a given data input  is a command or data to be displayed.

Circuit diagram

Interfacing 16×2 LCD module to 8051

 Procedure for programming LCD

In main function first initialize the LCD By Using the Commands and then apply your data to LCD.

CODE


#include <reg51.h>

sbit RS=P3^0;
sbit RW=P3^1;
sbit EN=P3^2;

sfr LCD=0XA0;

// delay function 
void Delay(unsigned int t)
{
unsigned int i;
while(t--)
for(i=0;i<1257;i++);
}

// lcd cmd lines

void lcd_cmd(unsigned char ch)
{
LCD=ch;
RS=0;
RW=0;
EN=1;
Delay(3);
EN=0;
}

// lcd data commands 

void lcd_data(unsigned char ch)
{
LCD=ch;
RS=1;
RW=0;
EN=1;
Delay(3);
EN=0;
}


// lcd init fuction

void lcd_init()
{
lcd_cmd(0X38);//Use 2 lines and 5×7 matrix
lcd_cmd(0X06);//Increment cursor
lcd_cmd(0X0C);//Display ON ,Cursor blinking OFF
lcd_cmd(0X01);//Clear screen
lcd_cmd(0X80);//start display from first line first charcater
}
 
// lcd print 

void lcd_print(unsigned char *str)
{
while(*str)
{
lcd_data(*str);
str++;	}
}

main()
{
	while(1)
	{
	lcd_init();
	lcd_cmd(0X80);//Cursor line 1 position 4
	lcd_print("WelCome embedded");
	lcd_cmd(0XC0);//For  2nd line
	lcd_print("LCD INTERFACING");
	lcd_cmd(0X94);//for third line
	lcd_print("well come");
	lcd_cmd(0XD4);//for fourth line
	lcd_print("DEVIL ");
	}
}

Thanks for reading this article for any assistance or doubts comment below.

Download Code
Exit mobile version