Categories
8051-8052 Assembly Language

8051 Assembly Code for LCD Display in 8-bit mode

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.

Components List

Here is the complete list of components you required to build a circuit with 8051 microcontrollers for lcd display interfacing in 8-bit mode.

  1. 8051 Microcontroller (e.g., AT89S51 or AT89S52)
  2. 16×2 LCD Display (HD44780-based)
  3. 11.0592 MHz Crystal Oscillator
  4. Reset Circuit Components:
    • 10kΩ Resistor
    • 10µF Capacitor
    • 10k Variable Resistor
    • Reset Switch (Optional)
  5. 7805 Voltage Regulator
  6. Capacitors for Power Supply Stability:
    • 0.1µF Capacitor
    • 10µF Capacitor
    • 33pf Capacitors on Crystal
  7. 10K Resistor Pack (for data lines D0-D7 on P0 of the 8051 microcontroller)
  8. Jumper Wires
  9. Breadboard or PCB
  10. Connecting Wires
  11. Optional: Current Limiting Resistors (for backlighting LEDs)

Wiring Instructions

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.

Block Diagram for lcd display driver

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.

Pin Functions Signal/Device Function No. of Lines I/O LCD Pin Number
RS MPU Selects registers. 0: Instruction register (for write) Busy flag:
address counter (for read)
1: Data register (for write and read)
1 I 4
RW MPU Selects read or write. 0: Write
1: Read
1 I 5
EN MPU Starts data read/write. 1 I 6
D0-D7 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. 8 I/O 7-14
VCC Power supply Power supply for LCD drive 1 2
GND Power supply Ground 1 1
Contrast Adjust Adjusts contrast of the LCD Adjusts the contrast of the LCD display 1 I N/A

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
Stack Overflow

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 ==================================

 ACALL DELAY2
 CLR EN
 
 MOV A,#FUNC_SET
 ACALL LCD_COM
 
 MOV A,#DISP_CONT
 ACALL LCD_COM
 
 MOV A,#DISP_CLR
 ACALL LCD_COM
 
 MOV A,#'F'
 ACALL LCD_DATA
 
 SJMP $

;======== LCD_COM ==================================

LCD_COM: CLR RS  
 MOV LCDDATA,A
 SETB EN
 NOP 
 CLR  EN
 ACALL DELAY2
 RET
 
;======== LCD_DATA =================================

LCD_DATA: SETB RS  
 MOV LCDDATA,A  
 SETB EN
 NOP 
 CLR EN
 ACALL DELAY2
 RET 

;======== DELAY2 ================================
  
DELAY2: MOV R6,#25
D2L2: MOV R7,#255
D2L1: DJNZ R7,D2L1
 DJNZ R6,D2L2
 RET
 
;================================================
Code language: ARM Assembly (armasm)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.