stm32f10x_gpio

STM32F10x GPIO(通用输入输出)标准库提供了丰富的函数接口,用于配置和控制GPIO引脚。本文档详细介绍了所有相关的函数、结构体和枚举类型。

数据类型定义

GPIO_InitTypeDef 结构体

typedef struct
{
  uint16_t GPIO_Pin;             /*!< 指定要配置的GPIO引脚
                                      此参数可以是 @ref GPIO_pins_define 中的任何值 */

  GPIOSpeed_TypeDef GPIO_Speed;  /*!< 指定所选引脚的速度
                                      此参数可以是 @ref GPIOSpeed_TypeDef 中的值 */

  GPIOMode_TypeDef GPIO_Mode;    /*!< 指定所选引脚的工作模式
                                      此参数可以是 @ref GPIOMode_TypeDef 中的值 */
}GPIO_InitTypeDef;

成员说明:

  • GPIO_Pin:要配置的GPIO引脚,可以是单个引脚或多个引脚的组合
  • GPIO_Speed:GPIO引脚的输出速度设置
  • GPIO_Mode:GPIO引脚的工作模式设置

GPIOSpeed_TypeDef 枚举

typedef enum
{ 
  GPIO_Speed_10MHz = 1,  /*!< 10MHz输出速度 */
  GPIO_Speed_2MHz,       /*!< 2MHz输出速度 */
  GPIO_Speed_50MHz       /*!< 50MHz输出速度 */
}GPIOSpeed_TypeDef;

GPIOMode_TypeDef 枚举

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;

BitAction 枚举

typedef enum
{ 
  Bit_RESET = 0,  /*!< 位复位 */
  Bit_SET         /*!< 位置位 */
}BitAction;

标准库函数详解

1.GPIO_DeInit

/**
 * @brief  将GPIO外设寄存器重设为默认值
 * @param  GPIOx: 选择要重置的GPIO外设
 * @retval
 * @example
 *     GPIO_DeInit(GPIOA);
 */
void GPIO_DeInit(GPIO_TypeDef* GPIOx);

2.GPIO_AFIODeInit

/**
 * @brief  将AFIO外设寄存器重设为默认值
 * @param
 * @retval
 * @example
 *     GPIO_AFIODeInit();
 */
void GPIO_AFIODeInit(void);

3.GPIO_Init

/**
 * @brief  根据GPIO_InitStruct中指定的参数初始化GPIOx外设
 * @param  GPIOx: 选择要初始化的GPIO外设
 * @param  GPIO_InitStruct: 指向GPIO_InitTypeDef结构的指针,包含指定GPIO外设的配置信息
 * @retval
 * @example
 *     GPIO_InitTypeDef GPIO_InitStructure;
 *     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
 *     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 *     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 *     GPIO_Init(GPIOA, &GPIO_InitStructure);
 */
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);

4.GPIO_StructInit

/**
 * @brief  将GPIO_InitStruct中的每个参数设为默认值
 * @param  GPIO_InitStruct: 指向GPIO_InitTypeDef结构的指针,将被初始化
 * @retval
 * @example
 *     GPIO_InitTypeDef GPIO_InitStructure;
 *     GPIO_StructInit(&GPIO_InitStructure);
 */
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);

5.GPIO_ReadInputDataBit

/**
 * @brief  读取指定GPIO端口的输入数据位
 * @param  GPIOx: 选择GPIO外设
 * @param  GPIO_Pin: 选择要读取的GPIO引脚
 * @retval 输入端口位值(Bit_SET或Bit_RESET)
 * @example
 *     uint8_t pinState = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
 */
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

6.GPIO_ReadInputData

/**
 * @brief  读取指定GPIO端口的输入数据
 * @param  GPIOx: 选择GPIO外设
 * @retval GPIO输入数据端口值
 * @example
 *     uint16_t portData = GPIO_ReadInputData(GPIOA);
 */
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);

