Instruction Rs RW D7 D6 D5 D4 D3 D2 D1 D0 Description
Instruction Rs RW D7 D6 D5 D4 D3 D2 D1 D0 Description
direction(I/D) and auto display shift (S) Turn display (D) and cursor (C) ON/OFF. Set cursor blinking(B) Shift display/cursor (S), specify direction (R/L) Set Interface data width (DL), number of display lines (N), character font (F) Set CGRAM address (D0-D5), CGRAM data is sent after this command Set DDRAM address (D0-D6), DDRAM data is sent after this command Read busy flag (BF) and address counter(D0-D6) Write data (D0-D7) into DDRAM/CGRAM Read data (D0-D7) from
I/D S
Display Control
R/L x
DL N
BF
Read Data
DDRAM/CGRAM Legends used in table:I/D: 1- Increment, 0- Decrement S: 1- Auto Display shift, 0 No display shift D: 1- Display ON, 0 Display OFF C: 1- Cursor ON, 0- Cursor OFF B: 1- Cursor blinking ON, 0 Cursor blinking OFF S: 1- Display Shift, 0 Cursor move R/L: 1- Shift right, 0- Shift left DL: 1- 8 bit interface, 0- 4 bit interface N: 1- 2 lines, 0- 1 line F: 1- 5X10 dots font, 0- 5X7 dots font BF: Busy Flag LCD Controller (JHD162A, HD44780) Commands All LCD functions are actually a combination of above mentioned commands. We will use some example functions to understand how to use commands to build higher level functionality. 2.1 LCD Initialization: To initialize LCD a typical sequence of commands is followed. We also configure all LCD related parameters such as data interface width, display characteristics etc. in this function. Initialization sequence: 1. Wait for power supplies to stabilize. Wait for approx 2-3 ms 2. Write 030 to LCD 3. wait for 50ms 4. Write 030 to LCD 5. wait for 10ms 6. Write 030 to LCD 7. wait for 1ms 8. Set data interface width (Write 030 for 8 bit or 020 for 4 bit) to LCD 9. wait for 50ms 10. Set display lines and font dots 11. Set Display OFF, Cursor and blinking off (Write 00B) 12. Set Display ON, Cursor and blinking off (Write 00F) 13. Set Cursor move direction (I/D) and display shift (S) 14. Clear display (Write 001) 15. Wait 50ms 2.2 Moving Cursor to Home: To move cursor to home position without clearing the screen we will use following commands 1. Write 003 2. Wait for approx 2ms
Similarly many functions can be implemented using LCD commands. LCD Library available from source code section (bottom of this article) provides following functions using C. Function Description
Utility functions InitializeLCD Initialize LCD hardware ClearScreen Clears LCD screen MoveCursorToHome Moves cursor to home position Cursor functions SetCursorPos Sets cursor position SetCursorState Sets cursor state (Blink,ON/OFF) SetCursorBlink Sets cursor in blinking state SetCursorNoBlink Disables cursor blinking SetCursorOn Cursor enabled SetCursorOff Cursor disabled Writing functions WriteCharAtPos Writes character at defined position WriteStringAtPos Writes string at defined position Custom character/font functions CreateCustomCharacter Creates user defined character LCD Library functions 2.3 Detailed function description:
void InitializeLCD(); None None Initializes LCD hardware. Configures data interface(4bit or 8 bit). Clears the display and places cursor at home with blinking mode
void ClearScreen(); None None Clear screen and moves cursor to home. This function takes approx 1.6 ms to complete.
MoveCursorToHome void MoveCursorToHome(); Prototype None Input parameters None Output Moves the cursor to home position Description
void SetCursorPos(unsigned char row, unsigned char col); row: row number (first row = 0 and so on)col: Column number (first column = 0 and so on) None Sets the cursor position to specified row and column position
void SetCursorState(unsigned char isBlinking, unsigned char isOn); isBlinking: 0 no blinking, 1- blinkingisOn: 0 cursor OFF, 1cursor ON None Changes cursor state (Blinking and On/Off)
void WriteCharAtPos(unsigned char row, unsigned char col, char ch); row: row position, First row =0 and so oncol: column position, First column = 0 and so onch: character to be written None Writes a single character ch at specified row and column position
void WriteStringAtPos(unsigned char row, unsigned char col, char *str); row: row position, First row =0 and so oncol: column position, First column = 0 and so onstr: string to be written None Writes character string at specified row and column position