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);
这些是CH32V003 GPIO库的函数,分类解释:
1. GPIO初始化和配置函数
| 函数 | 说明 |
|---|
GPIO_DeInit() | 复位GPIO外设,恢复默认值 |
GPIO_AFIODeInit() | 复位AFIO(复用功能IO) |
GPIO_Init() | 最重要的 - 初始化GPIO引脚模式、速度等 |
GPIO_StructInit() | 初始化GPIO配置结构体为默认值 |
GPIO_PinLockConfig() | 锁定GPIO配置,防止意外修改 |
GPIO_PinRemapConfig() | 配置引脚复用/重映射功能 |
2. GPIO读写操作函数
输入操作:
| 函数 | 说明 |
|---|
GPIO_ReadInputDataBit() | 读取单个输入引脚的电平(按键检测用这个) |
GPIO_ReadInputData() | 读取整个端口的输入数据(16位) |
输出操作:
| 函数 | 说明 |
|---|
GPIO_ReadOutputDataBit() | 读取单个输出引脚的设置值(不是实际电平) |
GPIO_ReadOutputData() | 读取整个端口的输出设置值 |
GPIO_SetBits() | 置位引脚(设为高电平/1) |
GPIO_ResetBits() | 复位引脚(设为低电平/0) |
GPIO_WriteBit() | 写入单个引脚的值(Bit_RESET/Bit_SET) |
GPIO_Write() | 写入整个端口的值 |
3. 特殊功能函数
| 函数 | 说明 |
|---|
GPIO_EventOutputConfig() | 配置事件输出功能(唤醒MCU等) |
GPIO_EventOutputCmd() | 启用/禁用事件输出 |
GPIO_EXTILineConfig() | 配置外部中断引脚(重要) |
常用函数使用示例:
1. GPIO初始化示例:
GPIO_InitTypeDef GPIO_InitStructure;
// LED引脚配置为输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 按键引脚配置为输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
2. 读取按键状态:
// 读取PA1引脚输入
uint8_t key_state = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1);
3. 控制LED:
// 方法1:分别控制
GPIO_SetBits(GPIOA, GPIO_Pin_0); // LED亮(高电平点亮)
GPIO_ResetBits(GPIOA, GPIO_Pin_0); // LED灭
// 方法2:使用WriteBit
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET); // 亮
GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET); // 灭
// 方法3:翻转(如果低电平点亮)
if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_0) == 0) {
GPIO_SetBits(GPIOA, GPIO_Pin_0); // 关->开
} else {
GPIO_ResetBits(GPIOA, GPIO_Pin_0); // 开->关
}
4. 配置外部中断:
// 配置PA1为外部中断源
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
GPIO模式说明:
| 模式 | 说明 |
|---|
GPIO_Mode_AIN | 模拟输入 |
GPIO_Mode_IN_FLOATING | 浮空输入(默认) |
GPIO_Mode_IPD | 下拉输入 |
GPIO_Mode_IPU | 上拉输入(常用,按键) |
GPIO_Mode_Out_OD | 开漏输出 |
GPIO_Mode_Out_PP | 推挽输出(常用,LED) |
GPIO_Mode_AF_OD | 复用开漏 |
GPIO_Mode_AF_PP | 复用推挽 |
在你的项目中:
- 按键:用
GPIO_ReadInputDataBit() 读取状态 - LED:用
GPIO_SetBits()/GPIO_ResetBits() 控制 - 初始化:用
GPIO_Init() 配置引脚模式
评论
暂无评论