LCD Interfacing with PIC18

Updated on 2017/11/20 12:41

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:

JHD162A-LCD-module.png

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

ALP 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++);
}



References

  • Created and developed by Prof. S.M.Wagh, SKNCOE  Pune
  • Interfacing diagram by Jayesh Gopal, Student, SKNCOE,Pune
  • WikiNote Foundation
Tags:
Created by Vishal E on 2017/09/15 14:26