| « Accelerate | Project: UChat » |
This post is going to go over using an HD44780 character LCD. These LCDs come in many sizes by are usually defined by how many characters they display; split by how many lines and characters per line.
The one thing about these LCDs is that they have a built in microcontroller, so we are going to be using our PIC microcontrollers to interface with the built in MCU on the HD44780 LCD.
Interfacing with the LCD is rather easy but often guides are very vague on exactly what you have to do. I will be going over how to statically and dynamically update the LCD using our PICs.
The LCDs work by first configuring how the LCD operates (usually an initializing routine you use from code to code. The way the LCD displays data is that each "character cell" corresponds to a DDRAM address (Display Data RAM) you load the ASCII Hex value into the DDRAM address and the corresponding cell displays that character. Thats it! Its pretty easy to use these LCDs.
How it works:
I would first like to post a wonderful link which i used to learn how to use these LCDs. It has all the info you need but is a little confusing if you don't know what to look for.
Link: http://home.iae.nl/users/pouweha/lcd/lcd.shtml
Any HD44780 character LCD with <=80 characters (less than or equal to) will have 16 pins. For MOST LCDs (ALWAYS consult your data sheet first)
Pin 1 is VSS(Ground)
Pin 2 is VDD(Pos voltage)
Pin 3 is Vee, or voltage for contrast adjustment you hook this up in a voltage divider configuration with a potentiometer or trimmer. You use this to adjust contrast.
Pin 4 is the RS line which controls whether your sending data or an instruction. 0 is for an instruction, 1 for data.
Pin 5 is Read/Write from/to the LCD, 0 for Write, 1 for Read.
Pin 6 is the most important, it is the Enable signal, The enable line for the HD44780 is edge triggered, on a falling edge (1->0) The HD44780 reads whatever is on the data bus. This is the one thing people have most trouble with.
Pins 7-14 is the primary data bus. Its 8 bits long and you can either hook it up to a spare 8 bit port or use serial communication and a shift register.
Pin 15 is the anode to the back light. Make sure you put correct voltage across this and the cathode
Pin 16 is the cathode to the back light.
Now that you know the pin out lets look at some instructions:
Commands:
Instructions control the operation of the LCD, They clear the DDRAM addresses, reset the address counter, set how the address counter functions, how the text displays and various other functions. We do all this before we send any data to the LCD, this initializes the LCD and gets it ready for accepting data and displaying it in a meaningful fashion.
To send an instruction we set the RS and R/W lines both low. When this happens we put the instruction we want onto our data bus and send it by setting the enable line from high to low.
The Instruction set:
Command: Clear Home
RS:0
RW:0
DB:0x01
This command clears the DDRAM and sets the address counter to 0x00
Command: Return Home
RS:0
RW:0
DB:0x02
Moves the address counter to 0x00, leaves the DDRAM untouched.
Command: Entry Mode
RS:0
RW:0
DB:0x04-0x07 (000001MS)
This controls what the cursor does when it gets a command. Bit "M" controls weather its to the left or the right. 0 will decrement the cursor position, (move it to the left) a 1 will increment the cursor position (move it to the right). S Controls if the display shifts (Where the text moves but not the cursor position) 0 is off and 1 is on.
Command: Display Control
RS:0
RW:0
DB:0x08-0x0F (00001DCB)
Sets weather the display is on(D) (useful), if the cursor is on(C) and if the cursor blinks(B). A 1 correlates to enabled.
Command: Cursor / Display shift.
RS:0
RW:0
DB:0x10-0x1F (0001SDxx)
This allows you to shift(S) the display(1) or the Cursor(0) and what direction(D) left(0), right(1)
Command: Function Set
RS:0
RW:0
DB:0x20-0x3F (001LNFxx)
Sets basic control information for the LCD
L - Data Length: 0 - 4-Bit communication (We will be using 8-bit) 1 - 8-bit communication
N - 1 line or 2 line: 0 - 1 line 1 - 2 line, (just set to 1)
F - Font: 0 - 5X7 Dots 1 - 5X10 dots. Set to 1
Command: Set CGRAM address
RS:0
RW:0
DB:0x40-0x7F (01(ADDRESS))
HD44780 LCDs support custom characters, CGRAM is where you set them, i will not be covering custom characters in this post.
Command: Set DDRAM address
RS:0
RW:0
DB:0x80-0xFF (1(ADDRESS))
This sets the DDRAM address which you will start sending data to.
Sending Command to the LCD:
To send commands to the LCD we have to do several things.
-Set the RS and RW line
-Put the data/instruction we want to send on our data port (any 8 bit port)
-Send it
To send data to the HD44780 we set the E line high, do a few NOPs and then set it low. The HD44780 reads the command and operates accordingly.
While the HD44780 is performing an operation we cannot send data to it. There are two ways we can make sure we don't send information while the LCD is busy. One is to just wait long enough for the LCD to finish. The other is to check the built in BUSY signal of the HD44780, I prefer this method as it is much quicker.
The Busy Flag:
RS:0
RW:1
DB:(BF(ADDRESS))
When we set RW to 1 (read/write) we read the busy flag as well as the address counter. To read the busy flag we set RW to 1 and then the E line from 1 to 0. This will put the busy flag on DB7. A simple BTFSS loop will allow you to check the busy flag.
Writing Data to the LCD:
Writing data encompasses several functions:
-Initializing the LCD
We set the proper functions for the LCD to operate and set it up for data transfer
-Selecting the first memory location
We send the first DDRAM address we want to write at (most parts sending 0x80 will set us to the first character)
-Writing the hex value for the char we wish to display
Command: Write
RS:1
RW:0
DB:Character
To write a char we just load the HEX value that corresponds to the data we wish to display, set RS to 1 and send the data (E1->0)
We keep writing as long as we want to stay on whatever line we started at. Every time you write, the address counter automatically increments, no need to keep resending the DDRAM address.
Command: Read
RS:0
RW:1
DB:Address Contents
This command reads the current address's contents.
With this, you have all the information to control the HD44780 based character LCD. I will be posting Code and a project involving this LCD so stay tuned!