GPIO_init()函数初始化详解

目录

1.GPIO_init()函数初始化示例
1.1 GPIO_InitTypeDef
1.2 GPIO_Init(GPIOB, &GPIO_GPIO_InitStructure)
1.3 GPIO_InitStructure.GPIO_Pin
1.4 GPIO_InitStructure.GPIO_Mode
1.5 GPIO_InitStructure.GPIO_Speed

2.GPIO_init()函数
2.1 GPIO_TypeDef
2.2 GPIO_InitTypeDef
2.3 IS_GPIO_PIN
2.4 IS_GPIO_SPEED
2.5 IS_GPIO_MODE

1.GPIO_init()函数初始化示例

示例代码

//笔者使用的硬件平台为STM32F103ZET6战舰版
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //指定GPIO - 端口配置PB5 -> LED0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //指定模式 - 推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //指定速度 - IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_GPIO_InitStructure); //由设定的参数初始化GPIOB.5

结构体的创建框架

typedef struct 
{
	uint16_t GPIO_Pin;            //指定要初始化的IO口
	GPIOSpeed_TypeDef GPIO_Speed; //设置IO口输出速度
	GPIOMode_TypeDef GPIO_Mode;   //设置工作模式:8种中的一个
}GPIO_InitTypeDef;
1.1 GPIO_InitTypeDef

定义一个结构体类型

1.2 GPIO_Init(GPIOB, &GPIO_GPIO_InitStructure)

GPIOB 用来指定IO口, &GPIO_GPIO_InitStructure 是一个结构体指针类型。

1.3 GPIO_InitStructure.GPIO_Pin

初始化IO口

1.4 GPIO_InitStructure.GPIO_Mode

初始化IO口模式

1.5 GPIO_InitStructure.GPIO_Speed

初始化IO口速度

2.GPIO_init()函数

GPIO_Init()

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 Mode Configuration -----------------------*/
 currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
 if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
 { 
   /* Check the parameters */
   assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
   /* Output mode */
   currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
 }
/*---------------------------- GPIO CRL Configuration ------------------------*/
 /* Configure the eight low port pins */
 if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)
 {
   tmpreg = GPIOx->CRL;
   for (pinpos = 0x00; pinpos < 0x08; pinpos++)
   {
     pos = ((uint32_t)0x01) << pinpos;
     /* Get the port pins position */
     currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
     if (currentpin == pos)
     {
       pos = pinpos << 2;
       /* Clear the corresponding low control register bits */
       pinmask = ((uint32_t)0x0F) << pos;
       tmpreg &= ~pinmask;
       /* Write the mode configuration in the corresponding bits */
       tmpreg |= (currentmode << pos);
       /* Reset the corresponding ODR bit */
       if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
       {
         GPIOx->BRR = (((uint32_t)0x01) << pinpos);
       }
       else
       {
         /* Set the corresponding ODR bit */
         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
         {
           GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
         }
       }
     }
   }
   GPIOx->CRL = tmpreg;
 }
/*---------------------------- GPIO CRH Configuration ------------------------*/
 /* Configure the eight high port pins */
 if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
 {
   tmpreg = GPIOx->CRH;
   for (pinpos = 0x00; pinpos < 0x08; pinpos++)
   {
     pos = (((uint32_t)0x01) << (pinpos + 0x08));
     /* Get the port pins position */
     currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
     if (currentpin == pos)
     {
       pos = pinpos << 2;
       /* Clear the corresponding high control register bits */
       pinmask = ((uint32_t)0x0F) << pos;
       tmpreg &= ~pinmask;
       /* Write the mode configuration in the corresponding bits */
       tmpreg |= (currentmode << pos);
       /* Reset the corresponding ODR bit */
       if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
       {
         GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));
       }
       /* Set the corresponding ODR bit */
       if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
       {
         GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));
       }
     }
   }
   GPIOx->CRH = tmpreg;
 }
}

作用: 初始化一个或者多个IO口(同一组)的工作方式和速度。该函数主要是操作GPIO_CRL(CRH)寄存器,在上拉或者下拉的时候有设置BSRR或者BRR寄存器。

2.1 GPIO_TypeDef

用于选中指定的GPIO口

//定义GPIO的7个寄存器,用于选中指定的GPIO口
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;
2.2 GPIO_InitTypeDef

定义一个结构体类型,分别对IO口、IO口速度和IO口模式进行初始化

