Professional Documents
Culture Documents
LCD pins
Pins 1 2 3 4 5 6 7-14 15-16 Function Ground Vcc (5V) Vee - Contrast Voltage "RS" Instruction/Register Select "R/W", Read/Write LCD Registers "E" Clock Data I/O pins LCD Backlight
Prepared by Ober Choo 5
E Clock
LCD read data
LCD Command
R/S 4 0 0 0 0 0 0 0 1 DB7 14 0 0 0 0 0 0 1 D DB6 13 0 0 0 0 0 0 A D DB5 12 0 0 0 0 0 1 A D DB4 11 0 0 0 0 1 DL A D DB3 10 0 0 0 1 SC N A D DB2 9 0 0 1 D RL F A D DB1 8 0 1 ID C X X A D DB0 7 1 X S B X X A D Pin count at LCD Clear Display Return Cursor and LCD to Home Position Set Cursor Move Direction Enable Display/Cursor Move Cursor/Shift Display Set Interface Length Move Cursor to Display Write a Character to the Display at the Current Cursor Position Functions
Bit Function
Command or Character Set cursor move direction Specifies to shift the display Sets On/Off of all display Cursor On/Off Cursor blinks Sets cursor move or display shift Shift direction Sets interface data length Number of display line Character font
Writing Character
LCD_RS = 1 Send Character to LCD data line (MSB nibble only) E Clock Send Character to LCD data line (LSB nibble) E Clock Done
Sending Character to LCD LCD_PORT = (PORTD & 0xF0) | (LCD_data >> 4);
10
LCD_RS = 0 Send address to LCD data line (MSB nibble only) E Clock Send Character to LCD data line (LSB nibble) E Clock Done
Sending Command to LCD LCD_PORT = (PORTD & 0xF0) | (address >> 4);
11
ASCII Character
LCD display character according to ASCII. ASCII = American Standard Code for Information Interchange It is uses in many communication protocol. 1 is not equal to 1. If you want to display 1 on LCD, you should send 49 or 0x31.
13
14
Lab01
Assignment 1 Initialize LCD Assignment 2 Display character and jump Assignment 3 Create function Assignment 4 Using library functions
Assignment 1
Initialize LCD
Lab01- Assignment 1
Task:
Initialize the 2x16 Character LCD
Open project file of Lab01.mcp (under Lab01 folder) using MPLAB IDE. Initialize I/O pin for all necessary hardware. SW1 to start initializing LCD as 2 line display, cursor blink and display
17
Program Flow
Start Initialize I/O
NO
SW1 = press
YES
LCD_DATA = 0x28 (Complete 2 nibble with E clock) LCD_DATA = 0x06 (Complete 2 nibble with E clock) LCD_DATA = 0x0F (Complete 2 nibble with E clock) LCD_DATA = 0x01 (Complete 2 nibble with E clock) Done
LCD_E = 1 Delay 15ms LCD_RS = 0 LCD_DATA = 0x20 (MSB nibble only) E Clock
18
19
Expected Result
Before SW1 is press, LCD show all across the 1st line. After pressing SW1, Home position of LCD show a blinking cursor.
20
21
Assignment 2
Display Character and jump
Lab01- Assignment 2
Task:
Display a character, further jump to display another character at other position.
Use the same project, continue code writing in Lab01_main.c The new code for display an A, further jump to 2nd line and display B should be continued after initialization of LCD (Assignment 1).
23
24
Program Flow
Start Initiate I/O LCD_RS = 0 LCD_DATA = (0x80 | 0x40) (Complete 2 nibble with E clock) LCD_RS = 1 Initiate LCD (Assignment 1) LCD_RS = 1 LCD_DATA = A (Complete 2 nibble with E clock) 25 LCD_DATA = 0x0F (Complete 2 nibble with E clock) Done
SW1 = press
26
Expected Result
Before SW1 is press, LCD show a blinking , thats the cursor at home position on LCD. After pressing SW1, Home position of LCD will have an A while 2nd line will have a B.
27
28
Assignment 3
Create function
Lab01- Assignment 3
Task:
Create an E clock function for LCD.
Use the same project, continue code writing in Lab01_main.c After the new E clock function is completed, replace all E clock code in Assignment 1 & 2 with new function.
30
Example
31
32
Expected Result
Assignment 3 uses smaller program memory than Assignment 2. Code is more tidy by calling functions.
33
34
Assignment 4
Using Library Functions
Lab01- Assignment 4
Task:
Adding C file and Calling LCD functions.
Use the same project, continue code writing in Lab01_main.c Add lcd.c and lcd.h into Lab01 project. Call existing LCD functions.
36
37
Include lcd.h
Use #include lcd.h in Lab01_main.c
38
Functions to call
Refer in header file lcd.h
39
Expected Result
Code is more tidy by calling functions. Whole project is more organize.
41
42
Bonus!
Use LCD Backlight for more clearer view of message. To control the back light, make LCD_BACKLIGHT high (1).
43
Thank You
Q&A Please!
Useful links
http://ouwehand.net/~peter/lcd/lcd0.shtml#ins truction_set
45