Sunday, March 20, 2016

Interfacing PIC16F877A with LCD using CCS C compiler


PIC16F877A LCD example
PIC microcontroller 1602 LCD display 
This topic shows a simple example for interfacing LCD display with HD44780 controller with PIC16F877A microcontroller using CCS PIC C compiler. 
In 4-bit mode there are 7 data lines between the PIC microcontroller and the LCD which are: RS, R/W, E, D4, D5, D6 and D7.
Interface PIC16F877A with LCD circuit:
Interface PIC16F877A with LCD circuit CCS
Interface PIC16F877A with LCD CCS C code:

// Interfacing PIC16F877A with LCD display
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

//LCD module connections
#define LCD_RS_PIN PIN_C0
#define LCD_RW_PIN PIN_C1
#define LCD_ENABLE_PIN PIN_C2
#define LCD_DATA4 PIN_C3
#define LCD_DATA5 PIN_C4
#define LCD_DATA6 PIN_C5
#define LCD_DATA7 PIN_C6
//End LCD module connections

#include <16F877A.h>
#use delay(crystal=8000000)
#include <lcd.c>

char i;
void main(){
  lcd_init();                 // Initialize LCD module
  while(TRUE){
    lcd_putc('\f');
    lcd_gotoxy(4, 1);           // Go to column 4 row 1
    lcd_putc("PIC16F877A");
    lcd_gotoxy(4, 2);           // Go to column 4 row 2
    lcd_putc("LCD example");
    delay_ms(5000);
    lcd_putc('\f');             // LCD clear
    lcd_gotoxy(3, 1);           // Go to column 3 row 1
    lcd_putc("Hello world!");
    lcd_gotoxy(2, 2);           // Go to column 2 row 2
    lcd_putc("Have a nice day");
    delay_ms(5000);
    lcd_putc('\f');             // LCD clear
    lcd_gotoxy(3, 1);           // Go to column 3 row 1
    lcd_putc("Hello world!");
    for(i = 0; i < 200; i++){
      lcd_gotoxy(7, 2);           // Go to column 7 row 2
      printf(lcd_putc,"%3u",i);   // Write i with 3 numbers max
      delay_ms(100);}
   }
}

Interface PIC16F877A with LCD video: