stm32的gpio函数介绍

时间:2023-03-09 20:53:20
stm32的gpio函数介绍

一、gpio_init函数 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

调用时的格式一般是例如

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);//使能时钟

GPIO_InitTypeDef  GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

GPIO_Init(GPIOD,&GPIO_InitStructure);

参数1是GPIO_TypeDef类型,如下,就是7个寄存器的地址。

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;

下面是宏定义

#define GPIOD               ((GPIO_TypeDef *) GPIOD_BASE)

#define GPIOD_BASE            (APB2PERIPH_BASE + 0x1400)

#define APB2PERIPH_BASE       (PERIPH_BASE + 0x10000)

#define PERIPH_BASE           ((uint32_t)0x40000000)

可以看出0x40000000是外设的首地址,在STM32芯片的内部STM32有两个,一个叫APB1,一个叫APB2

参数2为GPIO_InitTypeDef* GPIO_InitStruct。就是一个指向GPIO _InitTypeDef的地址。

typedef struct

{

uint16_t GPIO_Pin;

GPIOSpeed_TypeDef  GPIO_Speed;

GPIOMode_TypeDef  GPIO_Mode;

}GPIO_InitTypeDef;