LCD interfacing with PIC16F877A ( Proteus Simulation file available)

This PIC16F877 microcontroller tutorial answers the question, 
"How to interface LCD[1]  in 8bit mode with PIC16F877" ?

Also, using PIC16 simulator (Proteus) you can verify this LCD code and change it according to your needs. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the 'Downloadssection at the bottom of this page.

It is assumed that you know how to blink an LED with PIC16F877 microcontroller. If you don't then please read this page first, before proceeding with this article.

The following diagram (made in Proteus) shows the PIC microcontroller circuit diagram.

In the above figure, RD0 pin is being used as Enable pin for LCD. RD1 pin is used as RS pin and PORTB is used as Data bus for the LCD. When code starts running then 'Hello World!' is displayed on the LCD.

Code

The code for the main function is shown below.
In the main function, firstly LCD is initialized using InitLCD() function. After that, LCD screen is cleared usingClearLCDScreen() function. Then 'Hello World!' is written on the LCD screen[2]. In this way using WriteStringToLCD() function, you can write any string on the LCD screen.

In the code you can easily select pins to be used for interfacing with the LCD. Following figure shows the pin selection code.

// Define Pins#define LCD_E     RD0
// Enable pin for LCD#define LCD_RS RD1  
// RS pin for LCD#define LCD_Data_Bus PORTB 
// Data bus for LCD
// Define Pins direction registrers
#define LCD_E_Dir     TRISD0
#define LCD_RS_Dir   TRISD1
#define LCD_Data_Bus_Dir TRISB

Here for example, you can change RD0 to RD3 if you want to attach Enable pin of the LCD on pin22 of PIC16F877. You will also need to change LCD_E_Dir to TRISD3 for RD3.

We can write data or command to the LCD. Following code shows the functions required for this.
void WriteCommandToLCD(unsigned char Command)  { LCD_RS = 0;                              // It is a command
LCD_Data_Bus = Command;   // Write Command value on data bus
ToggleEpinOfLCD();}void WriteDataToLCD(char LCDChar)  
{ 
LCD_RS = 1;                            // It is data 
LCD_Data_Bus = LCDChar;   // Write Data value on data busToggleEpinOfLCD();}

WriteCommandToLCD() function writes a command to LCD in 8bit mode. WriteDataToLCD() function writes any given character on the LCD. For example, to write 'a' on LCD, simply call WriteDataToLCD('a'); in your main function.

LCD initialization function is shown below.

void InitLCD(void) // Firstly make all pins output LCD_E         = 0;      // E = 0 LCD_RS          = 0;      
// D = 0 LCD_Data_Bus     = 0;      
// CLK = 0 LCD_E_Dir        = 0;      
// Make Output LCD_RS_Dir       = 0;      
// Make Output LCD_Data_Bus_Dir = 0;      
// Make Output ///////////////// Reset process from datasheet //////////////   
__delay_ms(40);   WriteCommandToLCD(0x30);__delay_ms(6);   WriteCommandToLCD(0x30);__delay_us(300);   WriteCommandToLCD(0x30);__delay_ms(2);  
////////////////////////////////////////////////////////////////////////////////////////  
 WriteCommandToLCD(0x38);    //function set   
WriteCommandToLCD(0x0c);    //display on,cursor off,blink off   
WriteCommandToLCD(0x01);    //clear display   
WriteCommandToLCD(0x06);    //entry mode, set increment
}

InitLCD() function (shown above) is initializing LCD in 8bit mode with cursor off, blink off and entry mode set to increment. This initialization process is written according to the datasheet of HD44780U controller, which is built in the LCD[1].

You can leave your comments in the comment section below

Notes and References

[1]  Here LCD means most widely used common 16x2 LCD, which has HD44780U controller in it.
[2]  As shown in Figure 1, Hello World! is displayed in the Proteus simulation.

Downloads

LCD interfacing code using PIC16F877a was compiled in MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. 
To download code and Proteus simulation Click Here .

Comments

Popular Posts