前言
STM32F4Discovery开发板,由于PA9复用成了USB OTG的vbus,线路上有电容,导致不能正常用作UART。 UART1不仅仅适用PA9和PA10,也可以使用PB6,PB7 STM32F4Discovery USART1 PA9不能使用.
环境
硬件
STM32F4Discovery
软件
STM32CubeMX V6.2.1
配置
配置UART1_TX跟UART1_RX口为PB6,PB7
代码
函数说明
发送函数:HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) 接收函数:HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
代码实例
if (HAL_UART_Receive(&huart1, rx_buff, sizeof(rx_buff),1000) == HAL_OK)
{
switch(rx_buff[0]){
case 0xA1:
set_voltage(DAC_CHANNEL_1, rx_buff[1]<<8 | rx_buff[2]);
get_voltage(DAC_CHANNEL_1);
HAL_UART_Transmit(&huart1, (uint8_t *)tx_buff, 2, 100);
break;
case 0xA2:
set_voltage(DAC_CHANNEL_2, rx_buff[1]<<8 | rx_buff[2]);
get_voltage(DAC_CHANNEL_2);
HAL_UART_Transmit(&huart1, (uint8_t *)tx_buff, 2, 100);
break;
case 0xB1:
get_voltage(DAC_CHANNEL_1);
HAL_UART_Transmit(&huart1, (uint8_t *)tx_buff, 2, 100);
break;
case 0xB2:
get_voltage(DAC_CHANNEL_2);
HAL_UART_Transmit(&huart1, (uint8_t *)tx_buff, 2, 100);
break;
default:
HAL_UART_Transmit(&huart1, rx_buff, sizeof(rx_buff),100);
break;
}
}
输出结果示例
|