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);):

CommandDescription
LCD_FIRST_ROWMove cursor to the 1st row
LCD_SECOND_ROWMove cursor to the 2nd row
LCD_THIRD_ROWMove cursor to the 3rd row
LCD_FOURTH_ROWMove cursor to the 4th row
LCD_CLEARClear display
LCD_RETURN_HOMEReturn cursor to home position, returns a shifted display to its original position. Display data RAM is unaffected
LCD_CURSOR_OFFTurn off cursor
LCD_UNDERLINE_ONUnderline cursor on
LCD_BLINK_CURSOR_ONBlink cursor on
LCD_MOVE_CURSOR_LEFTMove cursor left without changing display data RAM
LCD_MOVE_CURSOR_RIGHTMove cursor right without changing display data RAM
LCD_TURN_ONTurn Lcd display on
LCD_TURN_OFFTurn Lcd display off
LCD_SHIFT_LEFTShift display left without changing display data RAM
LCD_SHIFT_RIGHTShift 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

1 thought on “Interfacing LCD with PIC microcontroller | MPLAB Projects”

  1. – Is there any way to have these changes in the LCD library?
    RW connected to pin RD1
    EN connected to pin RD2
    D4 connected to pin RD3
    D5 connected to pin RD4
    D6 connected to pin RD5
    D7 connected to pin RD6
    – Please help me. I need it urgently. Thank you

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top