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
Interfacing LCD with PIC microcontroller circuit:
Example circuit schematic diagram is shown below.
(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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //LCD module connections #define LCD_RS RD0 #define LCD_EN RD1 #define LCD_D4 RD2 #define LCD_D5 RD3 #define LCD_D6 RD4 #define LCD_D7 RD5 #define LCD_RS_DIR TRISD0 #define LCD_EN_DIR TRISD1 #define LCD_D4_DIR TRISD2 #define LCD_D5_DIR TRISD3 #define LCD_D6_DIR TRISD4 #define LCD_D7_DIR TRISD5 //End LCD module connections |
Interfacing LCD with PIC microcontroller MPLAB XC8 code:
The microcontroller used in this example is PIC16F887, configuration words are:
1 2 | #pragma config CONFIG1 = 0x2CD4 #pragma config CONFIG2 = 0x0700 |
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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | /* * Interfacing PIC16F887 microcontroller with LCD screen * C Code for MPLAB XC8 compiler * Internal oscillator used @ 8MHz * This is a free software with NO WARRANTY * http://simple-circuit.com/ */ #pragma config CONFIG1 = 0x2CD4 #pragma config CONFIG2 = 0x0700 //LCD module connections #define LCD_RS RD0 #define LCD_EN RD1 #define LCD_D4 RD2 #define LCD_D5 RD3 #define LCD_D6 RD4 #define LCD_D7 RD5 #define LCD_RS_DIR TRISD0 #define LCD_EN_DIR TRISD1 #define LCD_D4_DIR TRISD2 #define LCD_D5_DIR TRISD3 #define LCD_D6_DIR TRISD4 #define LCD_D7_DIR TRISD5 //End LCD module connections #include <xc.h> #define _XTAL_FREQ 8000000 #include <stdio.h> // for sprintf #include "LCD_Lib.c" // include LCD driver source file char i = 0, text[4]; void main(void) { OSCCON = 0X70; // set internal oscillator to 8MHz LCD_Begin(); // initialize LCD module LCD_Goto(4, 1); // go to column 4, row 1 LCD_Print("MPLAB XC8"); LCD_Goto(3, 2); // go to column 3, row 2 LCD_Print("LCD Example"); __delay_ms(5000); // wait 5 seconds LCD_Cmd(LCD_CLEAR); // clear the whole screen LCD_Goto(3, 1); // go to column 3, row 1 LCD_Print("Hello world!"); while (1) { sprintf(text, "%03u", i); LCD_Goto(7, 2); // go to column 7, row 2 LCD_Print(text); i++; // increment i __delay_ms(500); // wait 1/2 second } } // End of code. |
Discover more from Simple Circuit
Subscribe to get the latest posts sent to your email.
– 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