生成stm32f103c8t6配置串口3的代码
时间: 2023-08-27 17:06:45 浏览: 52
下面是生成stm32f103c8t6配置串口3的代码:
```
#include "stm32f10x.h"
void USART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
// Enable GPIO clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// Enable USART3 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
// Configure USART3 Tx pin as alternate function push-pull
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// Configure USART3 Rx pin as input floating
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// Configure USART3
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStruct);
// Enable USART3
USART_Cmd(USART3, ENABLE);
}
```
在这里,我们首先启用了GPIO和USART3的时钟,并对GPIO和USART3进行了初始化。我们使用GPIOB的Pin 10作为USART3的Tx引脚,GPIOB的Pin 11作为USART3的Rx引脚。我们将USART3的波特率设置为9600,数据位长度为8位,停止位为1位,无奇偶校验位,无硬件流控制,同时启用接收和发送模式。最后,我们启用USART3。