PIC16f877a 8 Bit mode Interfacing LCD Display using MPlab and Pickit3

Liquid Crystal Display (LCD):

16x2 LCD can be interfaced with microcontroller in 4 Bit or 8 Bit mode. These differs in how data is send to LCD. In 8 bit mode to write a character, 8 bit ASCII data is send through the data lines D0 – D7 and data strobe is given through E of the LCD. LCD commands which are also 8 bit are written to LCD in similar way.

But 4 Bit Mode uses only 4 data lines D4 – D7. In this mode 8 bit character ASCII data and command data are divided into two parts and send sequentially through data lines. The idea of 4 bit communication is used save pins of microcontroller. 4 bit communication is a bit slower than 8 bit communication but this speed difference can be neglected since LCD's are slow speed devices. Thus 4 bit mode data transfer is most commonly used.

Hardware Connection:


16x2 lcd Display in 8bit mode with PIC Microcontroller PIC16f877a with MP Lab
16x2 lcd Display in 8bit mode with PIC16f877a

Program:


/*
 * File:   4_LCDisplay8BitMode.c
 * Author: Udaya Prakash Jayaraman
 * 4_LCDisplay8BitMode
 * Circuitrcuit Connection
 * Vss to Ground
 * Vcc to 5V
 * Vee to potentiometer to adjust brightness if needed
 * RS- Register Select (0-Command mode)(1-data mode)
 * RW- Read/Write, R/W=0: Write & R/W=1: Read
 * EN- Enable. Falling edge triggered
 * D0 to D7 Databit 0 to 7
 * So connect D0 to D7 in Port B0 to B7
 * Connect RS(RC0), RW(RC1 or Gnd), EN(RC2)
 * A- Anode(+)
 * K- Cathode(-)
 *
 *
 * Created on June 3, 2019, 2:23 PM
 */

#define _XTAL_FREQ 8000000

#pragma config FOSC = HS    // Oscillator Selection bits (RC oscillator)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <pic16f877a.h>

/* Configure the data bus and Control bus as per the hardware connection
   Dtatus bus is connected to PB0:PB7 and control bus PD0:PD2*/

#define LcdDataBus      PORTB   //Data Bus
#define LcdControlBus   PORTC    //Control/Command Bus

#define LcdDataBusPins   TRISB
#define LcdCtrlBusPins   TRISC

#define EN RC2    //use EN as 0 or 1 to sent data and command
#define RW RC1    // Use RW as 0 to set in write mode
#define RS RC0    // Register Select (0-Command mode)(1-data mode)

void delay(unsigned int Count)
{
    unsigned int i,j;
    for(i=0;i<Count;i++)
    {
        for(j=0;j<100;j++);
    }
}

void Lcd_CmdWrite(char cmd)
{
    LcdDataBus = cmd;  //cmd is set to PORTB data pins PB0 to PB7
    RS = 0;    // set RS as 1 to configure in command mode
    RW = 0;               // set RW as 0 to configure as Write mode
    EN = 1;               // set EN as 1 to sent 8bit command from PORTB to LCD
    delay(100);        
    EN = 0;                // set EN as 0 after sending command to LCD display
    delay(100);
}
void Lcd_DataWrite(char dat)
{
    LcdDataBus = dat;  // dat is set to PORTB data pins PB0 to PB7
    RS = 1;               // set RS as 0 to configure in data mode
    RW = 0;               // set RW as 0 to configure as Write mode
    EN = 1;               // set EN as 1 to sent 8bit data from PORTB to LCD
    delay(100);        
    EN = 0;                // set EN as 0 after sending data to LCD display
    delay(100);
}

int main()
{
    while(1)
    {
    char i,a[]={"Good Morning"};
 
    LcdDataBusPins = 0x00;
    LcdCtrlBusPins = 0x00;
    TRISC0 = 0;
  
    delay(1000);
 
    Lcd_CmdWrite(0x38);        // enable 8bit mode, 5x7 mode for chars
    Lcd_CmdWrite(0x0E);        // Display OFF, Cursor ON
    Lcd_CmdWrite(0x01);        // Clear Display
    Lcd_CmdWrite(0x80);        // Move the cursor to beginning of first line

    Lcd_DataWrite('H');
    Lcd_DataWrite('i');
    Lcd_DataWrite(' ');
    Lcd_DataWrite('U');
    Lcd_DataWrite('d');
    Lcd_DataWrite('a');
    Lcd_DataWrite('y');
    Lcd_DataWrite('a');
    Lcd_DataWrite('P');
    Lcd_DataWrite('r');
    Lcd_DataWrite('a');
    Lcd_DataWrite('k');
    Lcd_DataWrite('a');
    Lcd_DataWrite('s');
    Lcd_DataWrite('h');
 
        Lcd_CmdWrite(0xc0);        //Go to Next line and display Good Morning
    
  for(i=0;a[i]!=0;i++)
    {
        Lcd_DataWrite(a[i]);
    }
    }
}


Have an opinion, suggestion , question or feedback about the article let it out here!