The display module serves as a fundamental component in microcontroller development, providing a direct interface for interaction with the microcontroller. Today, we will focus on the LCD1602 small screen, which, despite only being able to display simple characters, is inexpensive and practical, making it particularly friendly for beginners. I have prepared several interesting exercises for you to explore this little device with a black background and green text.
Key Hardware Principles
The LCD1602 is a character-type liquid crystal display module. What does 16×2 mean? It means it can display 2 lines of text, with 16 characters per line. This device uses serial communication, similar to sending messages on WeChat, transmitting data one piece at a time. It has several pins, among which the most important are the RS, RW, and E control pins, along with the 8 data pins D0~D7.
// LCD1602 pin definitions
sbit LCD_RS = P2^6;
sbit LCD_RW = P2^5;
sbit LCD_EN = P2^7;
#define LCD_DATA P0
💡 Tip: Be especially careful when wiring; do not reverse the RS and RW pins, or the display will show garbled text. I made this mistake the first time.
Initialization Steps
Initialization is like your morning routine; it needs to be done step by step. The LCD1602 must first set the working mode, clear the screen, and then set the cursor position, among other tasks.
void LCD_Init()
{
LCD_WriteCommand(0x38); // 8-bit data interface, 2-line display, 5×7 dot matrix
LCD_WriteCommand(0x0c); // Turn on display, do not show cursor
LCD_WriteCommand(0x06); // Move cursor right after writing a character
LCD_WriteCommand(0x01); // Clear screen
LCD_WriteCommand(0x80); // Move cursor to the start of the first line
}
Display String Experiment
Let’s start with something simple: display “Hello World” to get a taste of it.
void LCD_ShowString(unsigned char *str)
{
while (*str != '\0')
{
LCD_WriteData(*str++);
delay_ms(1); // Slight delay for more stable display
}
}
// Use it like this in the main function
int main()
{
LCD_Init();
LCD_ShowString("Hello World");
while(1);
return 0;
}
💡 Tip: Remember to add a delay when displaying strings; otherwise, it may not display stably or may show interference.
Moving Display Experiment
Want to make the string run on the screen? Try this:
void RunningDisplay()
{
unsigned char i;
LCD_WriteCommand(0x80 + 0x40); // Move to the second line
LCD_ShowString("I love MCU!");
while(1)
{
for(i=0; i<16; i++)
{
LCD_WriteCommand(0x18); // Shift all display left
delay_ms(300);
}
}
}
Custom Character Display
The LCD1602 can also display custom characters, like a smiley face, which is fun. These characters are stored in CGRAM, allowing you to design your desired patterns.
// Smiley face pattern data
unsigned char smile[] = {
0x00,0x0A,0x0A,0x00,0x11,0x0E,0x00,0x00
};
void Custom_Char()
{
unsigned char i;
LCD_WriteCommand(0x40); // Set CGRAM address
for(i=0; i<8; i++)
{
LCD_WriteData(smile[i]);
}
LCD_WriteCommand(0x80);
LCD_WriteData(0x00); // Display custom character
}
Common Problem Solutions
Although the LCD1602 is simple, there are some pitfalls to watch out for:
- Garbled display is usually due to poor data line connections; check the wiring.
- If it doesn’t display, the contrast potentiometer may not be adjusted properly.
- If the display is unclear, it may be due to insufficient delay time. I have tested all these code examples; they should work right away. The key to microcontrollers is practice. When I first played with this device, I faced various difficulties, but looking back, it was quite interesting. You can use these codes directly; just modify the pin definitions, connect the lines, and you will see the results. Completing all these experiments will give you a solid introduction to the basic operations of the LCD1602. Later, displaying temperature, time, and other data will be a breeze.