一、项目创建
- 选择芯片
- SYS设置
- RCC设置
- 时钟配置,输入72后回车
- USART1设置
将Mode设置为Asynchronous: 使串口中断: - 设置project,并打开项目
二、代码编写
#define LENGTH 10
uint8_t RxBuffer[LENGTH];
uint8_t Rxflag = 0;
HAL_UART_Receive_IT(&huart1, (uint8_t*)RxBuffer, LENGTH);
char data[] = "\nReveive Success\n";
if (Rxflag == 1){
Rxflag = 0;
HAL_UART_Transmit(&huart1, (uint8_t *)data, 18, 0xFFFF);
HAL_UART_Transmit_IT(&huart1, (uint8_t*)RxBuffer, LENGTH);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
if (huart->Instance == USART1){
Rxflag = 1;
HAL_UART_Receive_IT(&huart1, (uint8_t*)&RxBuffer, LENGTH);
}
}
三、运行及结果
四、总结
成功实现以中断方式实现串口通信
参考文章
STM32之中断方式实现串口通信
|