stm32f103c8t6配置USART2
时间: 2023-10-15 14:08:07 浏览: 144
好的,以下是配置USART2的步骤:
1. 首先需要开启USART2的时钟,可以使用RCC_APB1PeriphClockCmd函数来实现,例如:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
2. 配置USART2的GPIO引脚,将其设置为复用模式,并设置相应的速率、数据位数、停止位数和校验位等参数。例如:
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
3. 最后开启USART2,可以使用USART_Cmd函数来实现,例如:
USART_Cmd(USART2, ENABLE);
阅读全文