Exp. 05 : LCD Interfacing with 8051 Microcontroller
Introduction:
A Liquid Crystal Display commonly abbreviated as LCD is basically
a display unit built using Liquid Crystal technology.
It is an inevitable part in almost all embedded projects and this
article is about interfacing a 16×2 LCD with 8051 microcontroller.
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 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 LCD modules.
Hardware Discussion: Liquid Crystal Display (LCD)
Fig: Some of available LCD modules
LCD Features :
Alphanumeric LCD display module, meaning can display alphabets and numbers.
16x2 LCD consists of two rows and each row can print 16 characters.
Each character is build by a 5×8 pixel box.
Can work on both 8-bit and 4-bit mode.
It can also display any custom generated characters
Available in Green and Blue Back light.
Combinations available like, 8x1, 8x2, 16x1, 16x2, 16x4, 20x4 etc.
Operating Voltage is 4.7V to 5.3V.
Current consumption is 1mA without back light.
LCD Basics :
16×2 LCD module is a very common type of LCD module that is used in this tutorial. The pin
numbers, their name and corresponding functions are shown in the table below.
Fig: LCD Pinout with description
How it works?
Now, we know that each character has (5×8=40) 40 Pixels and for 32 Characters we will have
(32×40) 1280 Pixels. Further, the LCD should also be instructed about the Position of the
Pixels. Hence it will be a hectic task to handle everything with the help of MCU, hence an
Interface IC like HD44780 is used, which is mounted on the backside of the LCD Module itself.
The function of this IC is to get the Commands and Data from the MCU and process them to
display meaningful information onto our LCD Screen.
A 16×2 LCD has two registers like data register and command register. TheRS(register select)
is mainly used to change from one register to another. Whenthe register set is ‘0’, then it
is known ascommand register. Similarly, when theregister set is ‘1’, then it is known as data
register.
The main function of the command register is to store the instructions of command
which are given to the display. So that predefined tasks can be performed such as clearing
the display, initializing, set the cursor place, and display control.
The main function of the data register is to store the information which is tobe exhibited
on the LCD screen. Here, the ASCII value of the character is theinformation. Whenever we
send the information to LCD, it transmits to the dataregister, and then the process will
be starting there.
Now we can start for the test of a LCD. To do that we need to code for thecommon steps:
Programming LCD to Microcontroller :
Coming to the programming you should follow these steps:
STEP1: Initialization of LCD
STEP2: Sending command to LCD.
STEP3: Writing the data to LCD.
LCD initialization :
To initialize LCD to the 8051 the following instruction and commands are to
beembed in to the functions
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 increment 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 adata 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 : 8 bit LCD Interfacing to 8051.
Fig: A Single Digit Seven Segment Display Circuit.
Circuit Discussion :
The circuit diagram given above shows how to interface a 16×2 LCD module with AT89S1
microcontroller. Capacitor C3, resistor R3 and push button switchS1 forms the reset
circuitry. Ceramic capacitors C1,C2 and crystal X1 is related tothe clock circuitry
which produces the system clock frequency. P1.0 to P1.7 pins ofthe microcontroller
is connected to the DB0 to DB7 pins of the module respectively and through this route
the data goes to the LCD module. P3.3, P3.4 and P3.5 are connected to the E, R/W, RS
pins of the microcontroller and through this route the control signals are transferred
to the LCD module. Resistor R1 limits the current through the back light LED and so
do the back light intensity. POT R2 is used for adjusting the contrast of the display.
Program for interfacing LCD to 8051 micro-controller is shown below.
The microcontroller like 8051 has only limited number of GPIO pins. So to design complex
projects we need sufficient number of I/O pins . An LCD module can be interfaced with a
microcontroller either in 8 bit mode (as seen above) or in 4 bit mode. 8 bit mode is the
conventional mode which uses 8 data lines and RS, R/W,E pins for functioning. However 4
bit mode uses only 4 data lines along with thecontrol pins. This will saves the number
of GPIO pins needed for other purpose. Ifyou already know how to interface LCD with
microcontroller in 8 bit mode then it is not difficult to interface LCD in 4 bit mode.
In 4 bit interfacing mode that we only use 4 data lines [DB4-DB7] of LCD.
As we are going to use only 4 data lines so we need to send data in group of 4
bits( called nibble). The Higher nibble is send first and then lower nibble is send.
In this program i use two memory location to store my upper and lower nibble after
separating a 8 bit data.
Circuit Diagram :
Fig: LCD interfacing (4 bit mode) with 8051
Circuit Discussion :
As shown in the circuit diagram, port 0 of the controller is used for interfacingit with
LCD module. In 4 bit mode only 4 linesD4-D7, along with RS, R/W andE pins are used.
This will save us 4 pins of our controller which we might employ itfor other purpose. Here
we only need to write to the LCD module. So the R/W pincan be ground it as shown in the schematic
diagram. In this way the total numberof pins can be reduced to 6. In 4 Bit mode the data bytes are
split into two fourbits and are transferred in the form of a nibble. The data transmission to a
LCD isperformed by assigning logic states to the control pins RS and E. The reset circuit,oscillator
circuit and power supply need to be provided for the proper working ofthe circuit.
Assembly Program : lcd_4bit_8051.asm
Lesson Video
Exercises
Lab Task :
Use the same circuit and write program for display other characters 8 lines.
Home Task :
Use the same circuit with two push button to scroll your multi line messages on the LCD.
Use the same circuit and write program for some custom character on LCD.