- 开启串口中断服务函数;
- 在stm32xx_hal_it中USART_IRQHndler()函数的添加我们自己的Usart2_IRQ()函数;
uint8_t rxConut= 0;
uint8_t RxBuffer[32];
uint8_t uart_flag = 0;
void Uart2_IRQ(void)
{
uint8_t res = 0;
if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_RXNE) != RESET)
{
HAL_UART_Receive(&huart2,&res,1,1000);
if(rxConut < RX_BUFFER_SIZE)
{
RxBuffer[rxConut] = res;
rxConut++;
}
__HAL_UART_CLEAR_FLAG(&huart2,UART_FLAG_RXNE);
}
if(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_IDLE) != RESET)
{
while(__HAL_UART_GET_FLAG(&huart2,UART_FLAG_TC) != SET);
HAL_UART_Transmit(&huart2,pData,len,1000);
rxConut = 0;
__HAL_UART_CLEAR_IDLEFLAG(&huart2);
}
}
- 在MX_USART2_UART_Init函数中添加:
__HAL_UART_ENABLE_IT(&huart2,UART_IT_RXNE);
__HAL_UART_ENABLE_IT(&huart2,UART_IT_IDLE);
- printf输出函数
int fputc(int ch, FILE *f)
{
uint8_t temp[1] = {ch};
HAL_UART_Transmit(&huart1,temp,1,2);
}