LCD Interfacing

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

16x2 LCD Display

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

Pin No. Name Description


Pin no. 1 D7 Data bus line 7 (MSB)
Pin no. 2 D6 Data bus line 6
Pin no. 3 D5 Data bus line 5
Pin no. 4 D4 Data bus line 4
Pin no. 5 D3 Data bus line 3
Pin no. 6 D2 Data bus line 2
Pin no. 7 D1 Data bus line 1
Pin no. 8 D0 Data bus line 0 (LSB)

Pin no. 9 EN1 Enable signal for row 0 and 1 (1stcontroller)

0 = Write to LCD module


Pin no. 10 R/W
1 = Read from LCD module
0 = Instruction input
Pin no. 11 RS
1 = Data input

Pin no. 12 VEE Contrast adjust

Pin no. 13 VSS Power supply (GND)

Pin no. 14 VCC Power supply (+5V)

Pin no. 15 EN2 Enable signal for row 2 and 3 (2ndcontroller)

Pin no. 16 NC Not Connected

Table: Character LCD pins with 2 Controller

Instruction Register (IR) And Data Register(DR)


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. We will discuss more on LCD
instruction set further in this tutorial.

Commands and Instruction Set

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.

There are four categories of instructions that:

 Designate LCD functions, such as display format, data length, etc.


 Set internal RAM addresses
 Perform data transfer with internal RAM
 Perform miscellaneous functions

Display Data RAM (DDRAM)

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

Figure: DDRAM Address for 1 Line LCD

Figure: DDRAM Address for 2 Line LCD


Figure: DDRAM Address for 4 Line LCD

CGROM - Character Generator ROM


Now you might be thinking that when you send an ascii value to DDRAM, how
the character is displayed on LCD? so the answer is CGROM. The character
generator ROM generates 5 x 8 dot or 5 x 10 dot character patterns from 8-bit
character codes (see Figure 5 and Figure 6 for more details). It can generate 208
5 x 8 dot character patterns and 32 5 x 10 dot character patterns.
User defined character patterns are also available by mask-programmed
ROM.As you 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 want to display the fourth custom character then the code to display it is
0x03 i.e. when user send 0x03 code to the LCD DDRAM then the fourth user
created character or-patterned will-be displayed on the LCD.
Character Generator RAM (CGRAM)

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

Instruction Register (Ir) and Data Register (Dr)

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.

Initialization by Internal Reset Circuit


An internal reset circuit automatically initializes the HD44780U when the
power is turned on. The following instructions are executed during the
initialization. The busy flag (BF) is kept in the busy state until the initialization
ends (BF = 1). The busy state lasts for 10 ms after VCC rises to 4.5 V.

 Display clear
 Function set:

DL = 1; 8-bit interface data


N = 0; 1-line display
F = 0; 5 x 8 dot character font
 Display on/off control:
D = 0; Display off
C = 0; Cursor off
B = 0; Blinking off
 Entry mode set:
I/D = 1; Increment by 1
S = 0; No shift
Note: If the electrical characteristics conditions listed under the table Power
Supply Conditions Using Internal Reset Circuit are not met, the internal reset
circuit will not operate normally and will fail to initialize the HD44780U. For
such a case, initialization must be performed by the MCU as explained in the
section, Initializing by Instruction.

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.

Table: Power Supply condition for Internal Reset circuit

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).

LCD Entry Mode


From Table 3 in command section, you can see that the two bits decide the entry
mode for LCD, these bits are:
a) I/D - Increment/Decrement bit.
b) S - Display shift.
With these two bits we get four combinations of entry mode which are 0x04,
0x05, 0x06, 0x07. So we get different results with these different entry modes.
Normally entry mode 0x06 is used which is No shift and auto increment.

LCD Interfacing with Microcontrollers 4-Bit Mode

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.

 Wait for about 20mS


 Send the first init value (0x30)
 Wait for about 10mS
 Send second init value (0x30)
 Wait for about 1mS
 Send third init value (0x30)
 Wait for 1mS
 Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)
 Wait for 1mS
The busy flag will only be valid after the above reset sequence. Usually we do
not use busy flag in 4-bit mode as we have to write code for reading two nibbles
from the LCD. Instead we simply put a certain amount of delay usually 300 to
600uS. This delay might vary depending on the LCD you are using, as you
might have a different crystal frequency on which LCD controller is running. So
it actually depends on the LCD module you are using. So if you feel any
problem running the LCD, simply try to increase the delay. This usually works.
For me about 400uS works perfect.

LCD Connections in 4-Bit Mode

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.

Table: Command and Instruction set for LCD type HD44780


Although looking at the table you can make your own commands and test them.
Below is a brief list of useful commands which are used frequently while
working on the LCD.

The common steps are:

 Mask lower 4-bits


 Send to the LCD port
 Send enable signal
 Mask higher 4-bits
 Send to LCD port
 Send enable signal

No. Instruction Hex Decimal


1 Function Set: 8-bit, 1 Line, 5x7 Dots 0x30 48
2 Function Set: 8-bit, 2 Line, 5x7 Dots 0x38 56
3 Function Set: 4-bit, 1 Line, 5x7 Dots 0x20 32
4 Function Set: 4-bit, 2 Line, 5x7 Dots 0x28 40
5 Entry Mode 0x06 6
Display off Cursor off
6 (clearing display without clearing 0x08 8
DDRAM content)
7 Display on Cursor on 0x0E 14
8 Display on Cursor off 0x0C 12
9 Display on Cursor blinking 0x0F 15
10 Shift entire display left 0x18 24
12 Shift entire display right 0x1C 30
13 Move cursor left by one character 0x10 16
14 Move cursor right by one character 0x14 20
Clear Display (also clear DDRAM
15 0x01 1
content)
Set DDRAM address or coursor
16 0x80+add* 128+add*
position on display
Set CGRAM address or set pointer
17 0x40+add** 64+add**
to CGRAM location
Table : Frequently used commands and instructions for LCD

* DDRAM address given in LCD basics section see Figure 2,3,4


** CGRAM address from 0x00 to 0x3F, 0x00 to 0x07 for char1 and so on..

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy