当前位置:网站首页>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);}
边栏推荐
猜你喜欢
Configure clangd for vscode
Win11系统找不到dll文件怎么修复
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
开心一下,9/28名场面合集
推开机电的大门《电路》(一):电压,电流,参考方向
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
推开机电的大门《电路》(二):功率计算与判断
【STM32学习1】基础知识与概念明晰
推开机电的大门《电路》(三):说说不一样的电阻与电导
STM32LL库——USART中断接收不定长信息
随机推荐
Network Security Packet Capture
轻量化AlphaPose
Win10系统设置application identity自动提示拒绝访问怎么办
General syntax and usage instructions of SQL (picture and text)
CI24R1小模块2.4G收发模块无线通信低成本兼容si24r1/XN297超低功耗
mysql的索引结构为什么选用B+树?
Lightweight AlphaPose
Please make sure you have the correct access rights and the repository exists. Problem solved
pygame绘制弧线
二叉树创建之层次法入门详解
Article pygame drag the implementation of the method
Failed to install using npx -p @storybook/cli sb init, build a dedicated storybook by hand
flink+sklearn——使用jpmml实现flink上的机器学习模型部署
推开机电的大门《电路》(二):功率计算与判断
How to simulate 1/3 probability with coins, and arbitrary probability?
刷卡芯片CI520可直接PIN对PIN替换CV520支持SPI通讯接口
Win11声卡驱动如何更新?Win11声卡驱动更新方法
What should I do if I install a solid-state drive in Win10 and still have obvious lags?
项目:数据库表的梳理
TypeScript 快速进阶