stm32串口3初始化
时间: 2023-07-25 16:33:07 浏览: 67
以下是基于HAL库的STM32串口3初始化代码:
```
// 使能串口3时钟
__HAL_RCC_USART3_CLK_ENABLE();
// 配置GPIO
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11; // RX: PB11, TX: PB10
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// 配置USART3
USART_HandleTypeDef huart3 = {0};
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK) {
Error_Handler();
}
```
以上代码将串口3初始化为115200波特率,8位数据位,1位停止位,无校验位,无硬件流控制,使用GPIOB的PB10和PB11引脚作为TX和RX。
阅读全文