Introduction
Hi guys, in this article we will see how to interface LCD with 8051.
Components Required
- 8051 Microcontroller – 1
- 8051 Program Dumper- 1
- LCD – 1
- 10k Pot Resistor – 1
- Resistor 10k ohm – 1
- Breadboard – 1
- Jumper wires
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.
A 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
- 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 LM016L 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×7 dot resolution.
Pin Description
Pin No | Function | Name |
1 | Ground (0V) | Ground |
2 | Supply voltage; 5V (4.7V – 5.3V) | Vcc |
3 | Contrast adjustment; through a variable resistor | VEE |
4 | Selects command register when low; and data register when high | Register Select(RS) |
5 | Low to write to the register; High to read from the register | Read/write |
6 | Sends data to data pins when a high to low pulse is given | Enable |
7 | 8-bit data pins | DB0 |
8 | “ | DB1 |
9 | “ | DB2 |
10 | “ | DB3 |
11 | “ | DB4 |
12 | “ | DB5 |
13 | “ | DB6 |
14 | “ | DB7 |
13 | Anode | A |
14 | Cathode | K |
- 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 (VCC), 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.
- LM016L 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.
- Enable 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.
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 |
02 | Return home |
04 | Decrement cursor |
06 | Increment cursor |
0E | Display ON ,Cursor blinking OFF |
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 |
08 | Display OFF, Cursor OFF |
C1 | Jump to second line, position1 |
OC | Display ON, Cursor OFF |
C1 | Jump to second line, position1 |
C2 | Jump 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.
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.
Circuit diagram
Interfacing 16×2 LCD module to 8051
- The circuit diagram given above shows how to interface a 16×2 LCD module with AT89S1 microcontroller.
- Capacitor C3, resistor R1 and push button switch S1 forms the reset circuitry.
- Ceramic capacitors C1,C2 and crystal X1 is related to the clock circuitry which produces the system clock frequency.
- P2.0 to P2.7 pins of the microcontroller is connected to the DB0 to DB7 pins of the module respectively and through this route the data goes to the LCD module.
- P0.5, P0.6 and P0.7 are connected to the RS, R/W, E pins of the microcontroller and through this route the control signals are transffered to the LCD module.
- R2,R3,R4 are pull up resistors connected to P0.5, P0.6,P0.7.
- POT R5 is used for adjusting the contrast of the display.
Procedure for programming LCD
- Select the I/O Pins Required to interface LCD (RS, RW, EN, D0, D1, D2, D3, D4, D5, D6, and D7) to connect to the port.
- Write a function to apply command to LCD. By making RS Low, RW Low and EN High to Low. First Apply the Higher Nibble data and then apply lower nibble data to the 8bit data lines.
- Write a function to apply data to LCD. By making RS High, RW Low and EN High to Low. First Apply the Higher Nibble data and then apply lower nibble data to the 8bit data lines.
- To initialize the LCD in 8 bit mode we have to apply a series of commands (0X02, 0X28, 0X01, 0X06, 0X0C, 0X80).
- Write a function to apply string to LCD by applying series of characters.
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