一、cubemx配置
1.异步模式
2. 比特率 配置
二、核心代码(HAL库)
uint8_t receiveData[2];
while (1)
{
HAL_UART_Receive(&huart1,receiveData,2,HAL_MAX_DELAY); /*用huart1接受receiveData 延迟max_delay*/
HAL_UART_Transmit(&huart1,receiveData,2,100); /*用huart1发送receiveData 延迟100*/
GPIO_PinState state=GPIO_PIN_SET;
if (receiveData[1]=='0')
{state=GPIO_PIN_RESET;
}
if(receiveData[0]=='G'){
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_1,state);
}else if(receiveData[0]=='R'){
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_2,state);
}else if(receiveData[0]=='Y'){
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,state);
}else if(receiveData[0]=='W'){
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,state);
}
}