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 nameLCD pin Description
1VssGround pin of the LCD module.
2VccPower to LCD module (+5V supply)
3VEEContrast adjustment pin
4RS

Register select pin
RS=0 command register.
RS=1 data register. 

5R/W

Read/Write modes
R/W=1; Read mode
R/W=1; Write mode

6ENThis pin is meant for enabling the LCD module
7-14DB0 to DB78 data pins
15LED+Anode of the back light LED
16LED-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.InstructionHex
1Function Set: 8-bit mode, 1 Line, 5x7 Dots matrix for each character display0x30
2Function Set: 8-bit mode, 2 Line, 5x7 Dots matrix for each character display0x38
3Function Set: 4-bit mode, 1 Line, 5x7 Dots matrix for each character display0x20
4Function Set: 4-bit mode, 2 Line, 5x7 Dots matrix for each character display0x28
5Clear display screen0x01
6Return Home0x02
7Decrement cursor(Shift to left)0x04
8Increment cursor(Shift to Right)0x06
9Display off Cursor off0x08
10Display on Cursor on0x0E
11Display on Cursor off0x0C
12Display on Cursor blinking0x0F
13Shift entire display left0x18
14Shift entire display right0x1C
15Move cursor left by one character0x10
16Move cursor right by one character0x14
17Clear Display (also clear DDRAM content)0x01
18Force cursor position on  first position of  first 0x80 
19Force cursor position on  first position of  second row 0xC0 

Video Proteus Simulation

Algorithm for Interfacing

Interfacing Diagram

LCD interfacing with pic18

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

#include<p18f4520.h>

#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

#include<p18f4550.h>
void LCD_command(unsigned char value);//Function Prototype declaration
void lcddata(unsigned char value);
void msdelay(unsigned int itime);
void LCD_write_string(static char *str);
#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()
{
unsigned char i;
unsigned char msg1[ ]="SPPU";
unsigned char msg2[ ]="PUNE";
TRISD = 0x00;  //Set direction of PORTD as an output (Data lines of LCD)
ADCON1=0X0F; // CHANNELS OF INTERNAL ADC used as a digital pin
TRISE=0X00;     //set direction of PORTE as an output (Control lines of LCD)
msdelay(50);
LCD_command(0x38);  //8-bit mode, 2 Line, 5x7 Dots matrix for each character display
msdelay(50);
LCD_command(0x0E);  //Display ON, Cursor ON
msdelay(15);
LCD_command(0x01);   //clear Display screen
msdelay(15);
LCD_command(0x06);  //Increment cursor(shift right)
msdelay(15);
LCD_command(0X83);  // Force cursor to blink on first row 1st position
LCD_write_string(msg1); //Display string "SPPU"
msdelay(500);
LCD_command(0XC4); // Force cursor to blink on second row 5th position
LCD_write_string(msg2); //Display string "UNIVERSITY"
msdelay(500);
}
void LCD_command (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 LCD_write_string(static char *str)
{
int i = 0;
while (str[i] != 0)
{
lcddata(str[i]);
msdelay(15);
i++;
}
}
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 k,j;
for(k=0;k<itime;k++)
for(j=0;j<135;j++);
}

References

  • Created and developed by Prof. S.M.Wagh, SKNCOE  Pune
  • Interfacing diagram by Jayesh Gopal, Student, SKNCOE,Pune
  • WikiNote Foundation

Last modified: Thursday, 16 January 2020, 4:02 PM