OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 6565|回复: 3

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)的若干问题

[复制链接]

2

主题

40

帖子

0

精华

初级会员

Rank: 2

积分
99
金钱
99
注册时间
2021-5-15
在线时间
14 小时
发表于 2021-5-16 22:10:11 | 显示全部楼层 |阅读模式
5金钱
本帖最后由 STM2021 于 2021-5-17 16:23 编辑

实际使用这个函数时写为:


/***按键输入程序,端口设置为输入***/
void KEY_Init(void) //IO初始化
{
         GPIO_InitTypeDef GPIO_InitStructure;

         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE);//使能PORTA,PORTE时钟


        GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;//KEY0-KEY2
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
         GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOE2,3,4


        //初始化 WK_UP-->GPIOA.0          下拉输入
        GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉         
        GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.0


}
***********对比:
/***跑马灯实验,端口设置为输出***/
//初始化PB5和PE5为输出口.并使能这两个口的时钟                    
//LED IO初始化
void LED_Init(void)
{

GPIO_InitTypeDef  GPIO_InitStructure;
         
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);         //使能PB,PE端口时钟
        
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                                 //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                 //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);                                         //根据设定参数初始化GPIOB.5
GPIO_SetBits(GPIOB,GPIO_Pin_5);                                                 //PB.5 输出高


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                             //LED1-->PE.5 端口配置, 推挽输出
GPIO_Init(GPIOE, &GPIO_InitStructure);                                           //推挽输出 ,IO口速度为50MHz
GPIO_SetBits(GPIOE,GPIO_Pin_5);                                                  //PE.5 输出高

}


***继续看void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)***
它的内部:
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
  uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
  uint32_t tmpreg = 0x00, pinmask = 0x00;
  /* Check the parameters */
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));  



}

其中的第一个传入参数:结构体GPIO_TypeDef
typedef struct
{
  __IO uint32_t CRL;
  __IO uint32_t CRH;
  __IO uint32_t IDR;
  __IO uint32_t ODR;
  __IO uint32_t BSRR;
  __IO uint32_t BRR;
  __IO uint32_t LCKR;
} GPIO_TypeDef;
这个结构体有这么多变量,但实际使用比如用GPIOA,那么一个GPIOA就可以了,好奇怪啊!


其中的第二个传入参数:结构体GPIO_InitTypeDef
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;
这个结构体也有这么多变量,实际使用时,需要设定GPIO_Pin、GPIO_Speed、GPIO_Mode,此时就不是一个变量就可以的了,好奇怪啊!!!















最佳答案

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

2

主题

40

帖子

0

精华

初级会员

Rank: 2

积分
99
金钱
99
注册时间
2021-5-15
在线时间
14 小时
 楼主| 发表于 2021-5-16 22:10:12 | 显示全部楼层
回复

使用道具 举报

2

主题

40

帖子

0

精华

初级会员

Rank: 2

积分
99
金钱
99
注册时间
2021-5-15
在线时间
14 小时
 楼主| 发表于 2021-5-20 16:17:34 | 显示全部楼层
发好几天连看都没人看?这论坛凉了?
回复

使用道具 举报

2

主题

40

帖子

0

精华

初级会员

Rank: 2

积分
99
金钱
99
注册时间
2021-5-15
在线时间
14 小时
 楼主| 发表于 2021-5-20 16:18:09 | 显示全部楼层
发好几天了都没人看?这论坛凉了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-23 10:52

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表