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种工作模式

————————————————————

版权声明:本文为CSDN博主「學不董Gavin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/JAVADGAVIN/article/details/118146383

生成海报
点赞 0

學不董Gavin

我还没有学会写个人说明!

暂无评论

相关推荐

GPIO的8种工作模式详解

GPIO的8种工作模式详解浮空输入_IN_FLOATING 带上拉输入_IPU 带下拉输入_IPD 模拟输入_AIN 开漏输出_OUT_OD 推挽输出_OUT_PP 开漏复用输出_AF_OD 推挽复用输出_AF_PP 4输入 2 输出 2

基于STM32设计的实时心率检测仪

一、开发环境介绍 主控芯片:  STM32F103ZET6 代码编程软件: keil5 心率检测模块: PulseSensor WIFI模块: ESP8266 --可选的。直接使用串口有线传输给上位机也可以。 上位机:  C&#xff

基于STM32的室内环境监测系统

设计简介: 本设计是基于单片机的室内环境监测器,主要实现以下功能: 可实现LCD1602显示环境数据实时监测一氧化碳、甲烷、烟雾值空气质量大于各自限值报警,并通过TTS报警 标签&#x

基于stm32f407的示波器

一.设计要求 二.整体思路 硬件部分主要负责电压的缩放以及垂直灵敏度的控制,因为stm32的大部分引脚最高输入电压为3.3v,而要求的电压需要50v,需要进行电压缩放。 软件部分主要负责方波的实现&#x