LCD (4-bit Mode & 8-bit Mode) Interfacing with PIC18
LCD Interfacing with PIC18FXXX Microcontroller
Introduction to LCD
The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:
LCD Pin Description
LCD pin no. | LCD pin name | LCD pin Description |
---|---|---|
1 | Vss | Ground pin of the LCD module. |
2 | Vcc | Power to LCD module (+5V supply) |
3 | VEE | Contrast adjustment pin |
4 | RS | Register select pin |
5 | R/W | Read/Write modes |
6 | EN | This pin is meant for enabling the LCD module |
7-14 | DB0 to DB7 | 8 data pins |
15 | LED+ | Anode of the back light LED |
16 | LED- | Cathode of the back light LED |
The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register.
Mostly used commands or instructions for LCD | ||
---|---|---|
Sr.No. | Instruction | Hex |
1 | Function Set: 8-bit mode, 1 Line, 5x7 Dots matrix for each character display | 0x30 |
2 | Function Set: 8-bit mode, 2 Line, 5x7 Dots matrix for each character display | 0x38 |
3 | Function Set: 4-bit mode, 1 Line, 5x7 Dots matrix for each character display | 0x20 |
4 | Function Set: 4-bit mode, 2 Line, 5x7 Dots matrix for each character display | 0x28 |
5 | Clear display screen | 0x01 |
6 | Return Home | 0x02 |
7 | Decrement cursor(Shift to left) | 0x04 |
8 | Increment cursor(Shift to Right) | 0x06 |
9 | Display off Cursor off | 0x08 |
10 | Display on Cursor on | 0x0E |
11 | Display on Cursor off | 0x0C |
12 | Display on Cursor blinking | 0x0F |
13 | Shift entire display left | 0x18 |
14 | Shift entire display right | 0x1C |
15 | Move cursor left by one character | 0x10 |
16 | Move cursor right by one character | 0x14 |
17 | Clear Display (also clear DDRAM content) | 0x01 |
18 | Force cursor position on first position of first | 0x80 |
19 | Force cursor position on first position of second row | 0xC0 |
Video Proteus Simulation
Algorithm for Interfacing
Interfacing Diagram
Embedded C Program for the interfacing
Question: Draw and explain interfacing of 16x2 LCD with PIC18FXXX microcontroller in 8-bit mode and also write an Embedded C Program to display word "SK".
#pragma config OSC=HS
#pragma config PWRT=OFF
#pragma config WDT=OFF
#pragma config DEBUG=OFF, LVP=OFF
void lcdcmd(unsigned char value);//Function Prototype declaration
void lcddata(unsigned char value);
void msdelay(unsigned int itime);
#define ldata PORTD //Declare ldata variable for PORTD
#define rs PORTEbits.RE0 //Declare rs variable for pin RE0
#define rw PORTEbits.RE1 //Declare rw variable for pin RE1
#define en PORTEbits.RE2 //Declare en variable for pin RE2
void main()
{
TRISD = 0x00; //Set direction of PORTD as output
ADCON1=0X0F;
TRISE=0X00; //set direction of PORTE as output
msdelay(50);
lcdcmd(0x38); //16x2 LCD
msdelay(50);
lcdcmd(0x0E);
msdelay(15);
lcdcmd(0x01); //clear Display screen
msdelay(15);
lcdcmd(0x06); //Increment cursor and shift right
msdelay(15);
lcdcmd(0x80); //Force cursor on first row first position
lcddata('S'); //Display character 'S'
msdelay(50);
lcddata('K'); //Display character 'K'
msdelay(50);
}
void lcdcmd (unsigned char value)
{
ldata=value; //Send the command value to PORTD
rs=0; //selection of command register of LCD
rw=0;
en=1; //Generate High to Low pulse on Enable pin
msdelay(1);
en=0;
}
void lcddata (unsigned char value)
{
ldata=value; //Send the command value to PORTD
rs=1; //selection of DATA register of LCD
rw=0;
en=1; //Generate High to Low pulse on Enable pin
msdelay(1);
en=0;
}
void msdelay (unsigned int itime)
{
int i,j;
for(i=0;i<itime;i++)
for(j=0;j<135;j++);
}
Embedded C Program using for Loop for String of messages
Question - Draw an interfacing diagram to display 'SPPU' on 4th position in line1 and 'UNIVERSITY' at 5th position of line 2, Write an Embedded C Program
References
- Created and developed by Prof. S.M.Wagh, SKNCOE Pune
- Interfacing diagram by Jayesh Gopal, Student, SKNCOE,Pune
- WikiNote Foundation