一、CubeMX设置
R1设置为高电平熄灭状态
PB5的GPIO mode
RCC配置:配置时钟源为外部时钟源
配置时钟
配置引脚
设置中断
二、代码编写
在main中添加代码:
添加定义:
uint8_t aRxBuffer;
uint8_t Uart1_RxBuff[256];
uint8_t Uart1_Rx_Cnt = 0;
uint8_t cAlmStr[] = "数据溢出(大于256)\r\n";
在/* USER CODE BEGIN 2 */中添加如下代码:
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
在/* USER CODE BEGIN 4 */中添加如下代码:
/* USER CODE BEGIN 4 */
/**
* @brief Rx Transfer completed callbacks.
* @param huart pointer to a UART_HandleTypeDef structure that contains
* the configuration information for the specified UART module.
* @retval None
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
/* NOTE: This function Should not be modified, when the callback is needed,
the HAL_UART_TxCpltCallback could be implemented in the user file
*/
if(Uart1_Rx_Cnt >= 255) //溢出判断
{
Uart1_Rx_Cnt = 0;
for(int i=0;i<255;i++)
{
Uart1_RxBuff[i]=0;
}
HAL_UART_Transmit(&huart1, (uint8_t *)&cAlmStr, sizeof(cAlmStr),0xFFFF);
}
else
{
Uart1_RxBuff[Uart1_Rx_Cnt++] = aRxBuffer; //接收数据转存
if((Uart1_RxBuff[Uart1_Rx_Cnt-1] == 0x0A)&&(Uart1_RxBuff[Uart1_Rx_Cnt-2] == 0x0D)) //判断结束位
{
HAL_UART_Transmit(&huart1, (uint8_t *)&Uart1_RxBuff, Uart1_Rx_Cnt,0xFFFF); //将收到的信息发送出去
Uart1_Rx_Cnt = 0;
for(int i=0;i<255;i++)
{
Uart1_RxBuff[i]=0;
} //清空数组
}
}
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); //再开启接收中断
}
/* USER CODE END 4 */c
三、结果展示
四、参考博客
https://blog.csdn.net/qq_37286676/article/details/86556634
https://blog.csdn.net/sjxpf922/article/details/103979668