Pages

7.9 connecting LCD Display to PIC microcontroller

7.9 LCD Display

More microcontroller devices are using 'smart LCD' displays to output visual information. The following discussion covers the connection of a Hitachi LCD display to a PIC microcontroller. LCD displays designed around Hitachi's LCD HD44780 module, are inexpensive, easy to use, and it is even possible to produce a readout using the 8 x 80 pixels of the display. Hitachi LCD displays have a standard ASCII set of characters plus Japanese, Greek and mathematical symbols.
LCD_display

A 16x2 line Hitachi HD44780 display

For a 8-bit data bus, the display requires a +5V supply plus 11 I/O lines. For a 4-bit data bus it only requires the supply lines plus seven extra lines. When the LCD display is not enabled, data lines are tri-state which means they are in a state of high impendance (as though they are disconnected) and this means they do not interfere with the operation of the microcontroller when the display is not being addressed.
The LCD also requires 3 "control" lines from the microcontroller.

Enable (E)
This  line allows access to the display through R/W and RS lines. When this line is low, the LCD is disabled and ignores signals from R/W and RS. When (E) line is high, the LCD checks the state of the two control lines and responds accordingly.

Read/Write (R/W)
This line determines the direction of data between the LCD and microcontroller. When it is low, data is written to the LCD. When it is high, data is read from the LCD.

Register select (RS)
With the help of this line, the LCD interprets the type of data on data lines. When it is low, an instruction is being written to the LCD. When it is high, a character is being written to the LCD.

Logic status on control lines:
E     0 Access to LCD disabled
       1 Access to LCD enabled
R/W 0 Writing data to LCD
       1 Reading data from LCD
RS    0 Instruction
       1 Character
Writing data to the LCD is done in several steps:
Set R/W bit to low
Set RS bit to logic 0 or 1 (instruction or character)
Set data to data lines (if it is writing)
Set E line to high
Set E line to low
Read data from data lines (if it is reading)
Reading data from the LCD is done in the same way, but control line R/W has to be high. When we send a high to the LCD, it will reset and wait for instructions. Typical instructions sent to LCD display after a reset are: turning on a display, turning on a cursor and writing characters from left to right. When the LCD is initialized, it is ready to continue receiving data or instructions. If it receives a character, it will write it on the display and move the cursor one space to the right. The Cursor marks the next location where a character will be written. When we want to write a string of characters, first we need to set up the starting address, and then send one character at a time. Characters that can be shown on the display are stored in data display (DD) RAM. The size of DDRAM is 80 bytes.

The LCD display also possesses 64 bytes of Character-Generator (CG) RAM. This memory is used for characters defined by the user. Data in CG RAM is represented as an 8-bit character bit-map. Each character takes up 8 bytes of CG RAM, so the total number of characters, which the user can define is eight. In order to read in the character bit-map to the LCD display, we must first set the CG RAM address to starting point (usually 0), and then write data to the display. The definition of a 'special' character is given in the picture.

32

Before we access DD RAM after defining a special character, the program must set the DD RAM address. Writing and reading data from any LCD memory is done from the last address which was set up using set-address instruction. Once the address of DD RAM is set, a new written character will be displayed at the appropriate place on the screen. Until now we discussed the operation of writing and reading to an LCD as if it were an ordinary memory. But this is not so. The LCD controller needs 40 to 120 microseconds (uS) for writing and reading. Other operations can take up to 5 mS. During that time, the microcontroller can not access the LCD, so a program needs to know when the LCD is busy. We can solve this in two ways.

33

One way is to check the BUSY bit found on data line D7. This is not the best method because LCD's can get stuck, and program will then stay forever in a loop checking the BUSY bit. The other way is to introduce a delay in the program. The delay has to be long enough for the LCD to finish the operation in process. Instructions for writing to and reading from an LCD memory are shown in the previous table.
At the beginning we mentioned that we needed 11 I/O lines to communicate with an LCD. However, we can communicate with an LCD through a 4-bit data bus. Thus we can reduce the total number of communication lines to seven. The wiring for connection via a 4-bit data bus is shown in the diagram below. In this example we use an LCD display with 2x16 characters, labeled LM16X212 by Japanese maker SHARP. The message 'character' is written in the first row: and two special characters '~' and '}' are displayed. In the second row we have produced the word 'mikroElektronika'.

34

Connecting an LCD display to a microcontroller

File lcd.inc contains a group of macros for use when working with LCD displays.

lcd_inc

Using the macro for LCD support

lcdinit
Macro used to initialize port connected to LCD. LCD is configured to work in 4-bit mode.

Example:
lcdinit

lcdtext
lcdtext prints the text of up to 16 characters, which is specified as a macro parameter. First parameter selects the line in which to start printing. If select is zero, text is printed from the current cursor position.

Example:
lcdtext 1, "mikroelektronika"

lcdtext 1, "Temperature1"  ;Print the text starting from line 1, character 1

lcdtext 2, "temp="              ;Print the text starting from line 2, character 1

lcdtext 0, "     C"                ;Print C in the rest of the line 2

lcdcmd
Sends command instructions

LCDCLR
=  b'00000001'
;Clear display, cursor home

LCDCH
=  b'00000010'
;Cursor home

LCDCL
=  b'00000100'
;Move the cursor to the left

LCDCR
=  b'00000110'
;Move the cursor to the right

LCDSL
=  b'00011000'
;Move the content of display to the left

LCDSR
=  b'00011100'
;Move the content of display to the right

LCDL1
=  b'10000000'
;Select line 1

LCDL2
=  b'11000000'
;Select line 2

Example:
lcdcmd LCDCH

lcdbyte
Prints one byte variable and omits leading zeros

Example:
lcdbyte  Temperature

When working with a microcontroller the numbers are presented in a binary form. As such, they cannot be displayed on a display. That's why it is necessary to change the numbers from a binary system into a decimal system so they can be easily understood. For printing the variables lcdbyte and lcdword we have used the macros digbyte and digword which convert the numbers from binary system into a decimal system and print the result on LCD. Main program has the purpose of demonstrating use of LCD display. At the start it's necessary to declare variables LCDbuf, LCDtemp, Digtemp, Dig1, Dig2, and Dig3 used by the macros for LCD support. It is also necessary to state the port of microcontroller that LCD is connected to. Program initializes the LCD and demonstrates printing text and 8-bit variable temp.

lcd_asm