Please click above freefollow…
This lecture will introduce the microcontroller driving the LCD1602 liquid crystal display module. Through this lecture, readers can master the working principle of the LCD1602 and how to drive the LCD1602 with a microcontroller.
1. Introduction to the Principle
The liquid crystal display module has advantages such as small size, low power consumption, rich display content, and ultra-thin lightweight, and is increasingly used in embedded application systems. The LCD1602 liquid crystal display module introduced in this lecture (with an internal controller HD44780 chip) can display two lines, with 16 characters per line, equivalent to 32 LED digital tubes, and even more information than digital tube displays. It uses a single +5V power supply, has a simple peripheral circuit configuration, is inexpensive, and has a high cost-performance ratio.
The appearance of the LCD1602 is shown in Figure 1 (a). The pin distribution of the 1602 character LCD module is shown in Figure 1 (b), with the functions of its pins listed in Table 1.
Figure 1 (a) Appearance of LCD1602 (b) Pin Distribution of LCD1602
It is worth mentioning that various LCD manufacturers provide almost the same specifications of the 1602 module or compatible modules, although each manufacturer has different names for their products; however, the initially used LCD controller is the HD44780, and among the 1602 modules produced by various manufacturers, they basically use compatible control ICs, so their characteristics are basically the same; of course, many manufacturers offer different character colors, backlight colors, and other display modules.
The LCD1602 has 11 control instructions, as shown in Table 2. Among them, DDRAM: display data RAM, used to store character codes to be displayed; CGROM: character generation ROM; CGRAM: user-defined character graphics RAM. Due to space constraints, this will not be detailed here; readers can check the HD44780 chip documentation and the LCD1602 datasheet online.
Here are several commands frequently used in programming the LCD1602, as shown in Table 3.
2. Detailed Circuit Explanation
As shown in Figure 2, the control pins of the 1602 LCD are connected to the pins of the microcontroller. As mentioned earlier, in the function setting instructions, the LCD can be set to an 8-bit data interface or a 4-bit data interface. The figure uses an 8-bit data interface, but it can also be used as a 4-bit data interface. The positive terminal of the LCD power supply is connected to 5V, the negative terminal is connected to ground, and the positive terminal of the backlight is connected to 5V, while the negative terminal is connected to ground. In addition, the bias pin (VO) of the LCD is connected to the middle tap of a potentiometer, with the two ends of the potentiometer connected to 5V and ground, allowing adjustment of the contrast of the LCD1602 by adjusting the potentiometer. Experimental tests show that the voltage of the bias pin of the LCD1602 should be adjusted to 0.3~0.4V for the best contrast effect, and this pin can also be pulled down to ground through a 1k resistor. Readers can practice testing themselves.
Figure 2 Connection Diagram of 1602 LCD and Microcontroller
3. Program Design
Before designing the program for the 1602 LCD, let’s take a look at its read and write timing diagrams. From Figures 3 and 4, the read and write operations of the 1602 LCD can be summarized in Table 4.
Figure 3 Timing Diagram of Read Operation of 1602 LCD
Figure 4 Timing Diagram of Write Operation of 1602 LCD
By comparing the timing diagrams, we can easily write the various sub-functions to drive the LCD, as follows.
Command Write Function: Write Control Word to Register
Parameter: command – Control word
void LCD_en_command(unsigned char command)
{
LCD_delay(); // Delay for a while
LCD1602_RW=LOW; // When writing a command, R/W is low
LCD1602_RS=LOW; // When writing an instruction, RS is low
LCD1602_EN=HIGH; // Set the enable line high
LCDIO=command; // Receive command word and send to data bus
LCD1602_EN=LOW; // Write data on the falling edge of the enable line
}
Data Write Function: Write Data to Register
Parameter: dat – Data to be displayed
void LCD_en_dat(unsigned char dat)
{
LCD_delay(); // Delay for a while
LCD1602_RW=LOW; // When writing instructions, R/W is low
LCD1602_RS=HIGH; // When writing data, RS is high
LCD1602_EN=HIGH; // Set the enable line high
LCDIO=dat; // Receive data and send to data bus
LCD1602_EN=LOW; // Write data on the falling edge of the enable line
}
To ensure that the 1602 LCD works normally and is easy to operate, in addition to writing data and command functions, initialization and other operations are also required. The main functions used to operate the 1602 LCD in this lecture are designed as follows.
LCD Initialization Function: Set the Working Mode of the LCD
void LCD_init(void)
{
delaylcd_nms(5);
LCD_en_com_nobusy(DATA_MODE);
// Delay 5ms, write instruction, do not check busy signal
delaylcd_nms(5);
LCD_en_com_nobusy(DATA_MODE);
// Delay 5ms, write instruction, do not check busy signal
delaylcd_nms(5);
LCD_en_com_nobusy(DATA_MODE);
// Delay 5ms, write instruction, do not check busy signal
LCD_en_com(DATA_MODE);
// Set to 8-bit data transfer mode
delaylcd_nms(5);
LCD_en_com(OPEN_SCREEN); // Open display
LCD_en_com(DISPLAY_ADDRESS); // Set display address
CLEARSCREEN; // Clear screen
}
Set Display Coordinates Function: Set Character Display Position, Row, and Column Coordinates
Parameter: x – Column coordinate, y – Row coordinate
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == LINE1) // Determine row
address = LINE1_HEAD + x; // Set column coordinate
else
address = LINE2_HEAD + x;
LCD_en_command(address); // Write address to register
}
Write Character Function: Write a Character to the LCD
Parameter: x – Column coordinate, y – Row coordinate, dat – Character data to be displayed
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy( x, y ); // First set display coordinates
LCD_en_dat(dat); // Write data
}
Write String Function: Write a String to the LCD
Parameter: x – Column coordinate, y – Row coordinate, *s – String data to be displayed
void LCD_write_string(unsigned char x,unsigned char y,unsigned
char *s)
{
LCD_set_xy( x, y ); // First set display coordinates
while (*s) // Check if it is finished
{
LCDIO=*s;// Loop call write data function
LCD_en_dat(*s);
s ++;
}
}
4. Debugging Points and Experimental Phenomena
Connect the hardware properly (especially connect the 1602 LCD), download the generated .hex file of the program to the microcontroller through cold boot, reset the microcontroller, and then you can see the characters displayed on the LCD on the learning board (as shown in Figure 5).
Figure 5 LCD1602 Display Interface
During debugging, it should be noted that the internal display operations of the LCD require a certain amount of time. Therefore, if the microcontroller does not perform read-busy operations when writing to the LCD1602, it should delay for enough time (using a delay function) to allow the LCD to receive commands or data; otherwise, the LCD may not work or work abnormally. In addition, for the V0 pin, as the contrast adjustment pin for the LCD display, the contrast is weakest when connected to the positive power supply and highest when connected to ground. When the contrast is too high, “ghosting” may occur. Therefore, a potentiometer can be used to adjust its voltage, as mentioned earlier, adjusting it to 0.3~0.4V gives the best contrast effect.
5. Conclusion
This lecture introduced the working principle of the character LCD1602 and provided examples. Through this lecture, readers can understand and master the method of driving the LCD1602 with the 51 microcontroller.
This article is reproduced from the internet, copyright belongs to the original author. If you think it is not good, please contact us to delete it!
If you think it’s good, please click below to see👇👇👇👇👇👇
Leave a Comment
Your email address will not be published. Required fields are marked *