keil5的stm32串口3初始化
时间: 2023-12-06 22:19:28 浏览: 118
以下是基于Keil5的STM32串口3初始化代码:
```
// 使能串口3时钟
RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
// 配置GPIO
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
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_InitTypeDef USART_InitStruct = {0};
USART_InitStruct.BaudRate = 115200;
USART_InitStruct.WordLength = USART_WORDLENGTH_8B;
USART_InitStruct.StopBits = USART_STOPBITS_1;
USART_InitStruct.Parity = USART_PARITY_NONE;
USART_InitStruct.Mode = USART_MODE_TX_RX;
USART_InitStruct.HwFlowCtl = USART_HWCONTROL_NONE;
USART_InitStruct.OverSampling = USART_OVERSAMPLING_16;
USART_Init(USART3, &USART_InitStruct);
USART_Cmd(USART3, ENABLE);
```
以上代码将串口3初始化为115200波特率,8位数据位,1位停止位,无校验位,无硬件流控制,使用GPIOB的PB10和PB11引脚作为TX和RX。注意,在Keil5中,GPIO初始化函数为HAL_GPIO_Init。
阅读全文