#include
#include
main()
{
SCON = 0x50; /*SCON: working mode 1, 8-bit UART, allow receiving*/
TMOD |= 0x20;/*TMOD: Timer T1, working mode 2, 8-bit automatic reload mode*/
TH1= 0xf3;/*When the baud rate is 2400, the timer initial value*/
TR1= 1;/*Timer T1 starts running*/
TI = 1; /*Allow sending data*/
printf("hello world");
}
After debugging and running the above code, the serial port continuously outputs "hello world" and the program falls into an infinite loop.
Solution to the problem: add while(1); at the end of the program.
Reason: If you do not add while(1); the MCU will execute a RET instruction, which will reset the program.
Bit (software reset, pop 0000H from the stack to the PC register), the program restarts after reset
implement.
Thinking: I am used to programming in Windows. After the main program is executed, it will exit to the operating system, and the microcontroller will
It’s different now, where to retreat? The following is the conversation between the microcontroller and the compiler:
MCU: I have nothing to do, I'd rather die~~~
Compiler: Want to die? You should reset it~~~RET
MCU: o, shit~~~
MCU serial port related issues: The standard 51 has only one serial port, but now many MCUs based on the 51 core have
The microcontroller will have two or more serial ports, so which serial port does the printf function target?
Answer: There is a putchar.c file in the Keil\C51\LIB directory. The printf function will call the
The putchar function uses the standard 51 registers (such as TI, SBUF).
Their addresses are fixed, so we can see that the printf function only targets serial port 0.
Support serial port 1 or serial port 2. Then modify the register of putchar function, for example, change SBUF to
SBUF1. But can I use the printf function to apply to both serial port 0 and serial port 1? I want to do it myself.
French bar :)
In addition: If you just use printf to output a string, you should write a function yourself, because printf needs to process
The output of various types of data takes up a lot of code space. I tried it and there was only one printf statement in the program.
The code is 1K, and printf is also slow to execute.
Previous article:The use of header files in C51 programming
Next article:C51 Language Design Basics--Data Types and Functions
Recommended ReadingLatest update time:2024-11-16 13:07
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Internet of Things Development Technology Based on C Language (Edited by Gao Song)
- 智能化测量控制仪表原理与设计(第3版) (徐爱钧,徐阳编著)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- #DailyGoodReading# Tell us about the good books you have read
- I recently found an article (PPT) to solve the EMC problem of a certain system. I just found it after exploring a little bit (share it...
- Serial port sending and receiving string examples
- First look at TMS320C5410
- Open source INS/GNSS navigation development board Pellicanus
- Radio information
- Force value jitter problem
- VGA Chinese Character Display.pdf
- Seeking guidance, a few questions about RA2A1's SDADC
- Mother's Day is coming, what do you have to say?