基础发送:
uint8_t ch=0xAb;//1
uint8_t aRxBuffer[]="hello GKoSon\r\n";//2
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);//3
HAL_UART_Transmit(&huart1, (uint8_t *)aRxBuffer, 10,0xFFFF);//4
此后不再讨论发送,毕竟这个不是重难点,接受才是。
基础接受:
//HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);//可以发也可以不发
//HAL_UART_Transmit_IT(&huart1, (uint8_t *)aRxBuffer, sizeof(aRxBuffer));//可以发也可以不发
HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 10);//必须有这句话,貌似用了里面的代码,使能了中断!
重写接受函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Transmit(&huart1, (uint8_t *)aRxBuffer, 10,0xFFFF);
}
此时问题:1只能接受一次,以后中断不来了。2必须接受10个数才能进中断
优化如下:1每次接收以后再次开启接收中断,这样下次还可以进来啦 2修改为1个数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Transmit(&huart1, (uint8_t *)aRxBuffer, 1,0xFFFF);
HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 1);
}
|