Originally, I was going to start this section with the reverse display procedure when calling the internal font library of the LCD screen, but I was worried that the jump was too big and many beginners might not be able to keep up, so I inserted this section to talk about the basic functions that are often used in the menu program later. When calling the internal font library, how to display a variable of any value on the LCD screen. The functional requirements of this section are exactly the same as those of the previous section 76, except that the previous one does not use the built-in font library, but the current one does. We still need to make a function to convert a variable into ASCII code, and we only need to call this conversion function in the future. This section will teach you this conversion function and framework ideas.
For details, please see the source code.
(1) Hardware platform:
Based on Zhu Zhaoqi 51 single-chip microcomputer learning board.
(2) Functionality: We define a char type global variable and initialize it to 218 by default. After powering on, we can see that the value of this global variable, 218, is displayed right in the middle. You can also try to change its default initial value. As long as it does not exceed the maximum value of char type, 255, we will see that it displays this initial value after powering on.
(3) The source code is explained as follows:
#include "REG52.H"
sbit LCDCS_dr = P1^6; //chip select line
sbit LCDSID_dr = P1^7; //Serial data line
sbit LCDCLK_dr = P3^2; //Serial clock line
sbit LCDRST_dr = P3^4; //reset line
sbit beep_dr=P2^7; //Buzzer driver IO port
void initial_myself(void);
void initial_peripheral(void);
void delay_long(unsigned int uiDelaylong);
unsigned char *number_to_ASCII(unsigned char ucBitNumber);
void display_service(void); //Display service program, in main function
void SendByteToLcd(unsigned char ucData); //Send a byte of data to the LCD module
void SPIWrite(unsigned char ucWData, unsigned char ucWRS); //Simulate SPI to send a byte of command or data to the underlying driver of the LCD module
void WriteCommand(unsigned char ucCommand); //Send a byte command to the LCD module
void LCDWriteData(unsigned char ucData); //Send one byte of data to the LCD module
void LCDInit(void); //Initialization function including LCD module reset
void display_clear(void); // Clear the screen. Displaying 2 blank characters for all coordinate points in 4 rows and 8 columns is equivalent to clearing the screen.
void display_double_code(unsigned int x,unsigned int y,const unsigned char ucArray1,const unsigned char ucArray2); //Function to display one Chinese character or two characters at a coordinate point
void delay_short(unsigned int uiDelayshort); //延时
code unsigned char ucAddrTable[]= //When calling the internal font library, the coordinate system and position encoding of the LCD screen are driver contents, and readers do not need to delve into its meaning.
{
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
};
code unsigned char ASCII816_0[]="0"; //0 For the characters in the array, the compiler will automatically translate them into ASCII code (1 byte)
code unsigned char ASCII816_1[]="1"; //1
code unsigned char ASCII816_2[]="2"; //2
code unsigned char ASCII816_3[]="3"; //3
code unsigned char ASCII816_4[]="4"; //4
code unsigned char ASCII816_5[]="5"; //5
code unsigned char ASCII816_6[]="6"; //6
code unsigned char ASCII816_7[]="7"; //7
code unsigned char ASCII816_8[]="8"; //8
code unsigned char ASCII816_9[]="9"; //9
code unsigned char ASCII816_nc[]=" "; //空格
/* Note 1:
* The following variables are arbitrary variables of this program. Netizens can change their sizes to test this program. Do not exceed 255.
*/
unsigned char ucAnyNumber=218; //Any variable is initialized to 218 by default.
unsigned char ucWd1Part1Update=1; //The first local update display of window 1 Variable 1 represents the update display, and will be cleared inside the response function
void main()
{
initial_myself();
delay_long(100);
initial_peripheral();
while(1)
{
display_service(); //Display service program
}
}
/* Note 2: Function to display 1 Chinese character or 2 characters at a coordinate point
* The first and second parameters x and y are coordinate systems. The range of x is 0 to 8, and the range of y is 0 to 3.
* The third parameter ucArray1 is the first Chinese character machine code or ASCII code.
* The fourth parameter ucArray2 is the second Chinese character machine code or ASCII code.
*/
void display_double_code(unsigned int x,unsigned int y,const unsigned char ucArray1,const unsigned char ucArray2)
{
WriteCommand(0x30); //Basic instruction set
WriteCommand(ucAddrTable[8*y+x]); //Starting position
LCDWriteData(ucArray1);
LCDWriteData(ucArray2);
}
void display_clear(void) // Clear the screen. Displaying 2 blank characters for all coordinate points in 4 rows and 8 columns is equivalent to clearing the screen.
{
unsigned int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<8;j++)
{
display_double_code(j,i,0x20,0x20); //0x20 is the ASCII code for a space
}
}
}
void SendByteToLcd(unsigned char ucData) //Send a byte of data to the LCD module
{
unsigned char i;
for ( i = 0; i < 8; i++ )
{
if ( (ucData << i) & 0x80 )
{
LCDSID_dr = 1;
}
else
{
LCDSID_dr = 0;
}
LCDCLK_dr = 0;
LCDCLK_dr = 1;
}
}
void SPIWrite(unsigned char ucWData, unsigned char ucWRS) //Simulate SPI to send a byte of command or data to the underlying driver of the LCD module
{
SendByteToLcd( 0xf8 + (ucWRS << 1) );
SendByteToLcd( ucWData & 0xf0 );
SendByteToLcd( (ucWData << 4) & 0xf0);
}
void WriteCommand(unsigned char ucCommand) //Send a byte command to the LCD module
{
LCDCS_dr = 0;
LCDCS_dr = 1;
SPIWrite(ucCommand, 0);
delay_short(90);
}
void LCDWriteData(unsigned char ucData) //Send one byte of data to the LCD module
{
LCDCS_dr = 0;
LCDCS_dr = 1;
SPIWrite(ucData, 1);
}
void LCDInit(void) //Initialization function including LCD module reset
{
LCDRST_dr = 1; // reset
LCDRST_dr = 0;
LCDRST_dr = 1;
}
/* Comment 3:
* The core conversion function of this program.
* is a function that can convert any numeric variable into the corresponding ASCII code. Since the ASCII code is placed in an array, the returned value is a pointer, which represents the first address of the array.
*/
unsigned char *number_to_ASCII(unsigned char ucBitNumber)
{
unsigned char *p_ucAnyNumber; //This pointer calls different ASCII codes according to the value of ucBitNumber.
switch(ucBitNumber) //Call different ASCII codes according to the value of ucBitNumber.
{
case 0:
p_ucAnyNumber=ASCII816_0;
break;
case 1:
p_ucAnyNumber=ASCII816_1;
break;
case 2:
p_ucAnyNumber=ASCII816_2;
break;
case 3:
p_ucAnyNumber=ASCII816_3;
break;
case 4:
p_ucAnyNumber=ASCII816_4;
break;
case 5:
p_ucAnyNumber=ASCII816_5;
break;
case 6:
p_ucAnyNumber=ASCII816_6;
break;
case 7:
p_ucAnyNumber=ASCII816_7;
break;
case 8:
p_ucAnyNumber=ASCII816_8;
break;
case 9:
p_ucAnyNumber=ASCII816_9;
break;
case 10:
p_ucAnyNumber=ASCII816_nc;
break;
default: //If none of the above conditions are met, the default is to point to the space ASCII code
p_ucAnyNumber=ASCII816_nc;
break;
}
return p_ucAnyNumber; //Return the pointer after the conversion is completed
}
void display_service(void) //Display service program, in the main function
{
/* Note 4:
* The local variable here is modified with the static keyword because this function is always scanning in the main function while(1) loop, and I don’t want it to enter this function every time.
* It takes a few more instructions to initialize these local variables, which will consume a few more instructions, so I use the static keyword to avoid this situation and make these local variables
* Initialized only at the moment of power-on. There is no need to initialize these variables each time you enter this function.
*/
static unsigned char ucAnyNumber_1; //decompose the unit digit of the variable
static unsigned char ucAnyNumber_10; //decompose the tens digit of the variable
static unsigned char ucAnyNumber_100; //decompose the hundreds place of the variable
static unsigned char *p_ucAnyNumber_1; //After the number is converted into a font, the first address of the font of the decomposition variable
static unsigned char *p_ucAnyNumber_10; //After the number is converted into a font, the first address of the decomposition variable's ten-digit font
static unsigned char *p_ucAnyNumber_100; //After the number is converted into a font, the first address of the hundreds digit of the decomposition variable
if(ucWd1Part1Update==1) //The first partial update display variable of window 1, which contains some content that needs to be refreshed frequently
{
ucWd1Part1Update=0; //Reset to zero in time to avoid continuous updating
if(ucAnyNumber>=100) // more than 3 digits
{
ucAnyNumber_100=ucAnyNumber/100; //hundreds place
}
else //Otherwise it will display empty
{
ucAnyNumber_100=10; //In the following conversion function, code 10 represents an empty font.
}
if(ucAnyNumber>=10) // more than 2 digits
{
ucAnyNumber_10=ucAnyNumber%100/10; //ten digit
}
else //Otherwise it will display empty
{
ucAnyNumber_10=10; //In the following conversion function, code 10 represents an empty font.
}
ucAnyNumber_1=ucAnyNumber%10/1; //unit digit
p_ucAnyNumber_100=number_to_ASCII(ucAnyNumber_100); //Convert the number into character ASCII code
p_ucAnyNumber_10=number_to_ASCII(ucAnyNumber_10); //Convert numbers into ASCII characters
p_ucAnyNumber_1=number_to_ASCII(ucAnyNumber_1); //Convert numbers into ASCII characters
display_double_code(2,1,ASCII816_nc[0],p_ucAnyNumber_100[0]); //LCD display driver function Here ASCII816_nc[0] means fill the display with a space character
display_double_code(3,1,p_ucAnyNumber_10[0],p_ucAnyNumber_1[0]); //LCD display driver function
}
}
void delay_short(unsigned int uiDelayShort)
{
unsigned int i;
for(i=0;i
{
;
}
}
void delay_long(unsigned int uiDelayLong)
{
unsigned int i;
unsigned int j;
for(i=0;i
{
for(j=0;j<500;j++) //Number of empty instructions in the embedded loop
{
; //A semicolon is equivalent to executing an empty statement
}
}
}
void initial_myself(void) //Initialize the MCU in the first zone
{
beep_dr=1; //Use PNP transistor to control the buzzer, it will not sound when the output is high level.
}
void initial_peripheral(void) // Initialize the periphery of the second zone
{
LCDInit(); // Initialize 12864 including the reset of the LCD module
WriteCommand(0x0C); // Command word 0x0c means to use internal font library mode. Command word 0x36 means to use self-built font library mode.
display_clear(); // Clear the screen. Displaying 2 space characters for all coordinate points in 4 rows and 8 columns is equivalent to clearing the screen.
}
Closing remarks:
In LCD screen programs, the reverse display function is often used to indicate that a menu item is selected. When calling the internal font library, how should such a driver be written? For details, please listen to the next analysis - how to reverse a line of content when calling the internal font library of the LCD screen.
Previous article:Section 81: The LCD screen displays any Chinese characters and characters sent from the serial port
Next article:Section 83: LCD display program for inputting arbitrary numbers or decimal points using matrix keyboard
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Open Source 128x32 OLED SSD1316 Expansion Module AD Circuit Design
- [NXP Rapid IoT Review] Solution for collecting vibration data wirelessly via Bluetooth
- What is the MHz of the crystal oscillator in this example?
- Wireless sensor node power supply solution
- Playing with Zynq Serial 5 - Overview of Online Board-Level Debugging Based on Vivado
- [TI recommended course] #[High Precision Lab] Isolation: Isolated Gate Drivers#
- What is fast charging fraud?
- 【MSP430】PM2.5 concentration measurement
- AMC7932 single-chip solution enables detection and control of GaN power amplifiers
- LSM6DSL calls STM32_MotionGC_Library's .a file and compiles and reports an error