stm32 固件库操作GPIO接口

一.操作stm32的GPIO总共需要三个步骤:

1.使用RCC开启GPIO时钟。

2.使用GPIO_init函数初始化GPIO。

3.使用GPIO输出或输入函数控制GPIO口。

二.RCC外设库函数

首先,找到rcc.h文件,然后拉到最下面,一般.h文件最下面都是对函数的定义。

常用函数就这三个:

RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);

@brief                 启用或禁用高速APB(APB2)外设的时钟。
@param              RCC_APB2Periph:指定APB2外设来把关其时钟。这个参数可以是下列数值的                        任何组合:
@arg     

  *          RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
  *          RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
  *          RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
  *          RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
  *          RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
  *          RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,
  *          RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11     
@param          NewState: new state of the specified peripheral clock,can be:                         ENABLE or DISABLE.

 

RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);

@brief                  Enables or disables the Low Speed APB (APB1) peripheral clock.
@param               RCC_APB1Periph: specifies the APB1 peripheral to gates its clock.This                          parameter can be any combination of the following values:
@arg 
//注意这个参数全都是APB1,应该改成APB2

  *          RCC_APB1Periph_TIM2, RCC_APB1Periph_TIM3, RCC_APB1Periph_TIM4,
  *          RCC_APB1Periph_TIM5, RCC_APB1Periph_TIM6, RCC_APB1Periph_TIM7,
  *          RCC_APB1Periph_WWDG, RCC_APB1Periph_SPI2, RCC_APB1Periph_SPI3,
  *          RCC_APB1Periph_USART2, RCC_APB1Periph_USART3,RCC_APB1Periph_USART4, 
  *          RCC_APB1Periph_USART5, RCC_APB1Periph_I2C1, RCC_APB1Periph_I2C2,
  *          RCC_APB1Periph_USB, RCC_APB1Periph_CAN1, RCC_APB1Periph_BKP,
  *          RCC_APB1Periph_PWR, RCC_APB1Periph_DAC, RCC_APB1Periph_CEC,
  *          RCC_APB1Periph_TIM12, RCC_APB1Periph_TIM13, RCC_APB1Periph_TIM14
@param  NewState: new state of the specified peripheral clock,can be: ENABLE or DISABLE.

本函数第一个参数没有对GPIO口的定义,但也可以定义GPIO口。
 

RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);

@brief             Forces or releases AHB peripheral reset.
@param          RCC_AHBPeriph: specifies the AHB peripheral to reset.This parameter can be                           any combination of the following values:
  *     arg RCC_AHBPeriph_OTG_FS 
  *     arg RCC_AHBPeriph_ETH_MAC

@param  NewState: new state of the specified peripheral reset,can be: ENABLE or DISABLE.

三.GPIO外设库函数

同样点开gpio.h文件,拖到最后。常用函数也就是以下几个:

void GPIO_DeInit(GPIO_TypeDef* GPIOx);  参数是GPIOx接口,使用此函数,所指定的GPIO外设就会被复位。
void GPIO_AFIODeInit(void);此函数用来复位AFIO,调用后AFIO外设的配置就会全部清除
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);注意第二个函数是初始化结构体的地址,需要加取地址符&
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);

以下四个函数为常用GPIO输入函数:

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);

以下四个函数是常用GPIO输出函数:

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);  第一个参数是GPIOx,第二个是GPIO_Pin.此函数可以把指定的GPIO端口设置为高电平。
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);  这个函数可以直接对16个端口进行写入设置。

void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); //这个函数是用来固定引脚配置,输入参数后那个引脚配置就会被锁定,不会被意外更改
void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);//配置AFIO事件输出功能
void GPIO_EventOutputCmd(FunctionalState NewState);//配置AFIO事件输出功能
void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);//用来引脚重映射,第一个参数是选择重映射的方式,第二个参数是新的状态
void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);//配置AFIO数据选择器,以选择需要的中断引脚
void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface);

根据函数需要,可能要在函数定义之前先定义结构体,eg:

 第一行,GIPIO_InitTypeDef is a definition of the structure,in this case it is the GPIO type.As for the other 3 lines,input "." after structure's name, that u can select the structure members to insert.Press F12 on"GPIO_Mode/GPIO_Pin/GPIO_Speed"(etc) so that skip to the instructions,specifies the values,which is at the Notes part.Press Control and F meanwhile,skip.

选择好合适的输入输出模式,什么上拉、推挽等等,然后把名称作为value写入前面structure的mode的括号参数里就好。例如:GIPIO_InitTypeDef.GPIO_Mode = GPIO_Mode_Out_PP;  意为该设定GPIO为推完输出。

然后其他几个structure members 定义的方法也是这样的。至此,GPIO初始化就完成了。

接着,参考GPIO常用的4个输入函数和4个输出函数,即可进行输入输出的定义。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值