Sunday, March 20, 2016

PIC18F4550 interfacing with LCD using CCS PIC C compiler


PIC18F4550 LCD Example
This topic shows a simple example for interfacing LCD display with HD44780 controller with PIC18F4550 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 PIC18F4550 with LCD circuit:
Interface LCD with PIC18F4550 microcontroller using CCS PIC C
PIC18F4550 internal oscillator is used at 8MHz and MCLR pin function is disabled.
The button connected to RC0 used to increment a number displayed in the 2nd row as shown in the video below. 
Interface PIC18F4550 with LCD CCS C code:

// Interfacing PIC18F4550 with LCD display CCS C code
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

//LCD module connections
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_ENABLE_PIN PIN_D2
#define LCD_DATA4 PIN_D3
#define LCD_DATA5 PIN_D4
#define LCD_DATA6 PIN_D5
#define LCD_DATA7 PIN_D6
//End LCD module connections

#include <18F4550.h>
#fuses NOMCLR INTRC_IO
#use delay(clock=8000000)
#include <lcd.c>

char i;
void main(){
  setup_oscillator(OSC_8MHZ);       // Setup internal oscillator @ 8MHz
  lcd_init();                       // Initialize LCD module
  lcd_gotoxy(2, 1);                 // Go to column 2 row 1
  lcd_putc("PIC18F4550");
  lcd_gotoxy(1, 2);                 // Go to column 1 row 2
  lcd_putc("LCD example");
  for(i = 0; i < 2; i++){           // Shift display right 2 times
    delay_ms(200);
    lcd_send_byte(0,0x1E);
    }
  delay_ms(5000);
  for(i = 0; i < 14; i++){           // Shift display right 14 times
    delay_ms(200);
    lcd_send_byte(0,0x1E);
    }
  lcd_putc('\f');                    // LCD clear
  lcd_gotoxy(18, 1);                 // Go to column 18 row 1
  lcd_putc("Hello world!");
  lcd_gotoxy(17, 2);                 // Go to column 17 row 2
  lcd_putc("Have a nice day");
  for(i = 0; i < 15; i++){           // Shift display left 15 times
    delay_ms(200);
    lcd_send_byte(0,0x18);
    }
  delay_ms(5000);
  lcd_putc('\f');                    // LCD clear
  lcd_gotoxy(3, 1);                  // Go to column 3 row 1
  lcd_putc("Hello world!");
  i = 0;
  while(TRUE){
    if(input(PIN_C0) == 0){
      i++;
      if(i > 100)
        i = 0;
      lcd_gotoxy(7, 2);              // Go to column 7 row 2
      printf(lcd_putc,"%3u",i);      // Write i with 3 numbers max
      delay_ms(200);
      }
    }
}

Interface PIC16F877A with LCD video:
The following video shows the example in a hardware circuit.