【STM32】引脚配置—F1与F4系列( 二 )


不同外设的使能函数不同 , 不同系列芯片同种外设的使能函数也有差异 , 这与芯片内部构造有关 。在".h"(F4系列:”.h”)头文件中可以找到多种使能函数:
voidRCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState);voidRCC_AHB2PeriphClockCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState);voidRCC_AHB3PeriphClockCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState);voidRCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);voidRCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
函数都有两个参数 , 第一个参数是外设 , 第二个参数是新状态(使能()或不使能()) 。既然不同外设的使能函数不同 , 那么怎么选择函数呢?
关于使能函数的选择
这里介绍两种本人常用方法(当然记住最好):
第一种方法:
在".c"源文件中找到该函数 , 在函数的上面就有介绍 , 例如:找到介绍有,  , 就说明该函数就是使能复用(AFIO)及GPIOA外设的 。
第二种方法:
在”.h”头文件中找相关参数定义 。例如:要使能GPIOA;则找到这个 , 对照前面的APB2就知道要找的函数为:
voidRCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
3、完整的GPIO初始化步骤 (1)用作普通IO
a、首先使能外设时钟
/*Stm32f10x*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);//使能PC,PD端口时钟/*Stm32f4xx*/RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟
b、调用void ();初始化引脚
()函数的入口参数为结构体变量 , 所以先要定义一个结构体变量并赋值 , 然后调用void ()完成初始化:
/*Stm32f10x*/GPIO_InitTypeDefGPIO_InitStructure;//定义结构体变量GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;//C13端口GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//IO口速度为50MHzGPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOC.13/*Stm32f4xx*/GPIO_InitTypeDefGPIO_InitStructure;//定义结构体变量GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;//F9、10端口GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化
c、完整的初始化程序
/*Stm32f10x*/GPIO_InitTypeDefGPIO_InitStructure;//定义结构体变量 , 变量要在程序之前定义RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE); //使能PC,PD端口时钟GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;//C13端口GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//IO口速度为50MHzGPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOC_13/*Stm32f4xx*/GPIO_InitTypeDefGPIO_InitStructure;//定义结构体变量RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);//使能GPIOF时钟GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;//GPIOF9,F10初始化设置GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉GPIO_Init(GPIOF, &GPIO_InitStructure);//初始化
(没学结构体之前学的stm32 , 在看到这里时并不理解 , 只能照着做 , 学完结构体以及相关知识后再看这里豁然开朗 , 这句也是个废话)
(2)作为复用IO(以串口为例)