Connecting the display
Most screens use the following pin-out, however it is best to refer to the screen's datasheet if available.
The screen has 3 main control lines. RS, R/W, and Enable. The RS and R/W lines determine the operation of the screen, as shown in the table below.
The Enable line acts like a clock signal. To clock data in or out of the display the enable line should be set high for 1uS. The enable signal is registered on the falling edge.
The screen has 8 data lines which are used to both read and write data to and from the display. The screen can also operate with 4 data lines, where only DB4, DB5, DB6, and DB7 are used. In this case DB0 to DB3 can be left unconnected. This article will only cover operation of the display using all 8 data lines.
Setting up the display
Once the display is connected to the microcontroller, it needs to be correctly configured before it can be used. There are several settings that configure how the display operates; these are set by issuing commands to the LCD.
To send a command to the LCD the microcontroller must first check if the screen is busy. If the screen is busy it will ignore any commands sent to it. The pseudo code below shows how to check if the screen is busy.
Once the LCD is ready the microcontroller can send a command to the screen. The pseudo code below shows how this is done.
A list of commands can be found in the table below.
The dash "-" means that it doesn't matter if the data bit is a 1 or 0. A is an address bit. The remaining bit settings are shown in the table below.
To initialise the display we need to send several configuration commands as shown in the pseudo code below.
Wait until the screen is not busy
Wait until the screen is not busy
Controlling the display
Now the screen has been configured, data can be written to the LCD display. By default the cursor starts in the top left hand corner of the screen. The pseudo code below shows how the text "HI" can be written to the screen.
When "H" is entered the cursor automatically moves one character to the right. This means that when "I" is entered it appears to the right of "H".
If you were to keep entering more characters the cursor would eventually disappear off the edge of the screen. Characters will still be written to the LCD, but they will not be visible. This is because while the screen may only have space for 16 (or 8) characters on each line, the controller actually has space to store 80 characters in its memory.
A map of the display's memory (DDRAM) is shown below for the 4 most common LCD sizes.
Click image to enlarge
To set the position of the cursor use the "Set DDRAM address" command from the command table further up the page. The pseudo code below shows how the cursor position can be set.
This will print "H" on row 1 and a "I" underneath it.
Using the "Cursor/Display shift" command (see table further up the page) the cursor or screen can be moved left or right by one position. If the display is shifted the DDRAM addresses that are visible will change. This is shown in the image below.
The shift of the display can be reset by sending the "Return home" command (see command table further up the page).
Displaying symbols and other characters
The display supports the standard ASCII codes for the numbers 0-9, letters a-z and A-Z, as well as most standard ASCII symbols such as !?(),. In addition to these, there are many extra symbols. The characters are stored in the CGROM (Character generator rom). The contents of the CGROM can vary depending on who manufactured the display, but most manufactures either use the European or Asian character set produced by Hitachi. These can be found on pages 17 and 18 of the HD44780 datasheet.
The European character set is shown below. Using the table the character code for any character in the set can be identified.
Symbols are sent to the screen in the same way as normal characters, as shown below.
If the symbols do not match either the European or Asian character sets then the display is using a custom character set. In this case refer to the datasheet of the LCD display.
Generating custom LCD characters
Sometimes it is necessary to display characters that are not part of the default character set. The HD44780 has space for 8 custom characters (5x8 dots) which can be displayed on the screen. (For 5x10 dot displays please refer to page 19 of the Hitachi datasheet).
Each character requires 8 bytes of data, one for each of the 8 lines. The 3 MSBs of each byte are ignored, with the remaining 5 bits setting whether the dots appears on or off in the symbol, this is shown in the image below.
The CGRAM address of the first line of each of custom characters is shown in the table below. The character code is also shown. This is the code used to display the symbol when it has already been stored in the CGRAM.
The pseudo code below shows how a custom symbol can be stored and then displayed on the LCD screen.
An excellent web interface for designing custom characters can be found at quinapalus.com.
Interfacing an HD44780 LCD display with a PIC microcontroller
If you wish to use the HD44780 display with a PIC 16 microcontroller I have written a library that implements most of the functions of the display. The code can be found here.