Table of Contents
Interfacing LCD displays with PIC microcontrollers is a fundamental skill in embedded systems development. It allows you to display information, menus, and data to users, making your projects more interactive and user-friendly. This tutorial provides a step-by-step guide to help you connect and program an LCD display with a PIC microcontroller.
Understanding the Components
Before starting, ensure you have the following components:
- PIC Microcontroller (e.g., PIC16F877A)
- 16×2 LCD Display
- Resistors (220Ω for backlight, 10kΩ for contrast)
- Potentiometer (10kΩ for contrast adjustment)
- Connecting wires
- Breadboard
Wiring the LCD to the PIC Microcontroller
Connect the LCD to the PIC as follows:
- VSS to GND
- VCC to +5V
- V0 (contrast) to the middle pin of the potentiometer
- RS to RC0
- RW to GND
- EN to RC1
- D4 to RC2
- D5 to RC3
- D6 to RC4
- D7 to RC5
- Backlight + to +5V through resistor
- Backlight – to GND
Programming the PIC Microcontroller
Use MPLAB X IDE and XC8 compiler to write your code. Initialize the LCD and send commands to display messages.
Sample Code Snippet
Below is a simple example in C:
#include
#define _XTAL_FREQ 20000000
// Initialize LCD pins
#define RS PORTCbits.RC0
#define EN PORTCbits.RC1
#define D4 PORTCbits.RC2
#define D5 PORTCbits.RC3
#define D6 PORTCbits.RC4
#define D7 PORTCbits.RC5
void delay_ms(unsigned int ms) {
while(ms--) {
__delay_ms(1);
}
}
void LCD_Command(unsigned char cmd) {
// Send high nibble
D4 = (cmd & 0x10) >> 4;
D5 = (cmd & 0x20) >> 5;
D6 = (cmd & 0x40) >> 6;
D7 = (cmd & 0x80) >> 7;
RS = 0;
EN = 1;
__delay_ms(1);
EN = 0;
// Send low nibble
D4 = (cmd & 0x01);
D5 = (cmd & 0x02) >> 1;
D6 = (cmd & 0x04) >> 2;
D7 = (cmd & 0x08) >> 3;
EN = 1;
__delay_ms(1);
EN = 0;
}
void LCD_Init() {
delay_ms(20);
LCD_Command(0x02); // Initialize LCD in 4-bit mode
LCD_Command(0x28); // 2 lines, 5x8 matrix
LCD_Command(0x0C); // Display ON, Cursor OFF
LCD_Command(0x06); // Entry mode
LCD_Command(0x01); // Clear display
delay_ms(2);
}
void LCD_WriteChar(char data) {
// Send high nibble
D4 = (data & 0x10) >> 4;
D5 = (data & 0x20) >> 5;
D6 = (data & 0x40) >> 6;
D7 = (data & 0x80) >> 7;
RS = 1;
EN = 1;
__delay_ms(1);
EN = 0;
// Send low nibble
D4 = (data & 0x01);
D5 = (data & 0x02) >> 1;
D6 = (data & 0x04) >> 2;
D7 = (data & 0x08) >> 3;
EN = 1;
__delay_ms(1);
EN = 0;
}
void LCD_SetCursor(unsigned char row, unsigned char col) {
unsigned char address;
if(row == 0)
address = 0x80 + col;
else
address = 0xC0 + col;
LCD_Command(address);
}
int main() {
TRISC = 0x00; // Set PORTC as output
LCD_Init();
LCD_SetCursor(0, 0);
char message[] = "Hello, LCD!";
for(int i = 0; message[i] != '\0'; i++) {
LCD_WriteChar(message[i]);
}
while(1);
return 0;
}
Testing and Troubleshooting
Power your circuit and upload the code. The LCD should display “Hello, LCD!” on the first line. If nothing appears, check your wiring, contrast setting, and ensure your code matches your hardware connections. Use serial debugging or LEDs to verify code execution.
Conclusion
Interfacing an LCD with a PIC microcontroller is straightforward once you understand the wiring and initialization process. Practice with different messages and configurations to enhance your embedded systems skills. With this foundation, you can develop more complex user interfaces for your projects.