lcd interfacing


LCD Pinout

 8 data pins D7:D0
Bi-directional data/command pins.
Alphanumeric characters are sent in ASCII format.   
 
 RS:  Register Select
RS = 0 -> Command Register is selected
RS = 1 -> Data Register is selected
 
 R/W: Read or Write
0 -> Write,  1 -> Read
 
 E: Enable (Latch data)
Used to latch the data present on the data pins.
A high-to-low edge is needed to latch the data.
 
 VEE : contrast control

 
NOTE: When writing to the display, data is transferred only on the high to low transition of this signal. However, when reading from the display, data will become available shortly after the low to high transition and remain available until the signal falls low again.
LCD Memory
The LCD display contains three memory blocks:
  • DDRAM Display Data RAM;
  • CGRAM Character Generator RAM; and
  • CGROM Character Generator ROM.
DDRAM Memory
DDRAM memory is used for storing characters to be displayed. The size of this memory is sufficient for storing 80 characters. Some memory locations are directly connected to the characters on display.

It works quite simply: it is sufficient to configure the display so as to increment addresses automatically (shift right) and set the starting address for the message that should be displayed (for example 00 hex).
After that, all characters sent through lines D0-D7 will be displayed in the message format we are used to- from left to right. In this case, displaying starts from the first field of the first line since the address is 00 hex. If more than 16 characters are sent, then all of them will be memorized, but only the first sixteen characters will be visible. In order to display the rest of them, a shift command should be used. Virtually, everything looks as if the LCD display is a “window” which moves left-right over memory locations containing different characters. This is how the effect of message “moving” on the screen is made.
If the cursor is on, it appears at the location which is currently addressed. In other words, when a character appears at the cursor position, it will automatically move to the next addressed location.
Since this is a sort of RAM memory, data can be written to and read from it, but its contents is irretrievably lost when the power goes off.
CGROM Memory
CGROM memory contains the default chracter map with all characters that can be displayed on the screen. Each character is assigned to one memory location.

The addresses of CGROM memory locations match the characters of ASCII. If the program being currently executed encounters a command “send character P to port”, then the binary value 0101 0000 appears on the port. This value is the ASCII equivalent to the character P. It is then written to LCD, which results in displaying the symbol from 0101 0000 location of CGROM. In other words, the character “P” is displayed. This applies to all letters of alphabet (capitals and small), but not to numbers.
As seen on the previous “map”, addresses of all digits are pushed forward by 48 relative to their values (digit 0 address is 48, digit 1 address is 49, digit 2 address is 50 etc.). Accordingly, in order to display digits correctly, each of them needs to be added a decimal number 48 prior to be sent to LCD.
From their inception till today, computers can recognize only numbers, but not letters. It means that all data a computer swaps with a peripheral device has a binary format, even though the same is recognized by the man as letters (keyboard is an excellent example). Every character matches the unique combination of zeroes and ones. ASCII is character encoding based on the English alphabet. ASCII code specifies correspondance between standard character symbols and their numerical equivalents.
CGRAM memory
Apart from standard characters, the LCD display can also display symbols defined by the user itself. It can be any symbol in the size of 5x8 pixels. RAM memory called CGRAM in the size of 64 bytes enables it.
Memory registers are 8 bits wide, but only 5 lower bits are used. Logic one (1) in every register represents a dimmed dot, while 8 locations grouped together represent one character. It is best illustrated in figure below:

Symbols are usually defined at the beginnig of the program by simply writing zeros and ones to registers of CGRAM memory so that they form desired shapes. In order to display them it is sufficient to specify their address. Pay attention to the first coloumn in the CGROM map of characters. It doesn't contain RAM memory addresses, but symbols being discussed here. In this example, “display 0” means - display “č”, “display 1” means - display “ž” etc.
Registers
The HD44780 has two 8-bit registers, an instruction register (IR) and a data register (DR). The IR stores instruction codes. The DR temporarily stores data to be written into DDRAM or CGRAM and temporarily stores data to be read from DDRAM or CGRAM. Data written into the DR is automatically written into DDRAM or CGRAM by an internal operation. . These two registers can be selected by the register selector (RS) signal. See the table below:


