I recently used the serial port of stm32. It was very tiring to interrupt the reception of characters one by one. I searched the Internet and found a good post. I would like to share it with you. The original post address is: http://www.51hei.com/bbs/dpj-39885-1.html
Thanks again to the original poster for sharing. For your convenience, I copied the original text here.
Today, let's talk about how to receive indefinite length byte data on the STM32 microcontroller. Since the STM32 microcontroller has an IDLE interrupt, this interrupt can be used to receive indefinite length byte data. Since the STM32 microcontroller belongs to an ARM microcontroller, the method in this article is also suitable for other ARM microcontrollers.
When does the IDLE interrupt occur?
IDLE is an interrupt that occurs after the serial port receives a frame of data. What is a frame of data? For example, if 1 byte is sent to the microcontroller at a time, or 8 bytes are sent at a time, these data sent at a time are called a frame of data, or a packet of data.
How to judge the end of a frame of data is the issue we are discussing today. Because this is used in many projects, because only after receiving a frame of data can you judge how many bytes have been received this time and whether the content of each byte meets the protocol requirements.
After reading the definition of the IDLE interrupt above, you will understand that an IDLE interrupt will be generated after a frame of data ends. This interrupt is really useful. It saves a lot of trouble in judgment.
How to configure the IDLE interrupt?
Let's configure the serial port IDLE interrupt.
This is the serial port CR1 register, where writing 1 to bit4 enables the IDLE interrupt, and writing 1 to bit5 enables the receive data interrupt. (Note: The corresponding register bits may be different for different series of STM32)
(What is the difference between the RXNE interrupt and the IDLE interrupt?
When 1 byte is received, an RXNE interrupt will be generated, and when a frame of data is received, an IDLE interrupt will be generated. For example, if 8 bytes are sent to the microcontroller at one time, 8 RXNE interrupts and 1 IDLE interrupt will be generated.)
This is the status register. When the serial port receives data, bit5 will automatically become 1. After receiving a frame of data, bit4 will become 1.
It should be noted that in the interrupt function, the corresponding bit needs to be cleared to 0, otherwise it will affect the next data reception. For example, the RXNE data receiving interrupt will clear the interrupt as long as a received byte is read out. The IDLE interrupt needs to be cleared using the ICR register if it is an F0 series microcontroller. If it is an F1 series microcontroller, the clearing method is "read the SR register first, then read the DR register".
/* Enable receive interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable idle interrupt */
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
USART_Cmd(USART1, ENABLE);
The key point is to enable the idle interrupt.
uint8_t ch[20];
char i = 0;
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//Received a byte
{
//ch = USART1->DR;
ch[i++] = USART_ReceiveData(USART1);
}
else if(USART_GetITStatus(USART1,USART_IT_IDLE) != RESET)//Received a frame of data
{
USART1->SR; //Read SR first
USART1->DR; // read DR again
i = 0;
printf("%s",ch);
}
}
Previous article:STM32 USART serial port receiving data processing
Next article:STM32F10X series GPIO external interrupt
Recommended ReadingLatest update time:2024-11-15 15:06
- Popular Resources
- Popular amplifiers
- 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
- 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
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- EEWORLD University Hall----Live Replay: Melexis consumer-grade ultra-low power position sensor simplifies design and reduces costs
- EEWORLD University - Multiphase Buck Regulator Design: Case Study
- I need help from the experts on writing MAX7219 dot matrix in microPython
- 【Top Micro Intelligent Display Module】IV: Serial port interaction and application of curve, drawing board and animation controls
- Last few hours: NXP i.MX answer questions and win AI-IoT series books, 10 books left, not all of them are mine~
- EEWORLD University Hall - Sharing of household heating and cooling inverter air conditioner application solutions based on Lingdong MM32SPIN series MCU
- Passband gain
- Steps and precautions for live load verification of relay protection devices
- Answer the questions to win prizes! ADI Application Tour - Battery Management and Smart Energy Storage
- Huawei HR complained in real name: R&D brother worked overtime for 160 hours a month but was said to be not working hard