《STM32入门》-通用输入输出

介绍

GPIO(General-purpose input/output)通用输入输出口。分为GPIOA和GPIOB,并连接于APB2总线

GPIO基本结构

GPIO相关库函数

GPIO所在文件——gpio.h/gpio.c

//
void GPIO_DeInit(GPIO_TypeDef* GPIOx);
//
void GPIO_AFIODeInit(void);
//GPIO引脚初始化函数
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
//开启所有引脚,并将引脚结构体进行初始化->速度设置为2Mhz,引脚模式设置为浮空输入
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
//读取指定输入引脚的状态,并返回该引脚状态(1/0)
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);
//set指定引脚
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);

常用库函数

void GPIO_Init(GPIO_TypeDef* GPIOx, 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_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);

GPIO八种输入输出模式

//标准库中的定义
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开启

基本逻辑

开启外设时钟-初始化引脚-开启引脚

代码

	//举例:开启PA0引脚
        //开启CPIOA的外设时钟,注意:GPIOA在APB2总线。
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//引脚结构体:
        GPIO_InitTypeDef gpio_structure;
        
	gpio_structure.GPIO_Mode = GPIO_Mode_AF_PP;//选择输入输出模式
	gpio_structure.GPIO_Pin = GPIO_Pin_0;//选择需要开启的引脚编号
	gpio_structure.GPIO_Speed = GPIO_Speed_50MHz;//选择输出速度,注意:输入状态不会受到速度影响

        //引脚初始化
	GPIO_Init(GPIOA,&gpio_structure);
版权声明:本站文章均为 原创内容 ,且遵循 CC BY-NC-ND 4.0 许可协议 ,转载请标明本文作者: 红笺画文 ,并注明转载出处
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