stm32 serial port interrupt receives a frame of data

Publisher:技术掌门Latest update time:2017-09-20 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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".


  1. /* Enable receive interrupt */  

  2. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  

  3. /* Enable idle interrupt */  

  4. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);  

  5. USART_Cmd(USART1, ENABLE);  

The key point is to enable the idle interrupt.



  1. uint8_t ch[20];  

  2. char i = 0;  

  3. void USART1_IRQHandler(void)  

  4. {  

  5.   

  6.     if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)//Received a byte  

  7.     {     

  8.         //ch = USART1->DR;  

  9.             ch[i++] = USART_ReceiveData(USART1);  

  10.       

  11.     }   

  12.     else if(USART_GetITStatus(USART1,USART_IT_IDLE) != RESET)//Received a frame of data  

  13.     {  

  14.         USART1->SR; //Read SR first  

  15.         USART1->DR; // read DR again  

  16.         i = 0;  

  17.         printf("%s",ch);  

  18.     }  

  19.        

  20. }  




Keywords:stm32 Reference address:stm32 serial port interrupt receives a frame of data

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

Understanding of NVIC (Nested Vectored Interrupt Control) in STM32 (Cortex-M3)
1. The concept of priority in STM32 (Cortex-M3)    There are two priority concepts in STM32 (Cortex-M3): preemptive priority and response priority. The response priority is also called "sub-priority" or "sub-priority". Each interrupt source needs to be assigned these two priorities. 1. What is pre-emption priority?
[Microcontroller]
STM32 I2C hard usage
/* The operation object is 24C02 */ #include "STM32Lib\\stm32f10x.h" #include "hal.h" //#define AT24C01A //24C01A, I2C timing is the same as the later 24C02 #define AT24C01 //24C01, I2C timing is a little different from the normal one #define EEPROM_ADDR 0xA0 #define I2C_PAGESIZE 4 //24C01/01A page buffer is
[Microcontroller]
STM32 I2C hard usage
STM32 timer interrupt function
#include "timer.h" #include "led.h" //The crystal oscillator is 8MHZ, and the default CPU frequency is 9 times. //General timer 3 interrupt initialization //The clock here is selected to be twice that of APB1, and APB1 is 36M //SYSCLK:72M //AHB:72M //APB1(PCLK1):36M  // APB2(PCLK2):72M //PLL:72M //arr: automatically r
[Microcontroller]
STM32 uses timer to accurately delay (non-SysTick)
Use TIM2 for delay, the delay base time is 1ms, and the maximum delay is 65535ms. The system base frequency is 8MHz*4=32MHz. First configure the timer:     TIM_TimeBaseInitTypeDef timInitStruct;     timInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // Timer base frequency 32MHz     timInitStruct.TIM_Prescaler =
[Microcontroller]
STM32 - Power consumption cannot be reduced after entering STOP mode
background: Recently, after debugging the board into STOP mode, I found that the current was at the mA level and the power consumption could not be reduced. Finally, I found that after the ADC was turned on, I forgot to turn it off, resulting in a current consumption of 1.45mA. content: After using the HAL_PWR_Enter
[Microcontroller]
STM32 - Power consumption cannot be reduced after entering STOP mode
stm32 pwm breathing light
#include "main.h" #include "stm32f0xx_hal.h"   /* USER CODE BEGIN Includes */ int i=0; /* USER CODE END Includes */   /* Private variables ---------------------------------------------------------*/ TIM_HandleTypeDef htim3;   UART_HandleTypeDef huart1;   /* USER CODE BEGIN PV */ /* Private variables -----------------
[Microcontroller]
stm32 pwm breathing light
STM32 serial port DMA receives variable length data
introduction When using STM32 or other microcontrollers, serial communication is often used. So how to receive data effectively? If the data is of indefinite length, how to receive it efficiently? Student A: When data comes, it will enter the serial port interrupt, and we just need to read the data in the interrupt!
[Microcontroller]
STM32 serial port DMA receives variable length data
The difference between STM32 startup code __main and user main program main()
1. __main function The __main function is a function of the C/C++ runtime library. The embedded system must have an initialization process before entering the main application program. When using the __main label to boot the system, the entry point of the application must be defined as main().      During the initiali
[Microcontroller]
The difference between STM32 startup code __main and user main program main()
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号