LCD1602 Introduction
LCD1602 character liquid crystal (16 characters per line, two lines in total) - a dot matrix liquid crystal module specially used to display letters, numbers and symbols. It is composed of several 5x7 or 5x10 dot matrix characters. Each dot matrix character position can be used to display a character. There is a dot spacing between each bit, and there is also a spacing between each line, which plays the role of character spacing and line spacing. Because of this, it cannot display pictures very well.
1602 Pinout
We only need to pay attention to the following pins:
Pin 3: VL, LCD display bias signal, used to adjust the display contrast of LCD1602. Generally, an external potentiometer is used to adjust the bias signal. Note that the strongest contrast can be obtained when the voltage of this pin is 0.
Pin 4: RS, data/command selection terminal. When this pin is at a high level, the data byte can be transmitted to the 1602. When it is at a low level, the command byte is transmitted. The command byte is used to set some working modes of the LCD1602; the data byte is the byte used to display on the 1602. It is worth mentioning that the data of the LCD1602 is 8 bits.
Pin 5: R/W, read/write selection terminal. When this pin is high, the LCD1602 can be read, otherwise it can be written.
Pin 6: E, enable signal, is actually the data control clock signal of LCD1602. The rising edge of this signal is used to realize data transmission to LCD1602.
Pins 7~14: 8-bit parallel data port, which makes it much easier to read and write data to LCD1602.
Operation Timing
Since we don't need to read 1602, we only look at the write operation below:
① When we want to write instruction words, when setting the working mode of LCD1602: we need to set RS to low level, RW to low level, and then send the data to data port D0~D7, and finally a high pulse on the E pin will write the data.
② When we want to write data words and realize display on 1602: we need to set RS to high level, RW to low level, and then send the data to data port D0~D7, and finally a high pulse on the E pin will write the data.
Did you find that the difference between writing instructions and writing data is only the different RS levels. The following is the timing diagram of LCD1602:
Write Operation Timing
Timing parameters
When writing a command byte, from left to right, RS becomes low level, R/W becomes low level, and it is noted that the RS state changes first. Then, the data on DB0~DB7 enters the valid stage, and then the E pin has a full pulse jump, and then the E pulse width with a minimum time of tpw=400ns must be maintained. Then the E pin jumps negatively, the RS level changes, and the R/W level changes. This is a complete LCD1602 write command timing.
LCD1602 key command operation
1. Clear screen command
Clear the LCD display, that is, fill all the contents of DDRAM with the "blank" ASCII code 20H;
Return the cursor to the upper left corner of the LCD screen;
Set the value of the address counter (AC) to 0;
2. Enter mode setting command
Function: Set the direction of cursor shift after entering 1 bit of data each time, and set whether to move each character written. The parameter settings are as follows:
Bit Name --- Settings;
I/D —— 0=The cursor moves left after writing new data; 1=The cursor moves right after writing new data;
S —— 0=The display screen does not move after writing new data; 1=The display screen moves right by 1 character after writing new data;
3. Display switch control instructions
Function: Control the display on/off, cursor on/off, and whether the cursor flashes. The parameter settings are as follows:
Bit Name————Setting
D —— 0=display function off; 1=display function on;
C —— 0=no cursor; 1=with cursor;
B —— 0=cursor flashes; 1=cursor does not flash;
4. Function Setting Instructions
Function: Set the number of data bus bits, the number of displayed lines and fonts. The parameter settings are as follows:
Bit Name————Setting
DL —— 0 = data bus is 4 bits; 1 = data bus is 8 bits;
N —— 0=display 1 line; 1=display 2 lines;
F —— 0=5×7 dots/each character; 1=5×10 dots/each character;
5. RAM address map
After the setting is completed, we need to understand where and at what position the display is. When initializing the LCD module, we must first set its display mode. When the LCD module displays characters, the cursor automatically moves to the right without manual intervention. Before entering each instruction, it is necessary to determine whether the LCD module is still busy. DDRAM is the display data RAM, which is used to store the character code to be displayed. There are 80 bytes in total, and the corresponding relationship between its address and the screen is as follows:
To display characters, you must first enter the display character address, that is, tell the module where to display the characters; for example, the address of the first character in the second line is 40H, so can you directly write 40H to determine the cursor position? What about the position of the first character in the second line? This is not possible, because when writing the display address, the highest bit D7 is required to be constant at high level 1, so the actual data written should be:
0100000OB (40H)+10000000B(80H)=11000000B(C0H)。
In 1602, we just use the first 16. The second line also uses the first 16 addresses. The corresponding ones are as follows:
LCD1602 driver code
Write Command
// Write a one-byte command to the LCD1602, cmd-the command value to be written
// Write command timing: RS=0, RW=0, D7-D0 = data, E=positive pulse, LCD samples data on the falling edge of the pulse.
void lcd1602WriteCmd(unsigned char cmd)
{
delayNms(5); //delay 5ms
RS=0;// Order
RW=0;// Write
E=1;// Enable LCD1602
DPORT=cmd;// Output the command code on the data port
delay10us(); // short delay 10us
E=0;// Turn off LCD1602 enable and release the bus
}
Writing Data
// Write one byte of data to the LCD1602, dat-data value to be written
// Write data timing: RS=1, RW=0, D7-D0 = data, E=positive pulse, LCD samples data on the falling edge of the pulse.
void lcd1602WriteByte(unsigned char dat)
{
delayNms(5); //delay 5ms
RS=1;// data
RW=0;// Write
E=1;// Enable LCD1602
DPORT=that;// Send the data to be written to the data port
delay10us(); // short delay 10us
E=0; // Turn off LCD1602 enable and release the bus
}
Clear screen command
// Clear screen command: clear the display content and fill all the internal RAM of 1602 with blank ASCII code 20H
//Return the cursor to the coordinate origin in the upper left corner of the screen
// Set the 1602 internal display address to 0
void lcd1602Clear()
{
lcd1602WriteCmd(0x01);
delayNms(50);//50ms
}
Setting the Cursor
void lcd1602SetCursor(unsigned char Para) // Set the cursor
{
switch(To)
{
case 0:
{
lcd1602WriteCmd(0x08);break; // Turn off the display
}
case 1:
{
lcd1602WriteCmd(0x0c);break; // Turn on the display but without cursor
}
case 2:
{
lcd1602WriteCmd(0x0e);break; // Turn on the display with cursor but without blinking
}
case 3:
{
lcd1602WriteCmd(0x0f);break; // Turn on the display with the cursor flashing
}
default:
break;
}
}
initialization
// LCD 1602 initialization
void lcd1602Init()
{
lcd1602WriteCmd(0x38); /*Display mode setting*/
delayNms(5); //5ms
lcd1602WriteCmd(0x38);
//delayNms(5);
//lcd1602WriteCmd(0x38);
//delayNms(5);
//lcd1602WriteCmd(0x38);
lcd1602WriteCmd(0x08); //Display off, cursor not displayed, cursor not blinking
lcd1602WriteCmd(0x01); // Clear the screen
lcd1602WriteCmd(0x06); // Display the cursor position
delayNms(5);//5ms
lcd1602WriteCmd(0x0c); //Display on and cursor settings
lcd1602Clear();
}
Function Encapsulation
Steps:
1. Initialization
2. Write command (RS=L) to set the display coordinates
3. Write data (RS=H)
lcd1602.h file
Statement: Please write your own delay function based on the crystal oscillator
#include "delay.h" //Please write your own delay function according to the crystal oscillator
spitRS=P1^0;// Modify according to actual hardware connection
spitRW=P1^1;// Modify according to actual hardware connection
spitAND=P1^2;// Modify according to actual hardware connection
#define DPORTP0// Modify according to actual hardware connection
void lcd1602Init();// Initialization
void lcd1602Clear(); // Clear screen command
void lcd1602SetCursor(unsigned char Para);//Set the cursor
void lcd1602WriteChar(unsigned char xPos,unsigned char yPos,unsigned char Dat); // write 1 character
void lcd1602WriteString(unsigned char xPos,unsigned char yPos,unsigned char *s); // write string
lcd1602.c file
#include "lcd1602.h"
void lcd1602WriteByte(unsigned char dat){}//Write data
void lcd1602WriteCmd(unsigned char cmd){}//Write command
void lcd1602Clear(){}//Qingping
void lcd1602SetCursor(unsigned char Para){} // Set the cursor
void lcd1602Init(){}//initialization
//Internal function used to set the starting coordinates of displayed characters
void lcd1602WritePos(unsigned char xPos,unsigned char yPos)
{
unsigned char tmp;
xPos&=0x0f; // The x position range is 0~15
yPos&=0x01; // The y position range is 0~1
if(yPos==0) // Display the first line
tmp=xPos; // The first line of characters starts at address 0x00
else
tmp=xPos+0x40; // The second line of characters starts at address 0x40
tmp|=0x80; // Set RAM address
lcd1602WriteCmd(tmp);
}
//Display the specified character in the specified row and column, xpos: row, ypos: column, c: character to be displayed
void lcd1602WriteChar(unsigned char xPos,unsigned char yPos,unsigned char Dat)
{
lcd1602WritePos(xPos,yPos);
lcd1602WriteByte(That);
}
// Display the string on the LCD, xpos: row coordinate, ypos: column coordinate, str-string pointer
void lcd1602WriteString(unsigned char xPos,unsigned char yPos,unsigned char *s)
{
unsigned char i=0;
lcd1602WritePos(xPos,yPos); // Starting coordinates
while(s[i])
{
lcd1602WriteByte(s[i]);
i++;
if (i>=16) break; // Data exceeding 16 characters is discarded
}
}
Previous article:C51 entry-level small project - two-way intersection traffic light
Next article:Getting Started with C51 MCU - Buttons
Recommended ReadingLatest update time:2024-11-23 18:11
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Design of DC Feed in Microstrip Amplifier
- How to configure the low-level driver of the HT16L21 LCD? It is based on IIC.
- Practical DSP28335 Programming Ideas
- Constant current source, constant voltage source, voltage drop after MOS is turned on
- [April 23 | Suzhou] High-speed digital and optical communication test seminar invites you to meet
- TMS320C6748_UART(3) - UART polling mode
- Award-winning live broadcast: Application of TI millimeter-wave radar in cars is now open for registration~
- Modelsim10.4 installation and adding crack files error solution
- PCB production
- Sharing 10 common formulas for switching power supply design