【STM32】STM32学习笔记-GPIO相关API概述(06-1)
作者:mmseoamin日期:2023-12-19

00. 目录

文章目录

    • 00. 目录
    • 01. GPIO概述
    • 02. GPIO_Exported_Types
    • 03. GPIOSpeed_TypeDef
    • 04. GPIOMode_TypeDef
    • 05. GPIO_InitTypeDef
    • 06. BitAction
    • 07. GPIO_pins_define
    • 08. GPIO_Pin_sources
    • 09. GPIO_Port_Sources
    • 10. GPIO相关函数汇总
    • 11. GPIO_DeInit
    • 12. GPIO_AFIODeInit
    • 13. GPIO_Init
    • 14. GPIO_StructInit
    • 15. GPIO_ReadInputDataBit
    • 16. GPIO_ReadInputData
    • 17. GPIO_ReadOutputDataBit
    • 18. GPIO_ReadOutputData
    • 19. GPIO_SetBits
    • 20. GPIO_ResetBits
    • 21. GPIO_WriteBit
    • 22. GPIO_Write
    • 23. RCC_APB2PeriphClockCmd
    • 24. 附录

      【STM32】STM32学习笔记-GPIO相关API概述(06-1),在这里插入图片描述,第1张

      01. GPIO概述

      文件:stm32f10x_gpio.h和stm32f10x_gpio.c

      02. GPIO_Exported_Types

      /** @defgroup GPIO_Exported_Types
        * @{
        */
      #define IS_GPIO_ALL_PERIPH(PERIPH) (((PERIPH) == GPIOA) || \
                                          ((PERIPH) == GPIOB) || \
                                          ((PERIPH) == GPIOC) || \
                                          ((PERIPH) == GPIOD) || \
                                          ((PERIPH) == GPIOE) || \
                                          ((PERIPH) == GPIOF) || \
                                          ((PERIPH) == GPIOG))
      

      03. GPIOSpeed_TypeDef

      /** 
        * @brief  Output Maximum frequency selection  
        */
      typedef enum
      { 
        GPIO_Speed_10MHz = 1,
        GPIO_Speed_2MHz, 
        GPIO_Speed_50MHz
      }GPIOSpeed_TypeDef;
      

      04. GPIOMode_TypeDef

      /** 
        * @brief  Configuration Mode enumeration  
        */
      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;
      

      05. GPIO_InitTypeDef

      /** 
        * @brief  GPIO Init structure 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;
      

      06. BitAction

      /** 
        * @brief  Bit_SET and Bit_RESET enumeration  
        */
      typedef enum
      { Bit_RESET = 0,
        Bit_SET
      }BitAction;
      

      07. GPIO_pins_define

      /** @defgroup GPIO_pins_define 
        * @{
        */
      #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 */
      

      08. GPIO_Pin_sources

      /** @defgroup GPIO_Pin_sources 
        * @{
        */
      #define GPIO_PinSource0            ((uint8_t)0x00)
      #define GPIO_PinSource1            ((uint8_t)0x01)
      #define GPIO_PinSource2            ((uint8_t)0x02)
      #define GPIO_PinSource3            ((uint8_t)0x03)
      #define GPIO_PinSource4            ((uint8_t)0x04)
      #define GPIO_PinSource5            ((uint8_t)0x05)
      #define GPIO_PinSource6            ((uint8_t)0x06)
      #define GPIO_PinSource7            ((uint8_t)0x07)
      #define GPIO_PinSource8            ((uint8_t)0x08)
      #define GPIO_PinSource9            ((uint8_t)0x09)
      #define GPIO_PinSource10           ((uint8_t)0x0A)
      #define GPIO_PinSource11           ((uint8_t)0x0B)
      #define GPIO_PinSource12           ((uint8_t)0x0C)
      #define GPIO_PinSource13           ((uint8_t)0x0D)
      #define GPIO_PinSource14           ((uint8_t)0x0E)
      #define GPIO_PinSource15           ((uint8_t)0x0F)
      

      09. GPIO_Port_Sources

      /** @defgroup GPIO_Port_Sources 
        * @{
        */
      #define GPIO_PortSourceGPIOA       ((uint8_t)0x00)
      #define GPIO_PortSourceGPIOB       ((uint8_t)0x01)
      #define GPIO_PortSourceGPIOC       ((uint8_t)0x02)
      #define GPIO_PortSourceGPIOD       ((uint8_t)0x03)
      #define GPIO_PortSourceGPIOE       ((uint8_t)0x04)
      #define GPIO_PortSourceGPIOF       ((uint8_t)0x05)
      #define GPIO_PortSourceGPIOG       ((uint8_t)0x06)
      

      10. GPIO相关函数汇总

      void GPIO_DeInit(GPIO_TypeDef* GPIOx);
      void GPIO_AFIODeInit(void);
      void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
      void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
      uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
      uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
      uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
      uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
      void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
      void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
      void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
      void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
      void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
      void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
      void GPIO_EventOutputCmd(FunctionalState NewState);
      void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);
      void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
      void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface);
      

      11. GPIO_DeInit

      void GPIO_DeInit(GPIO_TypeDef* GPIOx);
      功能:
      	将外设 GPIOx 寄存器重设为缺省值
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设
      返回值:
      	无
      

      12. GPIO_AFIODeInit

      void GPIO_AFIODeInit(void);
      功能:
      	将复用功能(重映射事件控制和 EXTI 设置)重设为缺省值
      参数:
      	无
      返回值:
      	无
      

      13. GPIO_Init

      void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
      功能:
      	根据 GPIO_InitStruct 中指定的参数初始化外设 GPIOx 寄存器
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设
          GPIO_InitStruct:指向结构 GPIO_InitTypeDef 的指针,包含了外设 GPIO 的配置信息    
      返回值:
      	无
      

      14. GPIO_StructInit

      void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
      功能:
      	把 GPIO_InitStruct 中的每一个参数按缺省值填入
      参数:
      	GPIO_InitStruct:指向结构 GPIO_InitTypeDef 的指针,待初始化   
      返回值:
      	无
      

      参考实现

      /**
        * @brief  Fills each GPIO_InitStruct member with its default value.
        * @param  GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will
        *         be initialized.
        * @retval None
        */
      void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct)
      {
        /* Reset GPIO init structure parameters values */
        GPIO_InitStruct->GPIO_Pin  = GPIO_Pin_All;
        GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
      }
      

      15. GPIO_ReadInputDataBit

      uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
      功能:
      	读取指定端口管脚的输入
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设
          GPIO_Pin:待读取的端口位    
      返回值:
      	输入端口管脚值
      

      16. GPIO_ReadInputData

      uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
      功能:
      	读取指定的 GPIO 端口输入
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设
      返回值:
      	GPIO输入数据端口值
      

      17. GPIO_ReadOutputDataBit

      uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
      功能:
      	读取指定端口管脚的输出
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设
          GPIO_Pin:待读取的端口位    
      返回值:
      	输出端口管脚值
      

      18. GPIO_ReadOutputData

      uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
      功能:
      	读取指定的 GPIO端口输出
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设 
      返回值:
      	GPIO输出数据端口值
      

      19. GPIO_SetBits

      void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
      功能:
      	设置指定的数据端口位
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设
          GPIO_Pin:待设置的端口位    
      返回值:
      	无
      

      20. GPIO_ResetBits

      void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
      功能:
      	清除指定的数据端口位
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设
          GPIO_Pin:待设置的端口位    
      返回值:
      	无
      

      21. GPIO_WriteBit

      void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
      功能:
      	设置或者清除指定的数据端口位
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设
          GPIO_Pin:待设置或者清除指的端口位 
      返回值:
      	无
      

      22. GPIO_Write

      void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
      功能:
      	向指定 GPIO 数据端口写入数据
      参数:
      	GPIOx:x 可以是 A,B,C,D 或者 E,来选择 GPIO 外设
          PortVal: 待写入端口数据寄存器的值
      返回值:
      	无
      

      23. RCC_APB2PeriphClockCmd

      /**
        * @brief  Enables or disables the High Speed APB (APB2) peripheral clock.
        * @param  RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
        *   This parameter can be any combination of the following values:
        *     @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
        *          RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
        *          RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
        *          RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
        *          RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
        *          RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,
        *          RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11     
        * @param  NewState: new state of the specified peripheral clock.
        *   This parameter can be: ENABLE or DISABLE.
        * @retval None
        */
      void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
      功能:
      	使能或者失能 APB2 外设时钟
      参数:
      	RCC_APB2Periph: 门控 APB2 外设时钟
          NewState:指定外设时钟的新状态
      返回值:
      	无    
      

      24. 附录

      参考: 【STM32】江科大STM32学习笔记汇总