7.GPIO_ReadOutputDataBit

/**
 * @brief  读取指定GPIO端口的输出数据位
 * @param  GPIOx: 选择GPIO外设
 * @param  GPIO_Pin: 选择要读取的GPIO引脚
 * @retval 输出端口位值(Bit_SET或Bit_RESET)
 * @example
 *     uint8_t outputState = GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_5);
 */
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

8.GPIO_ReadOutputData

/**
 * @brief  读取指定GPIO端口的输出数据
 * @param  GPIOx: 选择GPIO外设
 * @retval GPIO输出数据端口值
 * @example
 *     uint16_t outputData = GPIO_ReadOutputData(GPIOA);
 */
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);

9.GPIO_SetBits

/**
 * @brief  设置指定GPIO端口的指定引脚为高电平
 * @param  GPIOx: 选择GPIO外设
 * @param  GPIO_Pin: 选择要设置的GPIO引脚
 * @retval
 * @example
 *     GPIO_SetBits(GPIOA, GPIO_Pin_5);
 */
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

10.GPIO_ResetBits

/**
 * @brief  清除指定GPIO端口的指定引脚为低电平
 * @param  GPIOx: 选择GPIO外设
 * @param  GPIO_Pin: 选择要清除的GPIO引脚
 * @retval
 * @example
 *     GPIO_ResetBits(GPIOA, GPIO_Pin_5);
 */
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

11.GPIO_WriteBit

/**
 * @brief  设置或清除指定的数据端口位
 * @param  GPIOx: 选择GPIO外设
 * @param  GPIO_Pin: 选择要写入的GPIO引脚
 * @param  BitVal: 选择要写入的值(Bit_SET或Bit_RESET)
 * @retval
 * @example
 *     GPIO_WriteBit(GPIOA, GPIO_Pin_5, Bit_SET);
 */
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);

12.GPIO_Write

/**
 * @brief  向指定的GPIO数据端口写入数据
 * @param  GPIOx: 选择GPIO外设
 * @param  PortVal: 要写入端口输出数据寄存器的值
 * @retval
 * @example
 *     GPIO_Write(GPIOA, 0x00FF);
 */
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);

13.GPIO_PinLockConfig

/**
 * @brief  锁定GPIO引脚配置寄存器
 * @param  GPIOx: 选择GPIO外设
 * @param  GPIO_Pin: 选择要锁定的GPIO引脚
 * @retval
 * @example
 *     GPIO_PinLockConfig(GPIOA, GPIO_Pin_5);
 */
void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

14.GPIO_EventOutputConfig

/**
 * @brief  选择GPIO引脚用作事件输出
 * @param  GPIO_PortSource: 选择用作事件输出的GPIO端口源
 * @param  GPIO_PinSource: 选择用作事件输出的GPIO引脚源
 * @retval
 * @example
 *     GPIO_EventOutputConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
 */
void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);

15.GPIO_EventOutputCmd

/**
 * @brief  使能或失能事件输出
 * @param  NewState: 事件输出新状态(ENABLE或DISABLE)
 * @retval
 * @example
 *     GPIO_EventOutputCmd(ENABLE);
 */
void GPIO_EventOutputCmd(FunctionalState NewState);

16.GPIO_PinRemapConfig

/**
 * @brief  改变指定引脚的映射
 * @param  GPIO_Remap: 选择要配置的重映射
 * @param  NewState: 重映射新状态(ENABLE或DISABLE)
 * @retval
 * @example
 *     GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
 */
void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);

17.GPIO_EXTILineConfig

/**
 * @brief  选择GPIO引脚用作外部中断线
 * @param  GPIO_PortSource: 选择用作外部中断线的GPIO端口源
 * @param  GPIO_PinSource: 选择用作外部中断线的GPIO引脚源
 * @retval
 * @example
 *     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
 */
void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);

18.GPIO_ETH_MediaInterfaceConfig

