当前位置:网站首页>STM32LL library - USART interrupt to receive variable length information
STM32LL library - USART interrupt to receive variable length information
2022-08-02 15:29:00 【Tianshan has no longevity tea】
The Universal Synchronous Asynchronous Receiver (USART) is both a serial port and can be connected with the serial port assistant on the computer, which is an important way to debug the microcontroller.Two-way communication requires at least 2 pins: receive data input (RX) and transmit data output (TX).
First, configure the basic parameters of using the serial port in STM32CubeMX, enable the serial port interrupt, and set the library to be used as the LL library:
After the basic code is automatically generated, add the following code to enable the receive buffer Rx non-empty interrupt and idle interrupt:
LL_USART_EnableIT_RXNE(USART2);LL_USART_EnableIT_IDLE(USART2);
RXNE is the reception buffer Rx non-empty flag, set to 1 to indicate that data (1 byte) has been received, and an interrupt is generated; IDLE is an idle flag, set to 1 to indicate that the current data is received (1 frame of data), and an interrupt is generated.Receive variable length messages:
char data[20];uint8_t Rflag = 0;uint8_t Rnum = 0;// We store the received data in the string array data[], Rnum indicates the data length, Rflag indicates the receiving status, = 1 means the reception is completevoid USART2_IRQHandler(void){/* USER CODE BEGIN USART2_IRQn 0 */// Idle interrupt, indicating that all data of 1 frame has been receivedif(LL_USART_IsActiveFlag_IDLE(USART2)){// clear interrupt flagLL_USART_ClearFlag_IDLE(USART2);data[Rnum] = '\0';Rflag = 1;Rnum = 0;}// Rx non-null interrupt, indicating that a byte was received// Read Rx to automatically clear the interrupt flagif(LL_USART_IsActiveFlag_RXNE(USART2)){// If the length of the array is greater than the set maximum length of data, stop receiving directlyif(Rnum > 18){data[19] = '\0';Rflag = 1; // stop receiveRnum = 0; // wait for the next data}if(Rflag == 0){data[Rnum] = LL_USART_ReceiveData8(USART2);Rnum++;}}WRITE_REG(USART2->RQR, USART_RQR_RXFRQ);/* USER CODE END USART2_IRQn 0 *//* USER CODE BEGIN USART2_IRQn 1 *//* USER CODE END USART2_IRQn 1 */}
The relevant LL library functions are defined as follows:
/* Legacy define */#define LL_USART_EnableIT_RXNE LL_USART_EnableIT_RXNE_RXFNE/*** @brief Enable RX Not Empty and RX FIFO Not Empty Interrupt* @note Macro IS_UART_FIFO_INSTANCE(USARTx) can be used to check whether or not* FIFO mode feature is supported by the USARTx instance.* @rmtoll CR1 RXNEIE_RXFNEIE LL_USART_EnableIT_RXNE_RXFNE* @param USARTx USART Instance* @retval None*/__STATIC_INLINE void LL_USART_EnableIT_RXNE_RXFNE(USART_TypeDef *USARTx){SET_BIT(USARTx->CR1, USART_CR1_RXNEIE_RXFNEIE);}/*** @brief Enable IDLE Interrupt* @rmtoll CR1 IDLEIE LL_USART_EnableIT_IDLE* @param USARTx USART Instance* @retval None*/__STATIC_INLINE void LL_USART_EnableIT_IDLE(USART_TypeDef *USARTx){SET_BIT(USARTx->CR1, USART_CR1_IDLEIE);}/*** @brief Read Receiver Data register (Receive Data value, 8 bits)* @rmtoll RDR RDR LL_USART_ReceiveData8* @param USARTx USART Instance* @retval Value between Min_Data=0x00 and Max_Data=0xFF*/__STATIC_INLINE uint8_t LL_USART_ReceiveData8(USART_TypeDef *USARTx){return (uint8_t)(READ_BIT(USARTx->RDR, USART_RDR_RDR) & 0xFFU);}
边栏推荐
- MATLAB绘图函数fplot详解
- Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
- Codeforces Round #624 (Div. 3)
- Mysql connection error solution
- flink+sklearn——使用jpmml实现flink上的机器学习模型部署
- Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
- MATLAB图形加标注的基本方法入门简介
- 【STM32学习1】基础知识与概念明晰
- Flink + sklearn - use JPMML implement flink deployment on machine learning model
- What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer
猜你喜欢
随机推荐
Mysql connection error solution
Win10系统设置application identity自动提示拒绝访问怎么办
基于矩阵计算的线性回归分析方程中系数的估计
Detailed introduction to drawing complex surfaces using the plot_surface command
专硕与学硕
关于c语言的调试技巧
实战美团Nuxt +Vue全家桶,服务端渲染,邮箱验证,passport鉴权服务,地图API引用,mongodb,redis等技术点
模板系列-二分
Win10 computer can't read U disk?Don't recognize U disk how to solve?
win10任务栏不合并图标如何设置
Codeforces Round #605 (Div. 3)
深入理解Golang之Map
Golang 垃圾回收机制详解
win11一直弹出用户账户控制怎么解决
How to solve Win11 without local users and groups
系统线性、时不变、因果判断
Publish module to NPM should be how to operate?Solutions to problems and mistake
How to add a one-key shutdown option to the right-click menu in Windows 11
The SSE instructions into ARM NEON
Win10 cannot directly use photo viewer to open the picture