当前位置:网站首页>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);}边栏推荐
- Publish module to NPM should be how to operate?Solutions to problems and mistake
- How to solve Win11 without local users and groups
- jest test, component test
- 关于c语言的调试技巧
- [System Design and Implementation] Flink-based distracted driving prediction and data analysis system
- Win11电脑一段时间不操作就断网怎么解决
- BLE蓝牙5.2-PHY6222系统级芯片(SoC)智能手表/手环
- General syntax and usage instructions of SQL (picture and text)
- MATLAB绘图函数plot详解
- pygame image rotate continuously
猜你喜欢

STM32LL库使用——SPI通信

Codeforces Round #605 (Div. 3)

How to simulate 1/3 probability with coins, and arbitrary probability?

将SSE指令转换为ARM NEON指令

Win11 keeps popping up User Account Control how to fix it

win11一直弹出用户账户控制怎么解决

The SSE instructions into ARM NEON

Win11 computer off for a period of time without operating network how to solve

关于c语言的调试技巧

6.统一记录日志
随机推荐
What is Win10 God Mode for?How to enable God Mode in Windows 10?
MATLAB绘制平面填充图入门详解
13.56MHZ刷卡芯片CI521兼容cv520/ci520支持A卡B卡MIFARE协议
推开机电的大门《电路》(二):功率计算与判断
FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
Win10 cannot directly use photo viewer to open the picture
LORA芯片ASR6505无线远距离传输8位MCU
【STM32学习1】基础知识与概念明晰
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
模板系列-并查集
背包问题-动态规划-理论篇
Mysql连接错误解决
使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
MATLAB图形加标注的基本方法入门简介
Please make sure you have the correct access rights and the repository exists. Problem solved
win11一直弹出用户账户控制怎么解决
DP4056电源保护芯片锂电池pin对pinTP4056
Win11 keeps popping up User Account Control how to fix it
What should I do if the Win10 system sets the application identity to automatically prompt for access denied?
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer