8051 Interfacing LCD With 8051: Search
8051 Interfacing LCD With 8051: Search
8051 Interfacing LCD With 8051: Search
Search
// youre reading...
1.Interfacing Hardware: Lets start with connecting the LCD hardware with microcontroller (P89v51). The following table lists all pins of LCD module (JHD162A) along with their description. Pin No 1 2 3 4 5 6 7 8 Name Vss Vdd Vee RS RW E D0 D1 Description Ground +5V Contrast Adjustment -2V to -5V Register Select 1 -Read , 0- Write Enable Strobe Data Line Data Line
9 10 11 12 13 14 15 16
D2 D3 D4 D5 D6 D7 LED+ LED-
Data Line Data Line Data Line Data Line Data Line Data Line Backlit LED +V Vdd (Optional signal) Backlit LED V Vss (Optional signal) LCD Pin Description (JHD162A)
1.1 Connecting Supplies: Vss (Pin 1) is connected to board ground. Vdd (Pin 2) is connected to + 5V supply. Vee (Pin 3) is connected using a variable resistance for adjusting contrast. LED- (Pin 16) is connected to GND LED+ (Pin 15) is connected to Vdd 1.2 Connecting control and data signals: Control signal RS, RW and E are connected to IO Port pins. For data lines we can have two configurations 8 bit mode and 4 bit mode. In 8 bit mode all lines D0-D7 are connected to IO port pins of microcontroller. In 4 bit mode only D4-D7 are connected to IO port pins. The example code available in source section(at bottom of this article) uses the 4 bit mode as default, however 8 bit mode is also supported. Following images show both 8 bit and 4 bit configuration for interfacing LCD with P89v51 microcontroller.
2. Writing software: The LCD module is an intelligent component. We communicate to LCD module by sending commands from microcontroller. To write data to LCD module separate sequence is followed for 4 bit and 8 bit mode. Writing command for 8 Bit mode:
Write 4 bit data (upper nibble) on D4-D7 Generate strobe by taking EN from high to low Write 4 bit data (lower nibble) on D4-D7 Generate strobe by taking EN from high to low
The following table lists commands supported by LCD module: LCD Commands: Instruction RS RW D7 NOP 0 0 0 Clear Display 0 0 0 Cursor Home 0 Entry mode set 0 0 0 0 0 D6 0 0 0 0 D5 D4 D3 0 0 0 0 0 0 0 0 0 0 0 0 D2 D1 D0 Description 0 0 0 No Operation 0 0 1 Clear Display and Address counter = 0 0 1 x Address counter = 0 1 I/D S Set cursor direction(I/D) and auto display shift (S) D C B Turn display (D) and cursor (C) ON/OFF. Set cursor blinking(B) R/L x x Shift display/cursor (S), specify direction (R/L) F x x 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)
Display Control
DL N
BF
Read Data
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)
Description
Enables cursor
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
CreateCustomCharacter void CreateCustomChar(unsigned char charIndex, unsigned char Prototype *charBitmap); charIndex: character index in CGRAM (0 to 7 are reserved for user Input parameters defined characters)charBitmap: Bitmap corresponding to the custom character. Character bitmap consists of 5X8 pixels. Bitmap is stored in 8 bytes. 5 LSBs of each byte store the pixel information.Pixel layout of custom character:charBitmap[0] * * * 1 2 3 4 5 charBitmap[1] * charBitmap[2] * * * * * 6 7 8 9 10
11 12 13 14 15
charBitmap[3] * charBitmap[4] * charBitmap[5] * charBitmap[6] * charBitmap[7] * Source Code: Microcontroller: P89V51RD2 LCD Controller: JHD162A Compiler: SDCC Source: LCD Source code
* * * * *
* * * * *
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
Related posts: 1. 2. 3. 4. Creating custom characters (Font) with LCD 8051 First Program: LED Blink using IOs Using SDCC (Open source compiler)for 8051 Development 3 Advanced tricks with LCD Interfacing
Discussion
5 comments for Interfacing LCD with 8051
3.
the code is giving lot of errors with 89v51reg.h Posted by Ankit Garg | July 18, 2011, 1:19 pm
4. i have a question here.do i need 10k puller when interface with p0 with lcd..because i see sum connection there have pulleri dnt understand about it. Posted by sivan | August 9, 2011, 8:27 pm
Post a comment
Name * E-mail * Web site
Comment
Topics
8051 Different Thoughts Embedded Basics Inside Systems Interfacing Source Code Tips and Tricks Tools
Most Emailed
Inside Set Top Box (STB) part 2 Decoder SoC and Software - 2 emails 8051 First Program: LED Blink using IOs - 2 emails Inside Set Top Box (STB) - 1 email
2010 embeddedtutorial.com.