先简单的介绍列出来,后续再总结。
一、GPIO寄存器
每组IO口含下面7个寄存器,一共可以控制一组GPIO的16个IO口。
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;
二、相关函数
1、GPIO_SetBits 置高
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BSRR = GPIO_Pin;
}
2、GPIO_ResetBits 置低
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BRR = GPIO_Pin;
}
3、GPIO_WriteBit 写bit
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
{
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
assert_param(IS_GPIO_BIT_ACTION(BitVal));
if (BitVal != Bit_RESET)
{
GPIOx->BSRR = GPIO_Pin;
}
else
{
GPIOx->BRR = GPIO_Pin;
}
}
4、GPIO_ReadOutputDataBit 读输出数据bit
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
uint8_t bitstatus = 0x00;
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GET_GPIO_PIN(GPIO_Pin));
if ((GPIOx->ODR & GPIO_Pin) != (uint32_t)Bit_RESET)
{
bitstatus = (uint8_t)Bit_SET;
}
else
{
bitstatus = (uint8_t)Bit_RESET;
}
return bitstatus;
}
}
三、GPIO配置
GPIO_InitTypeDef gpio_init; //定义一个GPIO数据结构体
gpio_init.GPIO_Pin = BSP_GPIOC_GSM_STATUS_PINS; //配置引脚
gpio_init.GPIO_Speed = GPIO_Speed_10MHz; //配置I/O速度
gpio_init.GPIO_Mode = GPIO_Mode_IPU; //输入上拉
GPIO_Init(GPIOC, &gpio_init); //初始化 GPIOC
四、相关定义
1、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;
2、GPIO 16个I/O口
#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 /
3、GPIO端口号
STM32F105RC使用GPIO端口号为:GPIOA、GPIOB、GPIOC
#define GPIOA ((GPIO_TypeDef *) GPIOA_BASE)
#define GPIOB ((GPIO_TypeDef *) GPIOB_BASE)
#define GPIOC ((GPIO_TypeDef *) GPIOC_BASE)
#define GPIOD ((GPIO_TypeDef *) GPIOD_BASE)
#define GPIOE ((GPIO_TypeDef *) GPIOE_BASE)
#define GPIOF ((GPIO_TypeDef *) GPIOF_BASE)
#define GPIOG ((GPIO_TypeDef *) GPIOG_BASE)
4、GPIO_Speed
typedef enum
{
GPIO_Speed_10MHz = 1,
GPIO_Speed_2MHz,
GPIO_Speed_50MHz
}GPIOSpeed_TypeDef;
5、GPIO 8种工作模式
/**
* @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;