LCD Interfacing with PIC Microcontroller
Diagram Interface |
Interfacing diagram is to interface 16x2 LCD with PIC Microcontroller. The data bus of LCD is connected to Port B of PIC. Enable terminal is connected to RC0 and the RS terminal is connected to RC1. The R/W terminal is connected to ground.
The embedded C program to display a string on both rows of LCD is given. The string will be displayed as shown below:
16X2 LCD
Program to display a string on LCD
/* Program to display a string on LCD */
#include <p18f4550.h>
#define LCD_EN PORTCbits.RC1 // RC1 is used as enable pin
#define LCD_RS PORTCbits.RC0 // RC0 is used as register select pin
// Delay Function
void delay()
{
unsigned int i;
for(i=0;i<5000;i++)
{ }
}
// Global Declaration of strings
unsigned char string1[]={'F','I','R','S','T'};
unsigned char string2[]={'L','A','S','T'};
// Command Function
void SendCommand(unsigned char command)
{
LCD_RS = 0; // RS=0
delay();
LCD_EN = 1; // EN=1
delay();
PORTB = command; // Command is placed on data bus
delay();
LCD_EN = 0; // EN=0
delay();
}
// Data Function
void SendData(unsigned char lcddata)
{
LCD_RS = 1; // RS=0
delay();
LCD_EN = 1; // EN=1
delay();
PORTB = lcddata; // Data is placed on data bus
delay();
LCD_EN = 0; // EN=0
delay();
}
// Main function
void main()
{
unsigned char Key,j,p,P;
TRISB = 0x00; //LCD pins as output
TRISCbits.RC0=0; // enable and RS pin of LCD as output
TRISCbits.RC1=0;
// Initialize LCD
P=0X38; // LCD COMMAND
SendCommand(P); // Called command function
P=0X0E; // LCD COMMAND
SendCommand(P);
P=0X01; // LCD COMMAND
SendCommand(P);
while(1) //Forever loop
{
P=0X80; // LCD COMMAND to display on first line
SendCommand(P);
// loop till j is equal to no. of characters in string 1
for (j=0;j<5 ;j++)
{
p=string1[j]; // get character from string
SendData(p); // send to display on LCD
}
P=0XC0; // LCD COMMAND to display on SECOND LINE
SendCommand(P);
// loop till j is equal to no. of characters in string 2
for (j=0;j< 4 ;j++)
{
p=string2[j]; // get character from string
SendData(p); // send to display on LCD
}
delay( );
}
}