- LCD display is an inevitable part in almost all embedded projects and this article is about interfacing 16×2 LCD with 8051 micro controller. Many guys find it hard to interface LCD module with the 8051 but the fact is that if you learn it properly, its a very easy job and by knowing it you can easily design embedded projects like digital voltmeter / ammeter, digital clock, home automation displays, status indicator display, digital code locks, digital speedometer/ odometer, display for music players etc etc. In order to understand the interfacing first you have to know about the 16×2 LCD module
- 16×2 LCD module is a very common type of LCD module that is used in 8051 based embedded projects. It consists of 16 rows and 2 columns of 5×7 or 5×8 LCD dot matrices.
- In this LCD each character is displayed in 5x7 pixel matrix.
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 |
| 0F | LCD ON, Cursor ON, Cursor blinking ON |
| 01 | Clear screen |
| 2 | Return home |
| 4 | Decrement cursor |
| 06 | Increment cursor |
| E | Display ON ,Cursor ON |
| 80 | Force cursor to the beginning of 1st line |
| C0 | Force cursor to the beginning of 2nd line |
| 38 | Use 2 lines and 5×7 matrix |
| 83 | Cursor line 1 position 3 |
| 3C | Activate second line |
| 0C3 | Jump to second line, position3 |
| OC1 | Jump to second line, position1 |
- The pin numbers, their name and corresponding functions are shown in the table below.
- VEE pin is meant for adjusting the contrast of the LCD display and the contrast can be adjusted by varying the voltage at this pin. This is done by connecting one end of a POT to the Vcc (5V), other end to the Ground and connecting the center terminal (wiper) of of the POT to the VEE pin.
- This LCD has two registers.1. Command/Instruction Register - stores the command instructions given to the LCD. A command is an instruction given to LCD to do a predefined task like initialising, clearing the screen, setting the cursor position, controlling display etc.2. 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.
- High logic at the RS pin will select the data register and Low logic at the RS pin will select the command register. If we make the RS pin high and the put a data in the 8 bit data line (DB0 to DB7) , the LCD module will recognise it as a data to be displayed . If we make RS pin low and put a data on the data line, the module will recognise it as a command.
- R/W pin is meant for selecting between read and write modes. High level at this pin enables read mode and low level at this pin enables write mode.
- E pin is for enabling the module. A high to low transition at this pin will enable the module.
- DB0 to DB7 are the data pins. The data to be displayed and the command instructions are placed on these pins.
LCD initialisation.
The steps that has to be done for initialising the LCD display is given below and these steps are common for almost all applications.- Send 38H to the 8 bit data line for initialisation
- Send for 0CH making LCD ON, cursor ON and cursor blinking ON.
- Send 06H for incrementing cursor position.
- Send 01H for clearing the display and return the cursor.
- Send 80H for the display of the character from column 0 and on line 1
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.- Make R/W low.
- Make RS=0 if data byte is a command and make RS=1 if the data byte is a data to be displayed.
- Place data byte on the data register.
- Pulse E from high to low.
- we will see how to display "hello world" on LCD.
- THE CIRCUIT DIAGRAM IS GIVEN BELOW:
HERE IS THE CODE FOR THE SAME:-
#include<reg51.h>
//THIS HEADER FILE CONTAINS ALL THE INBUILT LIBRARIES….
sfr port=0x80; //SFR
STANDS FOR SPECIAL FUNCTION REGISTER. HERE WE HAVE // USED PORT-0 . PORT-0 STARTS FROM THE MEMORY
LOCATION 80. //NOTE HERE THE DIFFERENCE HOW WE HAVE WRITTEN 80 IN HEX. I.E // 0X80
WHEREAS IN ASSEMBLY WE USED TO WRITE 80H. TO SEE ALL THE // MEMORY LOCATIONS OF
ALL PORT JUST DOUBLE CLICK ON THE // REG51 FILE YOU HAVE ADDED TO YOUR GROUP.
sbit rs=P2^0;
//SBIT IS A KEYWORD TO SELECT A SINGLE BIT.HERE WE HAVE //CONNECTED THE R/S PIN OF THE LCD WITH THE
P2^0
sbit rw=P2^1; //
R/W FOR READ OR WRITE
sbit en=P2^2; //
ENALE .
void delay(unsigned int t); //DELAY FUNCTION DECLARATION WITH AN
UNSIGNED INT //ARGUMENT.
void lcdcmd(unsigned char value); //LCDCMD FUNCTION
DECLARATION WITH UNSINGED // CHAR ARGUMENT TO LOAD INSTRUCTIONS INTO THE //COMMAND
REGISTERS OF THE LCD
void lcddata(unsigned char value1); // LCDDATA FUNCTION DECLARATION WITH
UNSINGED //CHAR ARGUMENT TO LOAD INSTRUCTIONS INTO THE //DATA REGISTERS OF THE
LCD.THE CHARACTER PASSED AS A ARGUMENT WILL BE DISPLAYED ON LCD
void main() //ITS HERE WHERE THE PROGRAMS START.
{ lcdcmd(0x38); //TO INITIALISE THE 16x2 LCD WITH 5x7
MATRIX.
delay(20); //WAIT FOR 20 mSec
lcdcmd(0x0C); //DISPLAY ON ,CURSOR OFF
delay(20); //AGAIN WAIT
lcdcmd(0x01); //CLEAR THE SCREEN
delay(20);
lcdcmd(0x06); //INCREATMENT CURSOR (SHIFT CURSOT TO RIGHT)
delay(25);
lcdcmd(0x80); //FORCE THE DISPLAY OF THE CHARACTER IN FIRST
LINE AND ZERO BLOCK.
delay(25);
lcddata('H'); //LOADS
CHARACTER ‘H’ ON PORT-0 AND THE SAME WILL BE DISPLAYED
delay(25); //WAIT FOR FEW MILLISECONDS
lcddata('E'); //LOADS
CHARACTER ‘E’ ON PORT-0 AND THE SAME WILL BE // DISPLAYED BUT IN THE SUBSEQUENT BLOCK. ALL THE LETTERS // ARE
DISPLAYED IN THE SAME MANNER.
delay(25);
lcddata('L');
delay(25);
lcddata('L');
delay(25);
lcddata('0');
delay(25);
lcddata('
');
delay(25);
lcddata('W');
delay(25);
lcddata('0');
delay(25);
lcddata('R');
delay(25);
lcddata('L');
delay(25);
lcddata('D');
delay(25);
}
void
lcdcmd(unsigned char value)
//LCDCMD DUNCTION DEFINATION
{
port=value; //LOADS
THE ARGUMENT ON PORT-0
rs=0; //R/S PIN LOW FOR WRITING INTO
COMMAND // REGISTERS .
rw=0; //R/W PIN LOW .THIS IS BECAUSE WE
ARE USING LCD IN WRITING MODE
en=1; //A HIGH
–TO-LOW PULSE MUST BE GIVEN TO ENABLE.
delay(1);
en=0;
return; //RETURNS
THE OUTPUT OF THE FUNCTION
}
void
lcddata(unsigned char value1) //LCDDATA FUNCTION DEFINATION
{
port=value1; //LOADS THE ARGUMENT ON PORT-0
rs=1; //R/S PIN HIGH FOR WRITING INTO DATA
REGISTERS .
rw=0; //R/W PIN LOW .THIS IS BECAUSE WE ARE
USING LCD IN WRITING MODE
en=1; ; //A HIGH
–TO-LOW PULSE MUST BE GIVEN TO ENABLE.
delay(1);
en=0;
return; //RETURNS THE
OUTPUT OF THE FUNCTION
}
void delay(unsigned int t) //DELAY FUNCTION DEFINATION
{
int
i,j;
//INTIALISATION
for(i=0;i<=t;i++) //THE LOOP WILL EXECUTE TILL i IS
LESS THAN t. HERE t IS THE //VALUE GIVEN BY US
for(j=0;j<=1275;j++);
//FOR EVERY I THIS LOOP WILL BE EXECUTED
FOR 1275 TIMES.SO //ULTIMATELY THE DELAY WOULD BE ix1275.
}
