一、 STM32端口复用
1. 端口复用定义:STM32有很多的内置外设,这些外设的外部引脚都是与GPIO复用。也就是说,一个GPIO如果可以复用为内置外设的功能引脚,那么当这个GPIO作为内置外设使用的时候,就叫做复用。
2. 作用:最大限度的利用端口资源
3. 以PA9,PA10配置为串口1为例
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- 复用外设时钟使能(如将端口PA9,PA10复用为串口,所以要使能串口时钟)。
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
- 端口模式配置。GPIO_Init()函数。
4. 端口复用配置过程代码及图示
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //IO时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //外设时钟使能
//初始化IO为对应的模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;//PA9 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10 浮空输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);