The LCD display module is widely used in various instruments and low-power systems due to its advantages such as low power consumption, small size, rich display content, and ultra-thin lightweight.
According to the display content, it can be divided into character LCDs and graphic LCDs. Based on display capacity, it can be divided into single line 16 characters, 2 lines 16 characters, two lines 20 characters, etc.
This article introduces the usage method of the commonly used 16×2 character LCD module. This is a universal module. Compared with digital tubes, this module has the following advantages:
1. More digits, can display 32 digits, while 32 digital tubes would take up a considerable amount of space.
2. Rich display content, can display all numbers and uppercase and lowercase letters.
3. Simple programming; if using digital tubes for dynamic display, a lot of time would be consumed refreshing the display, while the 1602 module automatically completes this function.
The 1602 module uses a standard 16-pin interface, which is labeled on the back of the module:
Pin 1: VSS is the ground power supply.
Pin 2: VDD connects to the 5V positive power supply.
Pin 3: V0 is the contrast adjustment pin of the LCD; when connected to positive power, the contrast is weakest, and when grounded, the contrast is highest. If the contrast is too high, ghosting may occur. A 10K potentiometer can be used to adjust the contrast (it is recommended to connect to ground; if not done properly, some modules may not display).
Pin 4: RS is the register select; high level selects data register, low level selects instruction register.
Pin 5: RW is the read/write signal line; high level for reading, low level for writing.
Pin 6: E is the enable pin; when E transitions from high to low, the LCD module executes the command.
Pins 7-14: D0-D7 are 8-bit bidirectional data lines.
Pins 15-16: Empty pins (some are used for backlight).
The character generator ROM (CGROM) inside the 1602 module has stored different dot matrix character graphics, including Arabic numerals, uppercase and lowercase English letters, commonly used symbols, and Japanese kana, each character has a fixed code, and the numbers and letters are compatible with ASCII codes.
It also has a custom character generator RAM (CGRAM) for storing user-defined characters.
The settings, read/write, and cursor control of the 1602 module are completed through instructions, with a total of 11 instructions as follows:
Instruction 1: Clear display, reset cursor to address 00H.
Instruction 2: Reset cursor, return cursor to address 00H.
Instruction 3: Cursor and display mode setting I/D: cursor moving direction, high level moves right, low level moves left, S: whether all text on the screen moves left or right. High level means valid, low level means invalid.
Instruction 4: Display switch control. D: controls the overall display on and off, high level means display on, low level means display off. C: controls the cursor on and off, high level means cursor is present, low level means cursor is absent. B: controls whether the cursor blinks, high level means blinking, low level means not blinking.
Instruction 5: Cursor or display shift S/C: high level moves the displayed text, low level moves the cursor. R/L, high means left, low means right.
Instruction 6: Function setting command DL: high level for 4-bit bus, low level for 8-bit bus. N: low level for single line display, high level for double line display. F: low level displays 5×7 dot matrix characters, high level displays 5×10 dot matrix characters. (Some modules have DL: high level for 8-bit bus, low level for 4-bit bus).
Instruction 7: Character generator RAM address setting, address: character address*8+character row number. (A character is divided into a 5*8 dot matrix, written in one row at a time, 8 rows make up a character).
Instruction 8: Set display address, first line: 00H-0FH, second line: 40H-4FH.
Instruction 9: Read busy signal and cursor address BF: busy flag, high level means busy, during this time the module cannot receive commands or data, low level means not busy.
Instruction 10: Write data.
Instruction 11: Read data.
The following is a program that displays the letter “A” at the first character position of the second line of the LCD module:
[Compiled successfully with Keil uVision2, AT89S51 + 12M crystal + JHD 162A module successfully displayed]
MCS51 microcontroller assembly program
ORG 0000H
RS EQU P3.7 ; determine specific hardware connection method
RW EQU P3.6 ; determine specific hardware connection method
E EQU P3.5 ; determine specific hardware connection method
MOV P1,#00000001B ; clear screen and reset cursor
ACALL ENABLE ; call write command subroutine
MOV P1,#00111000B ; set display mode, 8-bit 2 lines 5X7 dot matrix
ACALL ENABLE ; call write command subroutine
MOV P1,#00001111B ; display on, cursor on, cursor blinking allowed
ACALL ENABLE ; call write command subroutine
MOV P1,#00000110B ; text does not move, cursor automatically moves right
ACALL ENABLE ; call write command subroutine
MOV P1,#0C0H ; write display starting address (first position of the second line)
ACALL ENABLE ; call write command subroutine
MOV P1,#01000001B ; code for letter A
SETB RS ; RS=1
CLR RW ; RW=0 ; prepare to write data
CLR E ; E=0 ; execute display command
ACALL DELAY ; check if the LCD module is busy?
SETB E ; E=1 ; display completed, program halts
AJMP $
ENABLE:
CLR RS ; subroutine for writing control command
CLR RW
CLR E
ACALL DELAY
SETB E
RET
DELAY:
MOV P1,#0FFH ; subroutine for checking if the LCD is busy
CLR RS
SETB RW
CLR E
NOP
SETB E
JB P1.7,DELAY ; if P1.7 is high, it means busy, loop waiting
RET
END
To help everyone learn better, Changxue Electronics has specially added a public account for microcontrollers and EDA, pushing relevant knowledge every day, hoping to assist your studies!