You are on page 1of 45

2 x 16 Character Parallel LCD Digital IO Subroutines/Functions

V1.0 May 2009

Liquid Crystal Display

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

E = high Delay E = low Delay


E clock period >= 500ns
Prepared by Ober Choo 6

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

Prepared by Ober Choo

LCD Command Bit


Bit Name
RS ID S D C B SC RL DL N F

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

Bit = Low (0)


Command Decrement cursor position No display shift Display off Cursor off Cursor blink off Move cursor Shift left 4-bit interface 1 line 5x7 dots

Bit = High (1)


Character Increment cursor position Display shift Display on Cursor on Cursor blink on Shift Display Shift right 8-bit interface 2 lines 5x10 dots

Prepared by Ober Choo

Initiate LCD into 4 bit


Start LCD_E = 1 Delay 15ms LCD_RS = 0 LCD_DATA = 0x20 (MSB nibble only) E Clock Done 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)

Prepared by Ober Choo

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);

LCD_PORT = (PORTD & 0xF0) | (LCD_data & 0x0F);

Prepared by Ober Choo

10

Display at any where?

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);

LCD_PORT = (PORTD & 0xF0) | (address & 0x0F);

Prepared by Ober Choo

11

PIC and LCD


PIC pin no. 40 39 38 22 21 20 19 PIC pin label RB7 RB6 RB5 RD3 RD2 RD1 RD0 LCD pin label LCD_BACKLIGHT LCD_RS LCD_E DB7 DB6 DB5 DB4
Prepared by Ober Choo

LCD pin no. 16 4 6 14 13 12 11


12

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

Prepared by Ober Choo

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

Prepared by Ober Choo

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

Prepared by Ober Choo

18

OK! Lets start!


Write your code in Lab01_main.c Compile and debug if there is any error. Load it to TB40A and press SW1.

Prepared by Ober Choo

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.

Prepared by Ober Choo

20

What have you learnt?


Steps to initialize LCD into 4 bit mode Using bitwise operator &, | to combine bits. Using bitwise operator >> to shift bit.

Prepared by Ober Choo

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).

Prepared by Ober Choo

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

Prepared by Ober Choo

OK! Lets start!


Continue new code in Lab01_main.c Compile and debug if there is any error. Load it to TB40A and press SW1.

Prepared by Ober Choo

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.

Prepared by Ober Choo

27

What have you learnt?


Display ASCII character on LCD. Change the cursor to other position on LCD.

Prepared by Ober Choo

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.

Prepared by Ober Choo

30

Example

Prepared by Ober Choo

31

OK! Lets start!


Continue new code in Lab01_main.c Compile and debug if there is any error. Load it to TB40A and press SW1.

Prepared by Ober Choo

32

Expected Result
Assignment 3 uses smaller program memory than Assignment 2. Code is more tidy by calling functions.

Prepared by Ober Choo

33

What have you learnt?


Create functions in C. Calling functions in C.

Prepared by Ober Choo

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.

Prepared by Ober Choo

36

Adding lcd.c and lcd.h

Add these files

Prepared by Ober Choo

37

Include lcd.h
Use #include lcd.h in Lab01_main.c

Prepared by Ober Choo

38

Functions to call
Refer in header file lcd.h

Prepared by Ober Choo

39

OK! Lets start!


Continue new code in Lab01_main.c Use /* and */ to comment all previous assignment code development. Add lcd.c and lcd.h to Lab01 project. Include lcd.h in Lab01_main.c file. Call functions for initialize LCD, clear, diplay character, string and also jump. Compile and debug if there is any error. Load it to TB40A and press SW1.
40

Prepared by Ober Choo

Expected Result
Code is more tidy by calling functions. Whole project is more organize.

Prepared by Ober Choo

41

What have you learnt?


Adding library to project. Calling functions from library.

Prepared by Ober Choo

42

Bonus!
Use LCD Backlight for more clearer view of message. To control the back light, make LCD_BACKLIGHT high (1).

Prepared by Ober Choo

43

Thank You
Q&A Please!

Useful links
http://ouwehand.net/~peter/lcd/lcd0.shtml#ins truction_set

Prepared by Ober Choo

45

You might also like