PIC16F877A 8 bit LCD Code

PIC16F877A LCD Code

This post provides the code to make an 16*2 LCD  Interfacing with  PIC16F877A microcontroller. This code is written in C language using MPLAB X IDE with XC8 compiler. 

LCD pin descriptions:

The LCD discussed in this section has 16 pins, The function of each is described below.

VCC VSS and VEE :

       While Vcc and Vss provide +5 volts and ground, respectively Vee is used for controlling the LCD contrast.

RS, Register select :

  • There are two Important register inside the LCD
  • The RS pin is used for selection of the registers
  • If RS=0, Instruction Command register is selected, allowing the user to send command to LCD
  • IF RS=1, Data register is selected, allowing the user to send data to LCD


R/W, read/write :

R/W input allows the user to write information to the LCD or read the information from the LCD.

  • R/W = 1 when reading 
  • R/W =0 when writing

E, Enable :

  • The Enable pin is used by the LCD to latch the information presented to its pins.
  • When data is supplied to data pins,a high to low pulse must be applied to the En pin in order for the LD to latch the information present at the data pins.
  • The pulse must be a minimum of  450 ns wide

D0-D7 :

  • The 8 bit data pins, D0- D7 are used to send the information to the LCD or read the contents of the LCD's



Code:

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

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

// CONFIG
#pragma config FOSC = HS   // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF  // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON     /* 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)


/*define clock freq*/

#ifndef _XTAL_FREQ
  #define _XTAL_FREQ 20000000  // 20MHZ crystal
#endif

#define LCD_Data PORTD              // PORTD for LCD Data PINS D0 to D7
#define RS PORTBbits.RB0    // LCD RS
#define RW PORTBbits.RB1  //LCD RW
#define EN PORTBbits.RB2  //LCD EN

void LCD_DATA(unsigned char DATA);
void LCD_CMD( unsigned char CMD);
void LCD_RESET();
void DisplayString(unsigned char *string);

void main()
{
    TRISD = 0x00; // Make PORTD as Output
    TRISB = 0x00; // Make PORTB as Output
    LCD_RESET(); // LCD RESET PROCESS
    DisplayString("Hello World");
    while (1);

}

void LCD_CMD( unsigned char CMD)
{
    LCD_Data=CMD;   // Put the values on the pins
    RS=0;
    RW=0;
    EN=1;  //Strobe the Enable pin
    __delay_ms(1);
    EN=0;
}

void LCD_DATA(unsigned char DATA)
{
    LCD_Data=DATA;   // Put the values on the pins
    RS=1;
    RW=0;
    EN=1;   //Strobe the Enable pin
    __delay_ms(1);
    EN=0;
}

void LCD_RESET()
{
   EN=0;
    __delay_ms(250);
    LCD_CMD(0x38); // LCD Command for 2 Lines and 5*7 matrix
    __delay_ms(250);
    LCD_CMD(0x0E); // Display ON, Cursor Blink
    __delay_ms(15);
    LCD_CMD(0x01); // Clear display
    __delay_ms(15);
    LCD_CMD(0X06); // Increment Cursor (Shift Cursor to right)
    __delay_ms(15);
    LCD_CMD(0x80); // Display OFF Cursor OFF
    __delay_ms(15);

}

void DisplayString(unsigned char *string)
{
    while(*string)
    {
      LCD_DATA(*string++);
    }
}



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




Figure 1 : PIC16F877A LCD Interfacing circuit 8 bit mode


















Comments

Popular posts from this blog

555 Timer Astable mode, Lt spice simulation, and real-time testing

Exploring USB Device Detection with Arduino and USB Host Shield

PIC12F675 LED Blinking Code