Electronics‎ > ‎

Using an HD44780 alphanumeric LCD screen

posted 7 Sep 2013, 08:57 by James Gibbard   [ updated 7 Mar 2014, 04:33 ]
The Hitachi HD44780 is an LCD screen controller used to control small alphanumeric LCD displays. Many cheaper displays don't actually use the HD44780 IC. However, they use the same pin-out and control scheme, and therefore can be operated in the same manner. 


Connecting the display
Most screens use the following pin-out, however it is best to refer to the screen's datasheet if available.
  1. Ground
  2. Power (Normally 5V)
  3. Contrast adjust (Connect to the wiper of a 10kOhm potentiometer from VCC to GND) 
  4. Register select  (For a command RS = 0. For data RS = 1) 
  5. Read/Write (To write to the display = R/W = 0. To read = R/W = 1)
  6. Enable
  7. Data bit 0 (DB0)
  8. Data bit 1 (DB1)
  9. Data bit 2 (DB2)
  10. Data bit 3 (DB3)
  11. Data bit 4 (DB4)
  12. Data bit 5 (DB5)
  13. Data bit 6 (DB6)
  14. Data bit 7 (DB7)
  15. Backlight +
  16. Backlight -
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.

 RSR/W Operation 
 0Write a command to the display. E.g. Clear display
 0 Read the busy flag (Data bit 7) and the address counter (Data bits 0 to 6)
 1 Write data to the display 
 1 Read data from the display

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. 

Set data lines as inputs
Set R/W = 1
Set RS = 0
Set enable high
Wait 1uS
Set enable low
Read DB7
If DB7 is high then LCD busy, else if DB7 is low then LCD ready

Once the LCD is ready the microcontroller can send a command to the screen. The pseudo code below shows how this is done.

Wait until the screen is not busy
Set data lines to outputs
Set R/W = 0
Set RS = 0
Set the 8 data lines to the desired command
Set enable high
Wait 1uS
Set enable low

A list of commands can be found in the table below.

Command table
 InstructionDB7DB6DB5DB4DB3DB2DB1DB0 Description
 Clear display0 Clears display. Homes cursor
 Return home0 00000 -Returns cursor to adress 00. Resets any display shift
 Set entry modeI/D I/D sets cursor move direction
S sets if display shifts
 Display on/off0 01D sets display on or off.
C sets the cursor on or off
B sets if the cursor blinks
 Cursor/display shift0S/C R/L - S/C sets is the screen or cursor is shifted. R/L sets direction
 Function setDL -- DL sets 4 or 8 wire mode. N sets 1 or 2 line mode, and F sets font size.
 Set CGRAM addressAAAA Sets address in CGRAM 
 Set DDRAM address Sets address in DDRAM (Cursor position)

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.

 Setting Action when set to 0 Action when set to 1
 I/DCursor moves left after each letterCursor moves right after each letter
 SDisplay does not shift when typingDisplay does shift when typing
 DDisplay offDisplay on 
 CCursor offCursor on 
 BCursor blinking offCursor blinking on 
 S/CShift cursorShift screen 
 R/LShift leftShift right 
 DL4 wire mode8 wire mode 
 NOne line displayTwo line display 
 FCharacter size 5 x 8 dotsCharacter size 5 x 10 dots

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
Set data lines to outputs
Set R/W = 0
Set RS = 0
Set data lines to: 00111000 //Send "Function set" command. Sets display to                               8  wire mode, 2 lines and 5x8 dots.
Set enable high
Wait 1uS
Set enable low

Wait until the screen is not busy
Set data lines to: 00000110 //Send "Set entry mode" command. Shift cursor                                  right after character, don't move screen.
Set enable high
Wait 1uS
Set enable low

Wait until the screen is not busy
Set data lines to: 00001100 //Send "Display on/off" command. Display off,                                  cursor hidden, don't blink cursor.
Set enable high
Wait 1uS
Set enable low

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. 

Wait until the screen is not busy
Set data lines to outputs
Set R/W = 0
Set R/S = 1
Set data lines to 01001000 //ASCII code for "H"
Set enable high
Wait 1uS
Set enable low

Set data lines to 01001001 //ASCII code for "I"
Set enable high
Wait 1uS
Set enable low

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.

Wait until the screen is not busy
Set R/W = 0
Set RS = 0
Set data lines to: 10000000 //Set DDRAM address. Cursor to Row 1, column 1
Set enable high
Wait 1uS
Set enable low

Wait until the screen is not busy
Set R/W = 0
Set R/S = 1
Set data lines to 01001000 //ASCII code for "H"
Set enable high
Wait 1uS
Set enable low

Wait until the screen is not busy
Set data lines to outputs
Set R/W = 0
Set RS = 0
Set data lines to: 11000000 //Set DDRAM address. Cursor to Row 2, column 1
Set enable high
Wait 1uS
Set enable low

Wait until the screen is not busy
Set R/W = 0
Set R/S = 1Set data lines to 01001001 //ASCII code for "I"
Set enable high
Wait 1uS
Set enable low

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. 

Click image to enlarge

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.

Wait until the screen is not busy
Set data lines to outputs
Set R/W = 0
Set R/S = 1
Set data lines to 10011000 //Code for a bell symbol
Set enable high
Wait 1uS
Set enable low 

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.

Character CGRAM Address location Character code
 Custom 10100 0000 (ox40)0000 0000
 Custom 20100 1000 (0x48)0000 0001
 Custom 3
0101 0000 (0x50)0000 0010
 Custom 4
0101 1000 (0x58)0000 0011 
 Custom 50110 0000 (0x60)0000 0100
 Custom 60110 1000 (0x68)0000 0101
 Custom 70111 0000 (0x70)0000 0110
 Custom 80111 1000 (0x78)0000 0111

The pseudo code below shows how a custom symbol can be stored and then displayed on the LCD screen.

Wait until the screen is not busy   //NB. this has been omitted from the rest of this                                         code for clarity
Set data lines to outputs
Set R/W = 0
Set RS = 0
Set data lines to: 01000000 //Set the CGRAM address to line 1 of custom character 1
Pulse enable

Set R/W = 0
Set RS = 1
Set data lines to: 000 01110        //Write line 1 of the character
Pulse enable
Set data lines to: 000 11011
        //Write line 2 of the character
Pulse enable
Set data lines to: 000 10001
        //Write line 3 of the character
Pulse enable
Set data lines to: 000 10001
        //Write line 4 of the character
Pulse enable
Set data lines to: 000 10001
        //Write line 5 of the character
Pulse enable
Set data lines to: 000 10001
        //Write line 6 of the character
Pulse enable
Set data lines to: 000 10001        //Write line 7 of the character
Pulse enable
Set data lines to: 000 11111
        //Write line 8 of the character
Pulse enable

Set R/W = 0
Set RS = 0
Set data lines to: 1000 0000    //Set the DDRAM address to row 1, column 1
Pulse enable

Set R/W = 0
Set RS = 1
Set data lines to: 0000 0000    //Print custom character 1
Pulse enable


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.

TODO
  • Add option to select between different screen sizes. I.e 16x4, 16x2, 16x1, 8x4, 8x2, 16x1. Currently only 16x2 is supported.
  • Add option to select between 8x5 dot and 10x5 dot displays.
  • Modify code so that it is easier to use the code with any PIC microcontroller.
  • Add option to select 4 wire mode.
Comments