Today we are going to provide the 8051-assembly code for lcd display. The LCD 16×2 is very famous and mostly used for various projects to display the readings. We will be using 16×2 LCD display interfacing with 8051 microcontrollers in assembly language. This is our ongoing series on the 8051-assembly language programming. If you are interested, here is the list of previous articles where we talked about the 8051-assembly language programming.
Previous Related Articles
- 8051 LED Blinking Program
- 8051 Assembly If Else Statement
- 8051 Assembly 8 LED blinking program
- Keypad interfacing with 8051 in Assembly language
- Digital Dice project with 8051 Assembly language
Table of Contents
Here is the complete list of components you required to build a circuit with 8051 microcontrollers for lcd display interfacing in 8-bit mode.
- 8051 Microcontroller (e.g., AT89S51 or AT89S52)
- 16×2 LCD Display (HD44780-based)
- 11.0592 MHz Crystal Oscillator
- Reset Circuit Components:
- 10kΩ Resistor
- 10µF Capacitor
- 10k Variable Resistor
- Reset Switch (Optional)
- 7805 Voltage Regulator
- Capacitors for Power Supply Stability:
- 0.1µF Capacitor
- 10µF Capacitor
- 33pf Capacitors on Crystal
- 10K Resistor Pack (for data lines D0-D7 on P0 of the 8051 microcontroller)
- Jumper Wires
- Breadboard or PCB
- Connecting Wires
- Optional: Current Limiting Resistors (for backlighting LEDs)
Now you need to carefully build the circuit. 10K resistance with 10uf capacitor to build the Power on Reset Circuit for the microcontroller along with the 11.0592Mhz or 12MHz Crystal with 33pf capacitors to make an oscillator circuit on pin 18 and 19 of AT89S51 microcontroller. Make sure to properly ground the 20th pin of the microcontroller and double check that the 5v are applied on 40th pin of the microcontroller as well as 31th pin of the microcontroller. Now attach the P0 with D0-D7 with P0.0 to P0.7 respectively. The choice of Port 0 for driving the data pins is due to their open collector capability and capability to drive the more current. That’s why we must provide 10K pull up resistors to each pin of the P0. We are using 10K Resistor Pack as Pull up manner. Which means the common point would be pulled to +ve volts and each other end to P0 pins. RW pin of the LCD would be kept attached to the ground. RS pin of the LCD will be connected with P2.0 and En pin of the LCD will be connected with P2.1. You can choose other pins of your choice as well. 10K variable resistance will be attached to 3rd pin
Vo of the LCD.
Understanding Hitachi HD44780-compatible character LCDs
We are focusing on Hitachi HD44780-compatible character LCD. Which could be derived in two modes, 8-bit mode, or 4-bit mode. Today we are only going to talk about how to interface this 16×2 lcd with 8051 in 8-bit mode. The 4-bit mode operation is left for another article. If you are interested in 4-bit mode operation, you can go to this 4-bit lcd interfacing with 8051 in assembly language article.
First thing first, because we are going to use HD44780 compatible 16×2 lcd display, you may need to check the datasheet of HD44780 display driver. There is 8-bit parallel data D7-D0, one Enable(En), one Register Select (RS) and one Read/Write (RW) pins available on the LCD due to this display driver. Here is the block diagram from the datasheet of that controller.
The 16×2 lcd pins are explained in one of our previous articles where we explained how to interface the lcd with pic microcontrollers in PIC CCS compilers. Here is quick summary of the pins in tabular form.
Understanding LCD Pins
There are total 16 pins available in a standard JHD16x2 or JHD20x4 or JHD16x4 lcd displays. These 16 pins have 8 pins for Data which is denoted as D0 upto D7. There are 3 control pins which are RS,R/W and Enable pins. There are 2 power supply pins 1st pin is for GND and 2nd pin is for VDD which is attached to positive 5 volts. 3rd pin is used to adjust the LCD contrast, so, 10K potentiometer is used for that purpose. Last two pins are for back light LED which could be used like any normal LED where pin 15 is the anode terminal of the LED and the 16th pin is Cathode Terminal of the LCD which should be connected to ground.
|No. of Lines
|LCD Pin Number
|MPU Selects registers.
|0: Instruction register (for write) Busy flag:
address counter (for read)
1: Data register (for write and read)
|MPU Selects read or write.
|MPU Starts data read/write.
|MPU Bidirectional tristate data bus pins.
|Used for data transfer and receive between the MPU and the HD44780U. DB7 can be used as a busy flag.
|Power supply for LCD drive
|Adjusts contrast of the LCD
|Adjusts the contrast of the LCD display
Why is R/W pin forced GND?
The R/W pin is used to Read and Write the status. If we put this pin to 0 or GND, it will allow to write to the controller. Otherwise, we are only reading from the controller. If we put this to floating and unconnected, this is not good, as it may produce unwanted logic level on that pin of the lcd. If we put this to Logic High which means providing positive voltages on that pin, this will put the lcd controller into the giving the status if the lcd is busy or not. We may use this to read the lcd busy status. But for this tutorial we will keep it simple and simply put this pin attached to the ground or Low logic.
RS Control Pin
The RS stands for Register Select and is used to either select a command register inside the lcd controller, or to Data Register in the controller. Once we put the RS signal in Command mode, we are able to write a command to the LCD. Which means either we can clear the LCD, initialize the LCD, select the operating mode in 8-bit or 4-bit mode etc. Once we are in Data mode, we send the data which would be displayed on the LCD at current cursor position. That’s how we use one RS pin and 8 data pins to display data on LCD or give instructions to the LCD.
Enable Clock Pin
En pin is Enable pin used to create a strobe signal. This signal is used to let the data clocked into the controller. The data is always clocked in on Falling Edge of the signal. which should be hold at least 450ns. While writing data to the lcd there should be proper clock pulse on the
En pin of the LCD. Otherwise, the data would be corrupted or not displayed properly.
Enable Clock Sequence
To let the lcd work properly, the proper clock signal is very important. Although the Hitachi display controller is faster then the 8051 microcontroller, yet we need to properly add delay to wait proper clock signals. Here is the complete clock signal which is properly mentioned in this stack overflow answer. Here is how it is said in that answer.
In the code that generates the E strobe (data is clocked in on the falling edge of the E clock), are you using the correct setup and hold times?
- Enable pulse width (high level, E=1) 450 nsec min
- Write Data Setup time (before E=0) 195 nsec min
- Write Data Hold time (after E=0) 10 nsec min
8051 Assembly Code for LCD display 8-bit
Now it is finally the time for the actual Coding which is implemented in the 8051-assembly language for driving the 16×2 character LCD Display. Here is the complete code.
; LCD INTERFACING IN 8-BIT
LCDDATA EQU P0
RS BIT P2.0
EN BIT P2.1
FUNC_SET EQU 00111000B
DISP_CONT EQU 00001110B
DISP_CLR EQU 00000001B
;======== MAIN ==================================
;======== LCD_COM ==================================
LCD_COM: CLR RS
;======== LCD_DATA =================================
LCD_DATA: SETB RS
;======== DELAY2 ================================
DELAY2: MOV R6,#25
D2L2: MOV R7,#255
D2L1: DJNZ R7,D2L1
Code language: ARM Assembly (armasm)