一、GPIO的端口初始化即函数GPIO_Init(GPIO_TypeDef GPIOx, GPIO_InitTypeDef GPIO_InitStruct)**
1. GPIO_Init()初始化涉及两个参数:
GPIOx:控制IO口的寄存器组的基地址
GPIO_InitStruct:一个结构体指针所需参数是地址,成员内容可以看 GPIO_InitTypeDef的定义,操作方法即选中右键Go to definition即可看到结构体成员
typedef struct
{
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
以初始化GPIOA端口5号和3号管脚为例
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //**使能PC端时钟端口,使用端口前需将其使能**,
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIO初始化
注:使能函数要放在结构体声明之后放在声明之前会出现…\HARDWARE\led.c(5): error: #268: declaration may not appear after executable statement in block的错误原因不明
2.GPIO的函数声明在stm32的gpio.h文件的最下面可以看到可以进入GPIO_Init()的函数体内查询所需的参数,进入方法与上面相同,
例,进入函数体后看所需参数:assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
选中IS_GPIO_ALL_PERIPH
右键Go to definition查看,会出现
#define IS_GPIO_ALL_PERIPH(PERIPH) (((PERIPH) == GPIOA) || \
((PERIPH) == GPIOB) || \
((PERIPH) == GPIOC) || \
((PERIPH) == GPIOD) || \
((PERIPH) == GPIOE) || \
((PERIPH) == GPIOF) || \
((PERIPH) == GPIOG))
以上选项,是可选的
同样结构体内容不知道到填写什么都可以去看函数库,在stm32的学习中学会看定义是非常重要的(个人理解)