catalogue
Introduction to LCD1602 display screen
Internal structure block diagram
LCD1602 instruction operation flow:
Display of characters and strings
Various hexadecimal digital display
Display decimal digits on LCD1602
Display hexadecimal digits on LCD1602
Display binary digits on LCD1602
Introduction to LCD1602 display screen
LCD1602 (Liquid Crystal Display) Liquid Crystal Display is a character type Liquid Crystal Display module, which can display ASCII standard characters and some other built-in special characters, as well as 8 custom characters
Display capacity: 16 × 2 characters, each character is a 5 * 7 dot matrix
Pin introduction
Internal structure block diagram
Memory
DDRAM memory address
/** * @brief LCD1602 Set cursor position (convert row and column to address) * @param Line Row position, range: 1 ~ 2 * @param Column Column position, range: 1~16(Column-1) * @retval nothing */ //Set DB7 to 1 to set DDRAM void LCD_SetCursor(unsigned char Line,unsigned char Column) { if(Line==1) { LCD_WriteCommand(0x80|(Column-1));//00H,01H,,,27H } else if(Line==2) { LCD_WriteCommand(0x80|(Column-1+0x40));//40H,41H,,,67H } }
Addresses of CGRAM and {CGROM
Timing introduction
When assigning data, the hexadecimal data can be assigned directly to the P^0 port. It is not necessary to use the shift assignment of "&" bit by bit like the previous I2C and single line communication.
LCD1602 shortcut command
The commonly used instructions are mainly moving the screen and moving the cursor (the commonly used ones have been marked in red)
LCD1602 instruction operation flow:
initialization:
Send command 0x38 / / eight bit data interface, two lines display, 5 * 7 dot matrix
Send the command 0x0C. / / the display is on, the cursor is off, and the flash is off
Send the command 0x06. / / after data reading and writing, the cursor will automatically add one and the screen will not move
Send command 0x01 / / screen clearing
Display characters:
Send the command 0x80|AC / / to set the cursor position
Send data. / / send the character data to be displayed
Send data. / / send the character data to be displayed
Display of characters and strings
Character: the mapping of numbers to characters established according to certain rules (ASCII code table)
For example: 0x21 = '!', 0x41=’A’,0x00=’\0’
Definition method: char x = 'A'; (equivalent to char x=0x41;)
Character array: an array that stores character variables
Definition method: char y [] = {'A', 'B', 'C'};
(equivalent to char y[]={0x41,0x42,0x43})
String: add a string end flag after the character array, which is essentially a character array
Definition method: char z [] = "ABC"; (equivalent to char z [] = {'A', 'B', 'C', '0'};)
From the above explanation, we also found that when the difference between character array and string is equivalent to the form with curly braces, the latter system will automatically supplement '\ 0', while the former will not. Therefore, when designing the function to display the string, we can traverse the array, and finally stop the loop with '\ 0' as the flag
/** * @brief Start displaying the given string at the position specified by LCD1602 * @param Line Starting line position, range: 1 ~ 2 * @param Column Starting column position, range: 1 ~ 16 * @param String String to display * @retval nothing */ void LCD_ShowString(unsigned char Line,unsigned char Column,char *String) //Pass the first address of the array with a pointer { unsigned char i; LCD_SetCursor(Line,Column); for(i=0; String[i]!='\0'; i++)//String adds a flag to the end of the string after the character array { LCD_WriteData(String[i]);//When the mode is set, the cursor will move automatically } }
However, for some special symbols, such as single quotation marks, when applied, the computer may not know which single quotation marks and which single quotation marks are a pair, resulting in an error. At this time, translation symbols are used
Various hexadecimal digital display
Display decimal digits on LCD1602
Through the previous study of character display, we can think by analogy: split each bit of a multi digit number and then display it in the form of characters (we found in the addresses of CGRAM and CGROM that the corresponding number can be displayed only with '0', which further verified our conjecture)
/** * @brief Return value = Y power of X */ int LCD_Pow(int X,int Y) { unsigned char i; int Result=1;//Solve the case of Y=0 for(i=0;i<Y;i++) { Result*=X; } return Result; } /** * @brief Start to display the given number at the specified position of LCD1602 * @param Line Starting line position, range: 1 ~ 2 * @param Column Starting column position, range: 1 ~ 16 * @param Number Number to display, range: 0 ~ 65535 * @param Length To display the length of the number, range: 1 ~ 5 * @retval nothing */ void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) { unsigned char i; LCD_SetCursor(Line,Column); for(i=Length;i>0;i--) { LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0'); }//The number is expressed in the form of ASCLL table because it corresponds in the form of ASCLL table in (sub module library) }
We have also seen that this number can only display 65535 at most. What if we want to display a larger number: the implementation idea is the same as the previous timer high-order and low-order assignment. Set the high order as Number1 and the low order as Number2, which together constitute number
Then, on the first display function, Number1 = (Number/65535) and on the second display function, Number2 = (Number%65535) can display numbers greater than 65535.
Display hexadecimal digits on LCD1602
The method is roughly the same as that of displaying decimal numbers, except that each digit of hexadecimal should judge whether it is greater than 10. If it is greater than 10, it should be represented by 0 ~ 9 Arabic numerals, and if it is greater than 10, it should be represented by A, B, C, D, E and F
/** * @brief Display the given number in hexadecimal from the specified position of LCD1602 * @param Line Starting line position, range: 1 ~ 2 * @param Column Starting column position, range: 1 ~ 16 * @param Number Number to display, range: 0~0xFFFF * @param Length To display the length of the number, range: 1 ~ 4 * @retval nothing */ void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) { unsigned char i,SingleNumber; LCD_SetCursor(Line,Column); for(i=Length;i>0;i--) { SingleNumber=Number/LCD_Pow(16,i-1)%16;//Unlike hexadecimal numbers, hexadecimal numbers are divided by 16 to get different bits if(SingleNumber<10) { LCD_WriteData(SingleNumber+'0'); } else//10 the above shows ABCDEF, so subtract 10 first { LCD_WriteData(SingleNumber-10+'A'); } } }
Display binary digits on LCD1602
It is roughly the same as displaying decimal numbers, except that the base is 1 every 2
/** * @brief Start to display the given number in binary at the specified position of LCD1602 * @param Line Starting line position, range: 1 ~ 2 * @param Column Starting column position, range: 1 ~ 16 * @param Number Number to be displayed, range: 0 ~ 1111 1111 1111 1111 (note that binary data cannot be written directly in c language, so write hexadecimal replacement) * @param Length To display the length of the number, range: 1 ~ 16 * @retval nothing */ void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) { unsigned char i; LCD_SetCursor(Line,Column); for(i=Length;i>0;i--) { LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');//Different from hexadecimal, binary numbers divide by 2 and get different bits } }