Sunday 22 November 2015

HOW TO INTERFACE LCD USING 8051 MICRO CONTROLLER

         Interfacing 16×2 LCD with 8051  


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. Thoroughly going through this article will make you able to display any text (including the extended characters) on any part of the 16×2 display screen. In order to understand the interfacing first you have to know about the 16×2 LCD module.

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. The module were are talking about here is type number JHD162A which is a very popular one . It is available in a 16 pin package with back light ,contrast adjustment function and each dot matrix has 5×8 dot resolution. The pin numbers, their name and corresponding functions are shown in the table  below.
Pin No:Name Function
1VSSThis pin must be connected to the ground
2VCC Positive supply voltage pin (5V DC)
3VEEContrast adjustment
4RSRegister selection
5R/WRead or write
6E Enable
7DB0 Data
8DB1 Data
9DB2 Data
10DB3 Data
11DB4 Data
12DB5 Data
13DB6 Data
14DB7 Data
15LED+ Back light LED+
16LED- Back light LED-
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. See the circuit diagram for better understanding.
The JIHAD has two built in registers namely data register and command register.  Data register is for placing the data to be displayed , and the command register is to place the commands. The 16×2 LCD module has a set of commands each meant for doing a particular job with the display. We will discuss in detail about the commands later. 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 recognize it as a data to be displayed .  If we make RS pin low and put a data on the data line, the module will recognize 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.
LED+ is the anode of the back light LED and this pin must be connected to Vcc through a suitable series current limiting resistor. LED- is the cathode of the back light LED and this pin must be connected to ground.

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.
  • Send 38H to the 8 bit data line for initialization
  • Send 0FH for 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.



CIRCUIT DIAGRAM


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.
  • Repeat above steps for sending another data

CODING


void delay(unsigned int);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
void lcd_str(unsigned char*);
void lcd_init();
sbit rs=P3^2;
sbit rw=P3^3;
sbit en=P3^4;
void delay(unsigned int i)
  { 
while(i--);
  }
void lcd_cmd(unsigned char a)
 {
  P2=a;
  rs=0;
  rw=0;
 en=1;
  delay(2000);
  en=0;
 }
void lcd_data(unsigned char b)
  { 
    P2=b;
    rs=1;
    rw=0;
en=1;
    delay(2000);
    en=0;
   }
void lcd_init()
   {
  lcd_cmd(0x0E);
lcd_cmd(0x38);
}
void lcd_str(unsigned char *s)
 {

  while(*s)
    {
     lcd_data(*s);
  s++;
     }
  }


No comments:

Post a Comment