Tutorial #3: LCD Displays

This tutorial provides a simple set of instructions to use an LCD display that runs using a Hitachi 44780 chip or compatible. Included in this tutrorial is a complete part list, hardware diagram and software explanation.

Part List:

Hardware:

Setting up the LCD is not particularly difficult, and can be readily done with a few wires and two resistors. Powering the backlight allows you to read the display and is done through a 390 Ohm resistor in series with Vdd and Gnd. The LCD also requires power, so connect Vss(LCD pin 1) to Gnd, Vdd(LCD pin 2) to Vdd, and Vee(Lcd pin 3) to gnd as well. Vee is the contrast of the display, which could be hooked to a 5kOhm potientometer in place of directly to ground.

Since the LCD is being operated in 4 bit mode, see software section, data lines 4 to 7 are hooked up to the PIC on PORTA 0 to 3 respectively.Here is a table listing all the connections for this setup

Note: The pinout used bellow is for the LCD in the above part list. For any other model be sure to check the data sheet as the pinout may not be identicle.

LCD Pinout Connection Table:

LCD Pin
Connection or PIC Pin
1: Vss
GND
2: Vcc
V+
3: Vee
GND
4: RS
PORTA, 4 (PIC pin 3)
5: R/W
PORTA, 6 (PIC pin 15)
6: E
PORTA, 7 (PIC pin 16)
11: D4
PORTA, 0 (PIC pin 17)
12: D5
PORTA, 1 (PIC pin 18)
13: D6
PORTA, 2 (PIC pin 1)
14: D7
PORTA, 3 (PIC pin 2)
15: LED +
390 Ohm Resistor
16: LED Gnd
GND

 

LCD Hardware Diagram

Breadboared LCD Setup

Software:

The code for operating the LCD may appear complex at first glance, but once the majority of the instruction routines have been written, it is straight forward to use. The routines and commands explained in this tutorial are specifc to the code that was written to operate the screen. However the LCD is capble of many more operations, including custom fonts.

In this case, the LCD is being operated in 4 bit mode. This is set using the LCD_Init routine, and has the added advantage of using less pins, which is ideal when using chips like the 16f88.

Firstly, the display needs to initalized using the LCD_Init routine, which will setup the display into the 4 bit operation mode, set the cursor to move forward one space, set the cursor to flash, and clear the display for use. All the values for instructions being passed to the LCD were taken from the following table in the LCD Module datasheet, avalible at the bottom of the page.

In order to write a character to the display an ASCII character must be passed to the work register, and then LCD_Char routine is called to display the character to the display.

To move to the second line of the screen, or back to the first, the LCD_Line1 or LCD_Line2 routines can be used.

When the display routines are finished and the program ends, the display will automatically clear. Therefore a holding loop or delay is needed to keep the characters on the screen.

Setup of the PIC:

In order to pass information to the LCD we use an "EQU" command to call PORTA LCD_Port in order to pass bits directly to the LCD. This means that PORTA is being used as an output port, and that each bit of PORTA is equal to that of what we put into LCD_Port. For example, if 10001010 were placed into LCD_Port, then on PORTA, bits 1, 3, and 7 would be high.

Also the "EQU" command is used to assign bit names fro RS, R/W, and E to their respective ports. Therefore, if bit 7 is the enable line for instance and it needs to be set, instead of writing bsf LCD_Port, 7, bsf LCD_Port, E could be written. This makes the code easier to read and follow.

The Swapf Instruction:

Since the LCD is being operated in 4-bit mode, the swapf instruction is needed to format some of the information that will be passed to the LCD. Therefore, it is useful to know how the swapf instruction works. Swapf take a register f, and exchanges the first 4 bits, known as a nibble, with the last 4 bits. This instruction is useful for the LCD since it requires an 8-bit address, but is only being passed 4-bits at a time. Its exact use is further explained in the LCD_Cmd and LCD_Char routines

The Routines:

LCD_Clr:

Used to clear the display

  • Move 0x01 into the LCD to clear the display

LCD_Init:

Used to setup the LCD display in 4 bit mode and prepare it for use.

  • Move 0x20 into the LCD to enable 4 bit mode
    • Move 0x20 into the work register
    • Call LCD_Cmd
  • Move 0x28 into the LCD to enable 1 character shift
  • Move 0x06 into the LCD to move the cursor forward one character
  • Move 0x0D into the LCD to turn on the display and flash the cursor
  • Call LCD_Clr after initializing the display

LCD_ CharD:

Converts what is in the work register into an ascii value that can be read by the 44870 LCD Chip. This is done by adding 0x30 with the work register.

LCD_Cmd:

Used to pass commands to the LCD through the work register

  • Move the value of the work register into the TempLCD register
  • Move the upper nibble of the TempLCD register into the work register with the "swapf" command
  • "andlw" of 0x0F with the work register, in order to clear the upper 4 bits of the register so that it does not interfear with the E, RS, and R/W control lines
  • Move the work register into LCD_Port (ie PORTA)
  • Then clear the RS bit
  • Pulse the enable line to send the command to the LCD
  • The Process is the repeated for the lower bits of the TempLCD register
  • Move the TempLCD into the work register
  • "andlw" of 0x0F with the work register, in order to clear the upper 4 bits
  • Move the work register into LCD_Port (ie PORTA)
  • Clear the RS bit
  • Pulse the enable, E, line to send the command to the LCD

LCD_Char:

This method is nearly the same as the LCD_Cmd routine, only since we are accessing a register on the 44780 chip, the RS line must be set to 1.

LCD_Line1:

Used to move the cursor to Line 1 row 1

  • Move 0x80 into the LCD to move the cursor to the first column and row

LCD_Line2:

Used to move the cursor to Line 2 row 1

  • Move 0xC0 into the LCD to move the cursor to the first colum and second row

LCD_Line1W:

Used to move the cursor to Line1 row W

  • Addlw with 0x80 and put it into the LCD
  • Moves the cursor into the first column and the Wth row

LCD_Line2W:

Used to move the cursor to Line2 row W

  • Addlw with 0xC0 and put it into the LCD
  • Moves the cursor into the second column and the Wth row

LCD_CurOff:

Used to set the display on/off and cursor command

  • Move 0x0C into the LCD

LCD_CurOn:

Used to set the display on/off and cursor command

  • Move 0x0D into the LCD

Conlusion:

Using this source code and setup, any number of information can be displayed to an LCD screen. Although the code provided is written specifically for the LCD screen we used, it can easily be adapted to for use with other displays. Another useful idea not covered in this tutorial is using a lookup table to display values to the LCD, as opposed to manual calling LCD_Char each time after moving a value into the work register. Instead, place all the values in a lookup table, and create a loop to return each value into the work register then call LCD_Char. This method will make your code more compact and easy to follow.

External Resources: