Interfacing LCD with PIC microcontroller | MPLAB Projects

this post shows how to interface PIC16F887 microcontroller with 16×2 LCD screen (with HD44780 controller), the compiler used in this example is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler).
The 16×2 LCD screen has 2 rows and 16 columns which means we can write up to 32 character. There are other screens with the HD44780 controller such as: 16×1, 20×4 …

Hardware Required:

  • PIC16F887 microcontroller
  • 1602 (16×2) or 2004 (20×4) or any LCD compatible with HD44780 controller
  • 10k ohm variable resistor or potentiometer
  • 330 ohm resistor
  • 5V Power source
  • Breadboard
  • Jumper wires

PIC16F887 with LCD display - MPLAB XC8 LCD

Interfacing LCD with PIC microcontroller circuit:
Example circuit schematic diagram is shown below.

PIC microcontroller with 1602 LCD display circuit - PIC16F887 LCD

(All grounded terminal are connected together)

The 16×2 LCD screen is connected to the PIC16F887 microcontroller as follows:
RS —> RD0 pin
E  —> RD1 pin
D4 —> RD2 pin
D5 —> RD3 pin
D6 —> RD4 pin
D7 —> RD5 pin
VSS, RW, D0, D1, D2, D3 and K are connected to circuit GND (ground)
VEE to the variable resistor (or potentiometer) output pin
VDD to +5V and A to +5V through 330 ohm resistor

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In this project the PIC16F887 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

Interfacing LCD with PIC microcontroller C code:
The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05

To be able to compile the C code, a small LCD library for MPLAB XC8 compiler is required which can be downloaded from the following link:
MPLAB XC8 LCD Library

after the download, add the library file (LCD_Lib.c) to project folder.

MPLAB LCD library user functions:
LCD_Begin();  // must be called before any other function, it initializes the LCD module.
LCD_Goto(uint8_t col, uint8_t row);  // set write position on LCD (upper left is 1, 1 and second row first position is 1, 2)
LCD_PutC(char LCD_Char);    // prints a character (LCD_Char) on the LCD
LCD_Print(char* LCD_Str);    // prints a string (LCD_Str) on the LCD
LCD_Cmd(uint8_t Command);  // send a command to the LCD

The following commands can be used with LCD_Com function (example: LCD_Com(LCD_CLEAR);):

Command Description
LCD_FIRST_ROW Move cursor to the 1st row
LCD_SECOND_ROW Move cursor to the 2nd row
LCD_THIRD_ROW Move cursor to the 3rd row
LCD_FOURTH_ROW Move cursor to the 4th row
LCD_CLEAR Clear display
LCD_RETURN_HOME Return cursor to home position, returns a shifted display to its original position. Display data RAM is unaffected
LCD_CURSOR_OFF Turn off cursor
LCD_UNDERLINE_ON Underline cursor on
LCD_BLINK_CURSOR_ON Blink cursor on
LCD_MOVE_CURSOR_LEFT Move cursor left without changing display data RAM
LCD_MOVE_CURSOR_RIGHT Move cursor right without changing display data RAM
LCD_TURN_ON Turn Lcd display on
LCD_TURN_OFF Turn Lcd display off
LCD_SHIFT_LEFT Shift display left without changing display data RAM
LCD_SHIFT_RIGHT Shift display right without changing display data RAM

The connection between the PIC microcontroller and the LCD module is defined as follows (example):

Interfacing LCD with PIC microcontroller MPLAB XC8 code:
The microcontroller used in this example is PIC16F887, configuration words are:

Where:

  • In-Circuit Debugger disabled
  • Low voltage programming disabled
  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • Brown-out Reset (BOR) disabled
  • Data memory code protection disabled
  • Program memory code protection disabled
  • RE3/MCLR pin function is digital input, MCLR internally tied to VDD
  • Power-up Timer (PWRT) disabled
  • Watchdog Timer (WDT) disabled
  • INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
  • Flash Program Memory Self Write disabled
  • Brown-out Reset set to 4.0V

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.