using an hd44780 alphanumeric lcd

10
 Using an HD44780 alphanumeric LCD screen 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. C onne cting the d i sp lay   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 pot entiometer from V CC to GND) 4. Register select (For a comm and 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.  RS R/W Operation 0 0 Write a command to the display. E.g. Clear display 0 1 Read the busy flag (Data b it 7) and the address counter (Data bits 0 to 6) 1 0 Write data to the display

Upload: benji6999

Post on 08-Oct-2015

34 views

Category:

Documents


0 download

DESCRIPTION

Good writing of Using an HD44780 alphanumeric LCD with microcontrollers

TRANSCRIPT

  • Using an HD44780 alphanumeric LCD screen

    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.

    RS R/W Operation

    0 0 Write a command to the display. E.g. Clear display

    0 1 Read the busy flag (Data bit 7) and the address counter (Data bits 0 to

    6)

    1 0 Write data to the display

  • 1 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

    Instruction DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0 Description

    Clear display 0 0 0 0 0 0 0 1 Clears display. Homes cursor

    Return home 0 0 0 0 0 0 1 - Returns cursor to adress 00. Resets any display shift

    Set entry mode 0 0 0 0 0 1 I/D S I/D sets cursor move direction

    S sets if display shifts

  • Display on/off 0 0 0 0 1 D C B D sets display on or off.

    C sets the cursor on or off

    B sets if the cursor blinks

    Cursor/display shift 0 0 0 1 S/C R/L - - S/C sets is the screen or cursor is shifted.

    R/L sets direction

    Function set 0 0 1 DL N F - - DL sets 4 or 8 wire mode. N sets 1 or 2

    line mode, and F sets font size.

    Set CGRAM address 0 1 A A A A A A Sets address in CGRAM

    Set DDRAM address 1 A A A A A A A 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/D Cursor moves left after each letter Cursor moves right after each letter

    S Display does not shift when typing Display does shift when typing

    D Display off Display on

    C Cursor off Cursor on

    B Cursor blinking off Cursor blinking on

    S/C Shift cursor Shift screen

    R/L Shift left Shift right

    DL 4 wire mode 8 wire mode

    N One line display Two line display

    F Character size 5 x 8 dots Character 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 1 0100 0000 (ox40) 0000 0000

    Custom 2 0100 1000 (0x48) 0000 0001

    Custom 3 0101 0000 (0x50) 0000 0010

    Custom 4 0101 1000 (0x58) 0000 0011

    Custom 5 0110 0000 (0x60) 0000 0100

  • Custom 6 0110 1000 (0x68) 0000 0101

    Custom 7 0111 0000 (0x70) 0000 0110

    Custom 8 0111 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.