• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
24
博客园    首页    新随笔    联系   管理    订阅  订阅

操纵GPIO

开启外设时钟

使用复位和时钟控制RCC驱动程序
(stm32f10x_rcc.c)


有3个针对不同总线连接的外设时钟命令函数
• RCC_AHBPeriphClockCmd
• RCC_APB1PeriphClockCmd
• RCC_APB2PeriphClockCmd


GPIO通过APB2总线连接系统
开启GPIO外设时钟的函数
RCC_APB2PeriphClockCmd

 

如:开启GPIOA外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

 

GPIO的初始化结构类型 GPIO_InitTypeDef

typedef struct
 { uint16_t GPIO_Pin;
/* 指定配置的GPIO引脚 */
 GPIOSpeed_TypeDef GPIO_Speed;
 /* 指定GPIO引脚输出的最高频率 */
 GPIOMode_TypeDef GPIO_Mode;
 /* 指定GPIO引脚配置的工作模式 */
 }GPIO_InitTypeDef;

 

GPIO_InitTypeDef成员1:GPIO_Pin

要进行配置的GPIO引脚编号
其值是常量GPIO_Pin_y(y是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_15 ((uint16_t)0x8000)
 /*!< Pin 15 selected */
#define GPIO_Pin_All ((uint16_t)0xFFFF)
 /*!< All pins selected */

 

如:PA0—PA7均使用
GPIO_IntStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;

 

GPIO_InitTypeDef成员2:GPIO_Speed

定义在枚举类型GPIOSpeed_TypeDef 中
typedef enum
{ GPIO_Speed_10MHz = 1,
 GPIO_Speed_2MHz,
 // 不赋值的枚举变量自动加1,故此常量值为2
 GPIO_Speed_50MHz // 常量值为3
}GPIOSpeed_TypeDef;


选择最高输出频率
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 

 

GPIO_InitTypeDef成员3:GPIO_Mode

定义在枚举类型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;


选择工作模式为推挽输出
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;

 

根据设定参数初始化 GPIOA

 

 

控制外设工作

外设驱动库提供控制外设工作的有关函数
对GPIO主要是输入和输出数据


本例中只需要输出函数,例如
• 字输出GPIO_Write函数
• 位输出GPIO_WriteBit函数
• 置位GPIO_SetBits函数
• 复位GPIO_ResetBits函数

void GPIO_SetBits (GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)
void GPIO_ResetBits (GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)

 

 

 

编写用户文件

初始化结构体 GPIO_InitTypeDef 类型 -- led.c
初始化库函数 GPIO_Init() -- led.c
开启外设时钟 -- led.c
控制IO输出高、低电平 - led.c,led.h,main.c

 

Led.c

 

Led.h

 

main.c

 

posted @ 2023-04-05 19:31  wxk1213  阅读(123)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3