First example:
Avoid infinite loop, keep sending data (use the self-set receiving data flag method, do not turn off the serial port interrupt ES method) The flag must be placed at the last position in the if statement, otherwise an infinite loop will occur
#include#define uchar unsigned char #define uint unsigned int uchar flag=0,temp; void init() { TMOD=0X20; //Timer 1 mode 2 as baud rate generator TH1=0XF3; //Install the initial baud rate to 4800bps TL1=0XF3; TR1=1; //Start timer 1 SCON=0X50; //Set the serial port working mode to mode 1 PCON=0X80; // Double the baud rate to 4800 EA=1; //Open the general interrupt ES=1; //Enable serial port interrupt TI=0; // Clear the send interrupt flag RI=0; // Clear the receive interrupt flag } void main() { init(); while(1) { if(flag==1) { //ES=0; //flag is 1, indicating that the reception caused an interrupt //flag=0; //clear interrupt flag RI=0; // Clear the receive interrupt flag to prepare for the next frame of data reception temp=SBUF; //Read in received data. Without disabling interrupt ES, the CPU will not cause an interrupt when reading received data.
The receiving data is automatically executed by the receiving SBUF receiving buffer P0=temp; //Read data and send to P0 port SBUF=temp; //Return received data to the sender //TI=0; //Wait for the data to be sent, otherwise TI=0, flag=0 has been executed before the data is sent. //flag=0; //After sending, it causes an interrupt, and the flag is set to 1 again, entering an infinite loop and continuously sending data while(!TI);//Wait for sending to complete! ! Keep waiting! ! TI=0; //After data transmission, clear the transmission interrupt flag to prepare for the next frame of data transmission flag=0; //Must have otherwise enter an infinite loop, put last //ES=1; //Allow serial port interrupt to continue receiving data } } } void serial() interrupt 4 { flag=1; }
----------------------Example 2:-------------------------------------------------------
/********************************************** Interrupt serial communication (using the ES method to disable serial interrupts) is recommended to avoid frame loss *************************************************/ #include#define uchar unsigned char #define uint unsigned int uchar flag=0,temp; void init() { TMOD=0X20; //Timer 1 mode 2 as baud rate generator TH1=0XF3; //Install the initial baud rate to 4800bps TL1=0XF3; TR1=1; //Start timer 1 SCON=0X50; //Set the serial port working mode to mode 1 PCON=0X80; // Double the baud rate to 4800 EA=1; //Open the general interrupt ES=1; //Enable serial port interrupt TI=0; // Clear the send interrupt flag RI=0; // Clear the receive interrupt flag } void main() { init(); while(1) { if(flag==1) { ES=0; //flag is 1, indicating that the reception has completed and caused an interrupt, turn off the serial port interrupt, and process the current frame flag=0; // clear interrupt flag RI=0; // Clear the receive interrupt flag to prepare for receiving the next frame of data. Clearing it here will not cause frame loss. temp=SBUF; //Read in received data. If the interrupt ES is not disabled, the CPU will not cause an interrupt when reading received data.
The receiving data is automatically executed by the receiving SBUF receiving buffer P0=temp; //Read data and send to P0 port SBUF=temp; //Return received data to the sender //TI=0; //Wait for the data to be sent, otherwise TI=0, flag=0 has been executed before the data is sent. //flag=0; //After sending, it causes an interrupt, and the flag is set to 1 again, entering an infinite loop and continuously sending data while(!TI);//Wait for sending to complete! ! Keep waiting! ! TI=0; //After data transmission, clear the transmission interrupt flag to prepare for the next frame of data transmission //flag=0; //Must have otherwise enter an infinite loop, put last ES=1; // Allow serial port interrupt to continue receiving data } } } void serial() interrupt 4 //Execute this sentence to generate an interrupt indicating that the data has been received { //RI=0; //Do not clear the receive interrupt flag here, otherwise the receiving SUBF will be in the receiving state.
The interruption continues to affect other instructions, causing the return frame loss phenomenon. The experiment shows that flag=1; }
Previous article:Lookup table matrix keyboard
Next article:A brief discussion on how to eliminate key jitter
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
- Compile QT5 application with qt creator
- micropython update: 2021.3
- Show off the Xiaomi mouse I received from the Ti reading activity
- [AutoChips AC7801x motor demo board review] + motor demo board unboxing report and quick new project
- About the problem of L6226 controlling brushless DC motor
- Publish a batch of paper books
- Evaluation of domestic FPGA Gaoyun GW1N-4 series development board - stepper motor control (modified)
- Camel air conditioner fan 220v series 16v capacitor, please help analyze
- QT Py Color Cuff
- Some points to note when designing 2812