The most crucial output components in embedded projects and electronic goods are displays. One of the most popular display units is the 16x2 LCD. A 16x2 LCD has two rows and can show up to 16 characters per line; each character occupies 5X7 of the matrix area on the LCD. In this tutorial, the 16X2 LCD module will be connected to the 8051 microprocessors (AT89S52). For beginners, connecting an LCD to an 8051 microcontroller could seem rather difficult, but once you understand the concept, it becomes clear that it is really pretty simple. A 16-pin LCD must be understood and connected to the microcontroller, which might take some time. Let's first examine the LCD module's 16 pins.
Power Pins, contrast Pins, Control Pins, Data Pins, and Backlight Pins are the five categories into which we may split it.
TERM | Pin No | Pin Name | Function |
Power Pins | 1 | VSS | Ground pin which is connected to Ground |
2 | VDD or Vcc | Voltage Pin of +5V | |
Contrast Pin | 3 | V0 or VEE | connected to Vcc through a variable resistor. |
Control Pins | 4 | RS | Register Select Pin, For, RS=0 Command mode, RS=1 Data mode |
5 | RW | Read/ Write pin, RW=0 Write mode, RW=1 Read mode | |
6 | Enable | a high pulse to low pulse needs to enable the LCD | |
Data Pins | 7-14 | D0-D7 | Stores the Data to be displayed on LCD or the command instructions |
Backlight Pins | 15 | LED+ or A | To power the Backlight +5V |
16 | LED- or K | Backlight Ground |
All the pins are clearly understandable by their name and functions, except the control pins, so they are explained below:
RS: RS is the register select pin. We need to set it to 1, if we are sending some data to be displayed on LCD. And we will set it to 0 if we are sending some command instruction like clear the screen (hex code 01).
RW: This is Read/write pin, we will set it to 0, if we are going to write some data on LCD. And set it to 1, if we are reading from LCD module. Generally, this is set to 0, because we do not have need to read data from LCD. Only one instruction “Get LCD status”, need to be read sometimes.
ENABLE: This pin is used to enable the module when a high to low pulse is given to it. A pulse of 450 ns should be given. That transition from HIGH to LOW makes the module ENABLE.
There are some preset command instructions in LCD, we have used them in our program below to prepare the LCD (in lcd_init() function). Some important command instructions are given below:
Hex Code | Command to LCD Instruction Register |
0F | LCD ON, cursor ON |
01 | Clear display screen |
02 | Return home |
04 | Decrement cursor (shift cursor to left) |
06 | Increment cursor (shift cursor to right) |
05 | Shift display right |
07 | Shift display left |
0E | Display ON, cursor blinking |
80 | Force cursor to beginning of first line |
C0 | Force cursor to beginning of second line |
38 | 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, position 1 |
OC | Display ON, cursor OFF |
C1 | Jump to second line, position 1 |
C2 | Jump to second line, position 2 |
Circuit diagram for LCD interfacing with 8051 microcontroller is shown in the above figure. If you have basic understanding of 8051 then you must know about EA(PIN 31), XTAL1 & XTAL2, RST pin(PIN 9), Vcc and Ground Pin of 8051 microcontroller. I have used these Pins in above circuit. If you don’t have any idea about that then I recommend you to read this Article LED Interfacing with 8051 Microcontroller before going through LCD interfacing.
So besides these above pins we have connected the data pins (D0-D7) of LCD to the Port 2 (P2_0 – P2_7) microcontroller. And control pins RS, RW and E to the pin 12,13,14 (pin 2,3,4 of port 3) of microcontroller respectively.
PIN 2(VDD) and PIN 15(Backlight supply) of LCD are connected to voltage (5v), and PIN 1 (VSS) and PIN 16(Backlight ground) are connected to ground.
Pin 3(V0) is connected to voltage (Vcc) through a variable resistor of 10k to adjust the contrast of LCD. Middle leg of the variable resistor is connected to PIN 3 and other two legs are connected to voltage supply and Ground.