空闲中断
main.h中定义
#define RX_BUF_MAX_LEN 512
typedef struct
{
uint8_t rx_buff[RX_BUF_MAX_LEN];
uint16_t len;
uint8_t flag;
} USRAT_RX;
main.c中定义
#include "string.h"
USRAT_RX usart_rx;
uint8_t recv_buf;
stm32g0xx_it.c中定义
extern uint8_t recv_buf;
extern USRAT_RX usart_rx;
直接修改中断函数
if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_RXNE) != RESET) {
/* USER CODE END USART2_IRQn 0 */
HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
usart_rx.rx_buff[usart_rx.len++] = recv_buf;
if(usart_rx.len>RX_BUF_MAX_LEN){
usart_rx.flag = 1;
}
}
if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE) != RESET) {
__HAL_UART_CLEAR_IDLEFLAG(&huart2);
usart_rx.flag = 1;
if(usart_rx.len==0){
usart_rx.flag = 0;
}
}
HAL_UART_Receive_IT(&huart2,&recv_buf,1);
main.c中初始化
memset(&usart_rx, 0, sizeof(USRAT_RX));
HAL_UART_Receive_IT(&huart2,&recv_buf,1);
__HAL_UART_ENABLE_IT(&huart2,UART_IT_IDLE);
while中添加
if(usart_rx.flag){
printf("%s",usart_rx.rx_buff);
memset(&usart_rx, 0, sizeof(USRAT_RX));
}
测试10个字节10ms发送基本没有问题