1. Implementation of printf series functions
We know that the microcontroller transmits data to the computer window through the serial port. Sometimes we need to send a string, sometimes we need to send the value of a variable, and sometimes we need to send a carriage return and line feed. So we encapsulate these three functions to facilitate the subsequent use of the serial port.
First of all, the "void printf_str(u8 *str)" function written by the author is specifically used to send strings to the computer.
Secondly, the value of the variable sent by "void printf_num(u32 num)" only supports displaying decimal numbers 0 to 4294967295, that is, the parameter is of u32 type.
Finally, "void printf_rn()" sends the carriage return and line feed characters.
You can try to read how these three parts of the code are implemented. If there are many places you don’t understand, you don’t need to delve into them. Just keep learning. The author will explain their application in the following knowledge points.
We hope that the serial port function module can also be encapsulated into a separate file for use like the timer.
So create the "uart.c" and "uart.h" files, copy the following code
2.uart.c code
#include #include void ConfigUART(u16 baud) { SCON = 0x50; //Configure the serial port to mode 1 TMOD &= 0x0F; // Clear the control bit of T1 TMOD |= 0x20; //Configure T1 to mode 2 TH1 = 256 - (11059200/12/32)/baud; //Calculate T1 reload value TL1 = TH1; //initial value equals reload value ET1 = 0; //Disable T1 interrupt ES = 1; // Enable serial port interrupt TR1 = 1; //Start T1 } void printf_str(u8 *str) { while(*str != '') //Continuously send string data until the end character is detected { SBUF=*str++; while(!TI); //Wait for the byte to be sent and TI is set to 1 to exit this while loop TI = 0; // clear the flag } } void printf_num(u32 num) { u8 buf[10]; char i; //The value range is -128~127 for (i=0; i<10; i++) //Convert the long integer to a 10-digit decimal array { buf[i] = num % 10; num = num / 10; //Discard the single digit and reload } for (i=9; i>=1; i--) { if (buf[i] != 0)break; //Starting from the highest bit, ignore if 0 is encountered, and exit the loop if non-zero is encountered } while(i>=0) //The remaining low bits are sent out truthfully { SBUF='0'+buf[i]; //If the value of buf[i] is 1, then in the "character format" mode, the computer window only needs to add '0'+1 to display the character "1", because '0' is the ASCII code value 48. while(!TI); //Wait for the byte to be sent and TI is set to 1 to exit this while loop TI = 0; // clear the flag i--; } } void printf_rn() //Send carriage return and line feed characters { SBUF='r'; while(!TI); //Wait for the byte to be sent and TI is set to 1 to exit this while loop TI = 0; // clear the flag SBUF='n'; while(!TI); //Wait for the byte to be sent and TI is set to 1 to exit this while loop TI = 0; // clear the flag } 3.uart.h code #ifndef __UART_H__ #define __UART_H__ void ConfigUART(u16 baud); void printf_str(u8 *str);//send string void printf_num(u32 num);//Send parameter value void printf_rn(); //Send carriage return and line feed characters #endif 4.main.c code #include #include #include void main() { u8 key; u32 value=65535; LED_Init(); //Initialize LED hardware module KEY_Init(); //Initialize the key module EA = 1; //Close the main interrupt switch ConfigUART(9600); while(1) { key=KEY_Scan(0,1000); if(key==4) { printf_str("value=");//send string printf_num(value); //Send the value of the variable printf_rn(); //Send carriage return and line feed characters value++; } } } void InterruptUART() interrupt 4 { if (RI) // Byte received { RI = 0; //Manually clear the receive interrupt flag } } We did not write "if (TI) { TI = 0; }" in the serial port interrupt function because we have cleared TI to 0 in all sending functions, so there is no need to write TI in the serial port interrupt function. Don't forget to add "uart.c" and "uart.h" to the project file after creating them. Open the serial port, select the "Character format display" mode, and keep pressing K4 to print the following content
Previous article:51 MCU - Detailed understanding of ASCII code
Next article:51 MCU-LCD screen code explanation
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- Intelligent LED lighting solution based on sensor and MCU
- MSP430F5438a uses BSL downloader to download program
- 【mpy】The SD/MMC bus can be configured to 1 or 4 bits
- Introducing several circuit simulation software that can run on mobile phones
- The microcontroller uses a dip switch to set the address
- 5G-specific terminology--it's technology
- GD32307E-START unboxing and onboard resource evaluation
- I just started to use Bluetooth chips with MCU. I want to know if this chip can be easily made into a development board similar to STM32?
- What is the biggest motivation for you to change careers?
- Design of intelligent reactive power compensation controller based on MSP430 single chip microcomputer