LCD Interfacing
LCD Interfacing
LCD Interfacing
Introduction
The most commonly used Character based LCDs are based on Hitachi's
HD44780 controller or other which are compatible with HD44580. In this
system we will discuss about character based LCDs, their interfacing with
various microcontrollers, various interfaces (8-bit/4-bit), programming, special
stuff and tricks you can do with these simple looking LCDs which can give a
new look to your application.
Pin Description
The most commonly used LCDs found in the market today are 1 Line, 2 Line or
4 Line LCDs which have only 1 controller and support at most of 80
characters, whereas LCDs supporting more than 80 characters make use of
2HD44780-controllers.
Most LCDs with 1 controller has 14 Pins and LCDs with 2 controller has 16
Pins (two pins are extra in both for back-light LED connections). Pin
description is shown in the table below.
Fig :Character LCD type HD44780 Pin diagram
Only the instruction register (IR) and the data register (DR) of the LCD can be
controlled by the MCU. Before starting the internal operation of the LCD,
control information is temporarily stored into these registers to allow interfacing
with various MCUs, which operate at different speeds, or various peripheral
control devices. The internal operation of the LCD is determined by signals sent
from the MCU. These signals, which include register selection signal (RS),
read/write signal (R/W), and the data bus (DB0 to DB7), make up the LCD
instructions.
Display data RAM (DDRAM) stores display data represented in 8-bit character
codes. Its extended capacity is 80 X 8 bits, or 80 characters. The area in
display data RAM (DDRAM) that is not used for display can be used as general
data RAM. So whatever you send on the DDRAM is actually displayed on the
LCD. For LCDs like 1x16, only 16 characters are visible, so whatever you write
after 16 chars is written in DDRAM but is no visible to the user.
Figures below will show you the DDRAM addresses of 1 Line, 2 Line and 4
Line LCDs
As clear from the name, CGRAM area is used to create custom characters in
LCD. In the character generator RAM, the user can rewrite character patterns by
program. For 5 x 8 dots, eight character patterns can be written, and for 5 x 10
dots, four character patterns can be written. BF-Busy- Flag.
Busy Flag is an status indicator flag for LCD. When we send a command or
data to the LCD for processing, this flag is set (i.e. BF =1) and as soon as the
instruction is executed successfully this flag is cleared (BF = 0). This is helpful
in producing an exact amount of delay for the LCD processing.
As we can see in both the code maps, the character code from 0x00 to 0x07 is
occupied by the CGRAM characters or the user defined characters. If user
wants to display the fourth custom character then the code to display it is 0x03
i.e. when user sends 0x03 code to the LCD DDRAM then the fourth user
created character or pattern will be displayed on the LCD.
BF-Busy-Flag
Busy Flag is a status indicator flag for LCD. When we send a command or data
to the LCD for processing, this flag is set (i.e BF =1) and as soon as the
instruction is executed successfully this flag is cleared (BF = 0). This is helpful
in producing and exact amount of delay for the LCD processing.
To read Busy Flag, the condition RS = 0 and R/W = 1 must be met and The
MSB of the LCD data bus (D7) act as busy flag. When BF = 1 means LCD is
busy and will not accept next command or data and BF = 0 means LCD is ready
for the next command or data to the process.
Figure: LCD characters code map for 5x8 dots
There are two 8-bit registers in HD44780 controller Instruction and Data
register. Instruction register corresponds to the register where you send
commands to LCD e.g LCD shift command, LCD clear, LCD address etc. and
Data register is used for storing data which is to be displayed on LCD. When
send the enable signal of the LCD is asserted, the data on the pins is latched in
to the data register and data is then moved automatically to the DDRAM and
hence is displayed on the LCD.
Data Register is not only used for sending data to DDRAM but also for
CGRAM, the address where you want to send the data, is decided by the
instruction you send to LCD.
LCD-Initialization
Before using the LCD for display purpose, LCD has to be initialized either by
the internal reset circuit or sending set of commands to initialize the LCD. It is
the user who has to decide whether an LCD has to be initialized by instructions
or by internal reset circuit.
Display clear
Function set:
As mentioned in the Note, there is certain condition that has to be met, if user
wants to use initialization by internal reset circuit. These conditions are shown
in the Table 5 below.
Figure shows the test conditions which are to be met for internal reset circuit to
be active.
Now the problem with the internal reset circuit is, it is highly dependent on
power supply, to meet this critical power supply conditions is not hard but are
difficult to achieve when you are making a simple application. So usually the
second method i.e. Initialization by instruction is used and is recommended
most of the time.
Figure 7: Internal Power Supply reset
Initialization by Instructions
Initializing LCD with instructions is really simple. Given below is a flowchart
that describes the step to follow, to initialize the LCD.
As you can see from the flow chart, the LCD is initialized in the following
sequence...
1) Send command 0x30 - Using 8-bit interface
2) Delay 20ms
3) Send command 0x30 - 8-bit interface
4) Delay 20ms
5) Send command 0x30 - 8-bit interface
6) Delay 20ms
7) Send Function set - see Table 4 for more information
8) Display Clear command
9) Set entry mode command - explained below
Figure 8: Flow chart for LCD initialization
The first 3 commands are usually not required but are recommended when you
are using 4-bit interface. So you can program the LCD starting from step 7
when working with 8-bit interface. Function set command depends on what
kind of LCD you are using and what kind of interface you are using (see Table
4 in LCD Command section).
Introduction
Till now whatever we discussed in the previous part of this LCD tutorial, we
were dealing with 8-bit mode. Now we are going to learn how to use LCD in 4-
bit mode. There are many reasons why sometime we prefer to use LCD in 4-bit
mode instead of 8-bit. One basic reason is lesser number of pins are needed to
interface LCD.
In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then
the lower nibble. To enable the 4-bit mode of LCD, we need to follow special
sequence of initialization that tells the LCD controller that user has selected 4-
bit mode of operation. We call this special sequence as resetting the LCD.
Following is the reset sequence of LCD.
Above is the connection diagram of LCD in 4-bit mode, where we only need 6
pins to interface an LCD. D4-D7 are the data pins connection and Enable and
Register select are for LCD control pins. We are not using Read/Write (RW)
Pin of the LCD, as we are only writing on the LCD so we have made it
grounded permanently. If you want to use it.. then you may connect it on your
controller but that will only increase another pin and does not make any big
difference. Potentiometer RV1 is used to control the LCD contrast. The
unwanted data pins of LCD i.e. D0-D3 are connected to ground.
Sending Data/Command in 4-Bit Mode
We will now look into the common steps to send data/command to LCD when
working in 4-bit mode. As i already explained in 4-bit mode data is sent nibble
by nibble, first we send higher nibble and then lower nibble. This means in both
command and data sending function we need to separate the higher 4-bits and
lower 4-bits.