STM32 GPIO初始化的学习

一、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的学习中学会看定义是非常重要的(个人理解)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值