//定义一个结构体类型,分别对IO口、IO口速度和IO口模式进行初始化
typedef struct
{
  //指定IO口
  uint16_t GPIO_Pin;             /*!< Specifies the GPIO pins to be configured.
                                      This parameter can be any value of @ref GPIO_pins_define */
  //指定IO口的速度
  GPIOSpeed_TypeDef GPIO_Speed;  /*!< Specifies the speed for the selected pins.
                                      This parameter can be a value of @ref GPIOSpeed_TypeDef */
  //指定IO口模式(8种IO口模式中的1种)
  GPIOMode_TypeDef GPIO_Mode;    /*!< Specifies the operating mode for the selected pins.
                                      This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
2.3 IS_GPIO_PIN
//定义了有效的GPIO_Pin的范围0~15 + ALL
#define GPIO_Pin_0                 ((uint16_t)0x0001)  /*!< Pin 0 selected */
#define GPIO_Pin_1                 ((uint16_t)0x0002)  /*!< Pin 1 selected */
#define GPIO_Pin_2                 ((uint16_t)0x0004)  /*!< Pin 2 selected */
#define GPIO_Pin_3                 ((uint16_t)0x0008)  /*!< Pin 3 selected */
#define GPIO_Pin_4                 ((uint16_t)0x0010)  /*!< Pin 4 selected */
#define GPIO_Pin_5                 ((uint16_t)0x0020)  /*!< Pin 5 selected */
#define GPIO_Pin_6                 ((uint16_t)0x0040)  /*!< Pin 6 selected */
#define GPIO_Pin_7                 ((uint16_t)0x0080)  /*!< Pin 7 selected */
#define GPIO_Pin_8                 ((uint16_t)0x0100)  /*!< Pin 8 selected */
#define GPIO_Pin_9                 ((uint16_t)0x0200)  /*!< Pin 9 selected */
#define GPIO_Pin_10                ((uint16_t)0x0400)  /*!< Pin 10 selected */
#define GPIO_Pin_11                ((uint16_t)0x0800)  /*!< Pin 11 selected */
#define GPIO_Pin_12                ((uint16_t)0x1000)  /*!< Pin 12 selected */
#define GPIO_Pin_13                ((uint16_t)0x2000)  /*!< Pin 13 selected */
#define GPIO_Pin_14                ((uint16_t)0x4000)  /*!< Pin 14 selected */
#define GPIO_Pin_15                ((uint16_t)0x8000)  /*!< Pin 15 selected */
#define GPIO_Pin_All               ((uint16_t)0xFFFF)  /*!< All pins selected */

#define IS_GPIO_PIN(PIN) ((((PIN) & (uint16_t)0x00) == 0x00) && ((PIN) != (uint16_t)0x00))
2.4 IS_GPIO_SPEED
//定义枚举类型,列出3种可供选择的频率(速度)
typedef enum
{ 
  GPIO_Speed_10MHz = 1,
  GPIO_Speed_2MHz, 
  GPIO_Speed_50MHz
}GPIOSpeed_TypeDef;
#define IS_GPIO_SPEED(SPEED) (((SPEED) == GPIO_Speed_10MHz) || ((SPEED) == GPIO_Speed_2MHz) || \
                              ((SPEED) == GPIO_Speed_50MHz))
2.5 IS_GPIO_MODE

GPIO的8种工作模式

//定义8种模式
typedef enum
{ GPIO_Mode_AIN = 0x0,          //模拟输入
  GPIO_Mode_IN_FLOATING = 0x04, //浮空输入
  GPIO_Mode_IPD = 0x28,         //输入下拉
  GPIO_Mode_IPU = 0x48,         //输入上拉
  GPIO_Mode_Out_OD = 0x14,      //开漏输出
  GPIO_Mode_Out_PP = 0x10,      //推挽式输出
  GPIO_Mode_AF_OD = 0x1C,       //开漏复用输出
  GPIO_Mode_AF_PP = 0x18        //推挽式复用输出
}GPIOMode_TypeDef;

#define IS_GPIO_MODE(MODE) (((MODE) == GPIO_Mode_AIN) || ((MODE) == GPIO_Mode_IN_FLOATING) || \
                            ((MODE) == GPIO_Mode_IPD) || ((MODE) == GPIO_Mode_IPU) || \
                            ((MODE) == GPIO_Mode_Out_OD) || ((MODE) == GPIO_Mode_Out_PP) || \
                            ((MODE) == GPIO_Mode_AF_OD) || ((MODE) == GPIO_Mode_AF_PP))


—————————END—————————

回顾

GPIO的8种工作模式

————————————————————
  • 62
    点赞
  • 268
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
HAL_GPIO_Init 函数是一个在 STM32 系列单片机的 HAL 库中定义的函数,用于初始化 GPIO 口的配置。 其函数原型如下: ```c HAL_GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_Init) ``` 其中,GPIO_TypeDef* GPIOx 表示要初始化GPIO 口所在的 GPIOx 端口,例如 GPIOA、GPIOB 等,GPIO_InitTypeDef* GPIO_Init 表示 GPIO 口的配置信息,包括 GPIO 口的模式、输出类型、上拉下拉等参数。 在使用 HAL_GPIO_Init 函数初始化 GPIO 口时,需要先创建一个 GPIO_InitTypeDef 的结构体,然后根据需要设置 GPIO 口的各个参数,最后将结构体作为参数传入 HAL_GPIO_Init 函数中,即可完成 GPIO 口的初始化配置。 以下是 GPIO_InitTypeDef 结构体的定义: ```c typedef struct { uint32_t Pin; /* Specifies the GPIO pins to be configured. This parameter can be any value of @ref GPIO_pins_define */ uint32_t Mode; /* Specifies the operating mode for the selected pins. This parameter can be a value of @ref GPIO_mode_define */ uint32_t Pull; /* Specifies the Pull-up or Pull-Down activation for the selected pins. This parameter can be a value of @ref GPIO_pull_define */ uint32_t Speed; /* Specifies the speed for the selected pins. This parameter can be a value of @ref GPIO_speed_define */ uint32_t Alternate; /* Peripheral to be connected to the selected pins. This parameter can be a value of @ref GPIOEx_Alternate_function_selection */ } GPIO_InitTypeDef; ``` 其中,Pin 表示要配置的 GPIO 口的引脚编号,Mode 表示 GPIO 口的工作模式,Pull 表示 GPIO 口的上拉/下拉模式,Speed 表示 GPIO 口的速度,Alternate 表示 GPIO 口的复用功能。 总之,HAL_GPIO_Init 函数STM32 系列单片机 HAL 库中一个非常重要的函数,它能够帮助我们实现 GPIO 口的初始化和配置。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

學不董Gavin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值