Register Selection
RS
R/W
Operation
0
0
IR write as an internal operation (display clear, etc.)
0
1
Read busy flag (DB7) and address counter (DB0 to DB6)
1
0
DR write as an internal operation (DR to DDRAM or CGRAM)
1
1
DR read as an internal operation (DDRAM or CGRAM to DR)

LCD Basic Commands
All data transferred to LCD through the outputs D0-D7 will be interpreted as a command or a data, which depends on the pin RS logic state:
RS = 1 - Bits D0-D7 are addresses of the characters to be displayed. LCD processor addresses one character from the character map and displays it. The DDRAM address specifies the location on which the character is to be displayed. This address is defined before the character is transferred or the address of previously transferred character is automatically incremented.
RS = 0 - Bits D0 - D7 are commands which determine the display mode. The commands recognized by the LCD are given in the table below:
COMMAND
RS
RW
D7
D6
D5
D4
D3
D2
D1
D0
EXECUTION TIME
Clear display
0
0
0
0
0
0
0
0
0
1
1.64mS
Cursor home
0
0
0
0
0
0
0
0
1
x
1.64mS
Entry mode set
0
0
0
0
0
0
0
1
I/D
S
40uS
Display on/off control
0
0
0
0
0
0
1
D
U
B
40uS
Cursor/Display Shift
0
0
0
0
0
1
D/C
R/L
x
x
40uS
Function set
0
0
0
0
1
DL
N
F
x
x
40uS
Set CGRAM address
0
0
0
1
CGRAM address
40uS
Set DDRAM address
0
0
1
DDRAM address
40uS
Read “BUSY” flag (BF)
0
1
BF
DDRAM address
-
Write to CGRAM or DDRAM
1
0
D7
D6
D5
D4
D3
D2
D1
D0
40uS
Read from CGRAM or DDRAM
1
1
D7
D6
D5
D4
D3
D2
D1
D0
40uS

Instruction
Decimal
HEX
Function set (8-bit interface, 2 lines, 5*7 Pixels)
56
38
Function set (8-bit interface, 1 line, 5*7 Pixels)
48
30
Function set (4-bit interface, 2 lines, 5*7 Pixels)
40
28
Function set (4-bit interface, 1 line, 5*7 Pixels)
32
20
Entry mode set
See Below
See Below
Scroll display one character right (all lines)
28
1E
Scroll display one character left (all lines)
24
18
Home (move cursor to top/left character position)
2
2
Move cursor one character left
16
10
Move cursor one character right
20
14
Turn on visible underline cursor
14
0E
Turn on visible blinking-block cursor
15
0F
Make cursor invisible
12
0C
Blank the display (without clearing)
8
08
Restore the display (with cursor hidden)
12
0C
Clear Screen
1
01
Set cursor position (DDRAM address)
128 + addr
80+ addr
Set pointer in character-generator RAM (CG RAM address)
64 + addr
40+ addr


What is the Busy flag?
Compared to the microcontroller, the LCD is an extremely slow component. Because of this, it was necessary to provide a signal which will, upon command execution, indicate that the display is ready to receive a new data. That signal, called the busy flag, can be read from line D7. When the BF bit is cleared (BF=0), the display is ready to receive a new data.
LCD Connection
Depending on how many lines are used for connecting the LCD to the microcontroller, there are 8-bit and 4-bit LCD modes. The appropriate mode is selected at the beginning of the operation. This process is called “initialization”. 8-bit LCD mode uses outputs D0-D7 to transfer data. The main purpose of 4-bit LED mode is to save valuable I/O pins of the microcontroller.

Fortunately, there is a simple solution. After sending a character or a command it is important to give the LCD enough time to do its job. Owing to the fact that execution of the slowest command lasts for approximately 1.64mS, it will be sufficient to wait approximately 2mS for LCD.