/**
 * @brief  配置以太网媒体接口
 * @param  GPIO_ETH_MediaInterface: 选择以太网媒体接口
 * @retval
 * @example
 *     GPIO_ETH_MediaInterfaceConfig(GPIO_ETH_MediaInterface_RMII);
 */
void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface);

常用宏定义

GPIO引脚定义

#define GPIO_Pin_0                 ((uint16_t)0x0001)  /*!< 选择引脚0 */
#define GPIO_Pin_1                 ((uint16_t)0x0002)  /*!< 选择引脚1 */
#define GPIO_Pin_2                 ((uint16_t)0x0004)  /*!< 选择引脚2 */
#define GPIO_Pin_3                 ((uint16_t)0x0008)  /*!< 选择引脚3 */
#define GPIO_Pin_4                 ((uint16_t)0x0010)  /*!< 选择引脚4 */
#define GPIO_Pin_5                 ((uint16_t)0x0020)  /*!< 选择引脚5 */
#define GPIO_Pin_6                 ((uint16_t)0x0040)  /*!< 选择引脚6 */
#define GPIO_Pin_7                 ((uint16_t)0x0080)  /*!< 选择引脚7 */
#define GPIO_Pin_8                 ((uint16_t)0x0100)  /*!< 选择引脚8 */
#define GPIO_Pin_9                 ((uint16_t)0x0200)  /*!< 选择引脚9 */
#define GPIO_Pin_10                ((uint16_t)0x0400)  /*!< 选择引脚10 */
#define GPIO_Pin_11                ((uint16_t)0x0800)  /*!< 选择引脚11 */
#define GPIO_Pin_12                ((uint16_t)0x1000)  /*!< 选择引脚12 */
#define GPIO_Pin_13                ((uint16_t)0x2000)  /*!< 选择引脚13 */
#define GPIO_Pin_14                ((uint16_t)0x4000)  /*!< 选择引脚14 */
#define GPIO_Pin_15                ((uint16_t)0x8000)  /*!< 选择引脚15 */
#define GPIO_Pin_All               ((uint16_t)0xFFFF)  /*!< 选择所有引脚 */

使用示例

基本GPIO配置示例

#include "stm32f10x_gpio.h"

void GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    // 使能GPIOA时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    // 配置PA5为推挽输出,50MHz
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // 配置PA0为浮空输入
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void GPIO_Operation(void)
{
    // 设置PA5为高电平
    GPIO_SetBits(GPIOA, GPIO_Pin_5);
    
    // 延时
    for(volatile int i = 0; i < 1000000; i++);
    
    // 设置PA5为低电平
    GPIO_ResetBits(GPIOA, GPIO_Pin_5);
    
    // 读取PA0的状态
    uint8_t pinState = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
}

外部中断配置示例

#include "stm32f10x_gpio.h"
#include "stm32f10x_exti.h"

void EXTI_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
    
    // 使能时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
    
    // 配置PA0为浮空输入
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    
    // 配置PA0为外部中断线
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
    
    // 配置外部中断
    EXTI_InitStructure.EXTI_Line = EXTI_Line0;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
}

注意事项

  1. 时钟使能:在使用GPIO之前,必须先使能对应的GPIO时钟
  2. 引脚锁定:使用GPIO_PinLockConfig可以锁定引脚配置,防止意外修改
  3. 重映射功能:某些引脚支持功能重映射,可以改变引脚的功能
  4. 速度设置:输出模式下需要根据实际应用选择合适的输出速度
  5. 输入模式:根据外部电路选择合适的输入模式(浮空、上拉、下拉)

总结

STM32F10x GPIO标准库提供了完整的GPIO控制接口,包括引脚配置、数据读写、中断配置等功能。通过合理使用这些函数,可以实现各种GPIO应用,如LED控制、按键检测、外部中断等。在使用过程中需要注意时钟使能、引脚配置等关键步骤。