Mastering the LCD1602 Display Module with 51 Microcontroller

Mastering the LCD1602 Display Module with 51 Microcontroller

This lecture will introduce how to drive the LCD1602 display module with a microcontroller. Through this lecture, readers can master the working principle of the LCD1602 and how to drive it using a microcontroller.

1. Introduction to the Principle

The LCD display module has advantages such as small size, low power consumption, rich display content, and ultra-thin light weight, making it increasingly popular in embedded application systems. This lecture introduces the LCD1602 display module (with an internal controller of the HD44780 chip), which can display two lines with 16 characters each, thus equivalent to 32 LED digital tubes, and can display more information than digital tubes. It is powered by 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), and the functions of its pins are listed in Table 1.

Mastering the LCD1602 Display Module with 51 Microcontroller

Figure 1 (a) Appearance of LCD1602 (b) Pin distribution of LCD1602

Mastering the LCD1602 Display Module with 51 Microcontroller

It is worth noting that various LCD manufacturers provide almost the same specifications for the 1602 module or compatible modules, although they may have different product names. However, the initially used LCD controller is the HD44780, and most manufacturers’ 1602 modules basically use compatible control ICs, so their characteristics are essentially the same. Of course, many manufacturers offer different character colors, backlight colors, and other display modules.

The LCD1602 has 11 control instructions, see Table 2. Among them, DDRAM: Display Data RAM, used to store character codes to be displayed; CGROM: Character Generator ROM; CGRAM: User-defined Character Graphics RAM. Due to space limitations, we will not discuss it in detail here; readers can check the HD44780 chip information and the LCD1602 data manual online.

Mastering the LCD1602 Display Module with 51 Microcontroller

Here are a few instructions frequently used when programming the LCD1602, see Table 3.

Mastering the LCD1602 Display Module with 51 Microcontroller

2. Circuit Details

As shown in Figure 2, the control pins of the 1602 LCD are connected to the microcontroller pins. 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 grounded, the positive terminal of the backlight is connected to 5V, and the negative terminal is grounded. In addition, the LCD’s bias pin (VO) is connected to the middle tap of a potentiometer, with both ends connected to 5V and ground, allowing the contrast of the LCD1602 to be adjusted by adjusting the potentiometer. Experimental tests show that the voltage adjustment of the bias pin of the LCD1602 is best at 0.3~0.4V, and it can also be pulled down to ground through a 1k resistor. Readers can test this themselves.

Mastering the LCD1602 Display Module with 51 Microcontroller

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, we can summarize the read and write operation timing of the 1602 LCD as shown in Table 4.

Mastering the LCD1602 Display Module with 51 Microcontroller

Figure 3 Timing diagram of the read operation of the 1602 LCD

Mastering the LCD1602 Display Module with 51 Microcontroller

Figure 4 Timing diagram of the write operation of the 1602 LCD

Mastering the LCD1602 Display Module with 51 Microcontroller

By referring to the timing, we can easily write the various sub-functions to drive the LCD as follows.

Command writing function: Write control words to the register

Parameter: command – control word

void LCD_en_command(unsigned char command)

{

LCD_delay(); // Delay for a while

LCD1602_RW=LOW; // When writing commands, R/W is low

LCD1602_RS=LOW; // Write instruction, RS is low

LCD1602_EN=HIGH; // Set high enable line

LCDIO=command; // Receive command word and send to data bus

LCD1602_EN=LOW; // Write data on the falling edge of the enable line

}

Data writing function: Write data to the 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; // Write data, RS is high

LCD1602_EN=HIGH; // Set high enable line

LCDIO=dat; // Receive data and send to data bus

LCD1602_EN=LOW; // Write data on the falling edge of the enable line

}

To ensure the normal operation and convenience of the 1602 LCD, in addition to the data and command writing 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); // Turn on display

LCD_en_com(DISPLAY_ADDRESS); // Set display address

CLEARSCREEN; // Clear screen

}

Set display coordinates function: Set the display position of the character, 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 the row

address = LINE1_HEAD + x; // Set column coordinate

else

address = LINE2_HEAD + x;

LCD_en_command(address); // Write address to the 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 ends

{

LCDIO=*s; // Loop call write data function

LCD_en_dat(*s);

s++;

}

}

4. Debugging Points and Experimental Phenomena

Connect the hardware properly (especially the connection of the 1602 LCD). After downloading the generated .hex file to the microcontroller via cold start, reset the microcontroller, and you can see the displayed characters on the learning board (as shown in Figure 5).

Mastering the LCD1602 Display Module with 51 Microcontroller

Figure 5 LCD1602 display interface

During debugging, it should be noted that because the internal display operation of the LCD requires a certain amount of time, if the microcontroller does not perform a read-busy operation during the write operation to the 1602 LCD, it should delay enough time (using a delay function) to allow the LCD to receive commands or data; otherwise, the LCD will not work or may work improperly. In addition, for the V0 pin, as the contrast adjustment pin of the LCD display, the contrast is weakest when connected to the positive power supply and highest when grounded. When the contrast is too high, it will produce “ghosting”; thus, a potentiometer can be used to adjust its voltage. As mentioned earlier, adjusting it to 0.3~0.4V yields 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.

Mastering the LCD1602 Display Module with 51 Microcontroller

Mastering the LCD1602 Display Module with 51 Microcontroller

Leave a Comment