
This blog post provides a comprehensive guide on interfacing a 16x2 LCD display with the PIC 18F microcontroller, covering the necessary components, wiring, commands, and a sample program to display text on the LCD.
Welcome back to the channel! In this post, we will explore the topic of LCD interfacing with the PIC 18F microcontroller. This is a crucial topic for students, especially from an exam perspective, where you may be asked to draw the interfacing diagram and write a specific program.
The interfacing diagram we will discuss involves a 16x2 line LCD display, which is commonly used in various applications. The diagram includes a potentiometer, data lines, and control pins connected to the microcontroller.
The LCD display is connected to the PIC 18F microcontroller as follows:
RS (Register Select): This pin determines whether the command register or data register is being accessed.
RW (Read/Write): This pin indicates the operation mode.
E (Enable): This pin is used to latch the data into the command or data register. A high-to-low transition on this pin is required to latch the data.
Before we dive into coding, it's essential to understand how to write cursor addresses and the corresponding LCD commands. Below is a table of commands for a 16x2 LCD display:
| Position | Command (Hex) |
|---|---|
| Line 1 Start | 0x80 |
| Line 1 Position 4 | 0x84 |
| Line 2 Start | 0xC0 |
| Line 2 Position 5 | 0xC5 |
To effectively communicate with the LCD, we need to create subroutines for sending commands and data:
This subroutine sends commands to the LCD:
This subroutine sends data to the LCD:
Now, let's look at a sample program to display the words "My" at the fourth position of line one and "Book" at the fifth position of line two.
#include <xc.h>
#define LCD_DATA_PORT PORTB
#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define E PORTCbits.RC2
void LCD_Command(unsigned char cmd) {
LCD_DATA_PORT = cmd;
RS = 0; // Command register
RW = 0; // Write mode
E = 1; // Enable high
__delay_ms(1);
E = 0; // Enable low
}
void LCD_Data(unsigned char data) {
LCD_DATA_PORT = data;
RS = 1; // Data register
RW = 0; // Write mode
E = 1; // Enable high
__delay_ms(1);
E = 0; // Enable low
}
void main() {
TRISB = 0; // Set PORTB as output
TRISC = 0; // Set PORTC as output
LCD_Command(0x38); // Initialize LCD
LCD_Command(0x0C); // Display on, cursor off
LCD_Command(0x01); // Clear display
LCD_Command(0x84); // Move cursor to line 1 position 4
LCD_Data('M');
LCD_Data('y');
LCD_Command(0xC4); // Move cursor to line 2 position 5
LCD_Data('B');
LCD_Data('o');
LCD_Data('o');
LCD_Data('k');
}
LCD_Command and LCD_Data functions handle sending commands and data to the LCD.main function, we initialize the LCD and display the desired text.In this blog post, we covered the essentials of interfacing a 16x2 LCD display with the PIC 18F microcontroller. We discussed the wiring, control pins, commands, and provided a sample program to help you get started. With this knowledge, you should be able to write your own programs for LCD interfacing. Thank you for reading!
Paste a YouTube link and let Magica create the key takeaways.
Summarize another video