版权声明:博主的博客不值钱随便转载但要注明出处 https://blog.csdn.net/easylovecsdn/article/details/81839334
串口初始化的一般步骤:
1.GPIO时钟使能RCC_AHB
2.USART时钟使能RCC_APB
3.USART端口配置(调用GPIO_Init)
4.USART对应引脚复用映射GPIO_PinAFConfig
以PA9、PA10复用USART1为例,具体过程见代码:
//关于时钟使能RCC函数名称的含义:
//AHBx、APBx是总线名称
//寻找相关GPIO口对应的时钟使能函数的技巧:
//在stm32f4xx_rcc.h下查找该GPIO口
void usart_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能GPIO时钟
RCC_AHB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能USART1时钟
//USART1端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9 | GPIO_PinSource10, GPIO_AF_USART1); //USART1对应引脚复用映射
}