How to use printf to display information on the serial port? There are two ways. Because in keil C51, the printf function calls putchar to transfer data, and putchar should first determine whether TI is 1. If it is not 1, it waits for it to be 1. If it is 1, it clears it to 0 and then sends a character. Therefore, if you use the printf function directly, your program will wait for TI to be 1 in the putchar function. At this time, your program is equivalent to being dead. You can achieve your own purpose by rewriting the putchar function. TI is equivalent to being initialized and will not work if it is not assigned an initial value.
So the first way is to assign TI a value of 1 during initialization, for example
void uart_init()
{
PCON &= 0x7F; //Baud rate is not doubled
SCON = 0x50; //8-bit data, variable baud rate
AUXR |= 0x40; //Timer 1 clock is Fosc, i.e. 1T
AUXR &= 0xFE; //Serial port 1 selects timer 1 as the baud rate generator
TMOD &= 0x0F; // Clear timer 1 mode bit
TMOD |= 0x20; //Set timer 1 to 8-bit auto-reload mode
TL1 = 0xDC; //Set the initial timing value
TH1 = 0xDC;
ET1 = 0; //Disable timer 1 interrupt
TR1 = 1; //Start timer 1
TI=1; //Send interrupt flag position 1
}
But sometimes, when using UART interrupt, the serial data transmission interrupt flag TI cannot be set to 1 for a long time. At this time, we need to rewrite the putchar function to redirect the output of the printf function, and then use printf to display it. For details, please refer to C: Keil/C51/LIB/PUTCHAR.C, the following is an example:
char putchar (char c) {
while (!TI);
TI = 0;
return (SBUF = c);
}
Previous article:The 51 MCU serial port prints garbled characters in both Chinese and English
Next article:51 MCU Getting Started Tutorial (0) —— Development Environment Configuration
Recommended ReadingLatest update time:2024-11-23 21:42
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 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
- ADI & Shijian New Infrastructure Series Episode 2 - Answer the Energy Storage Questions to Win Gifts! Let's Start~
- Have you ever encountered problems with function return classes in embedded programming?
- Why do camera modules use small-size EEPROM chips?
- Is it possible to create a SQLite database on the ESP32?
- How does this product function?
- 【Fudan Micro FM33LC046N Review】+ LED and KEY
- STM32F429IGT6 core board schematic diagram
- Ink display driver with 32KB SRAM cache
- Analysis of the causes of abnormal conditions in CNC machine tools
- Multiple uses of CC6678 digital signal processor (DSP)