定义自己的GPIO引脚的结构体
原始封装定引脚比较复杂如下需要多次重复容易错移植性差
#include “stm32f4xx.h”
/*peripheral*/
#define PERIPH_BASE ((unsiged int )0x400000000)
#define AHB1PERIPH_BASE (PERIHP_BASE+0x00020000)
#define GPIOH_BASE (AH1PERIPH +0x00001c00)
#define GPIOA_BASE (AH1PERIPH +0x00000000)
#define GPIO_MODER *(unsigned int *)(GPIO_BSAE+0x00)
#define GPIO_OTYPER *(unsigned int *)(GPIO_BSAE+0x04)
#define GPIO_OSPEEDER *(unsigned int *)(GPIO_BSAE+0x08)
#define GPIO_PUPDR *(unsigned int *)(GPIO_BSAE+0x0c)
#define GPIO_IDR *(unsigned int *)(GPIO_BSAE+0x10)
#define GPIO_ODR *(unsigned int *)(GPIO_BSAE+0x14)
#define GPIO_BSRR *(unsigned int *)(GPIO_BSAE+0x18)
#define GPIO_LCKR *(unsigned int *)(GPIO_BSAE+0x1c)
#define GPIO_AFRL *(unsigned int *)(GPIO_BSAE+0x20)
#define GPIO_AFRH *(unsigned int *)(GPIO_BSAE+0x24)
采用结构体封装类型强转可简化代码提高可移植性和可读性
typedef unsigned int unit32_t
typedef unsigned short unit16_t
typedef struct
{
unit32_t MODER
unit32_t OTYPER
unit32_t OSPEEDER
unit32_t PUPDR
unit32_t IDR
unit32_t ODR
unit16_t BSRRL
unit16_t BSRRH
unit32_t LCKR
unit32_t AFR[2]
}GPIO_Typedef
#define GPIOH ((GPIO_Typedef *) GPIOH_BASE)
#define GPIOA ((GPIO_Typedef *) GPIOA_BASE)
#define RCC_BASE (AHB1PERIPH_BASE+0x00003800)
#define RCC_AHB1ENR *(unsigned int *)(RCC_BASE+0x00)
void GPIO_SetBits() #GPIO引脚置位
void GPIO_ResetBits() #GPIO引脚复位
void main()
{
}
void SystemInit(void)
{
}
void GPIO_SetBits(GPIO_Typedef *GPIOx, uint16_t GPIO_pin)
{
GPIOx->BSRRL|=(1<<GPIO_PIN);
}
void GPIO_ResetBit(GPIO_Typedef *GPIOx,unit16_t GPIO_pin)
{
GPIOx->BSRRH|=(1<<GPIO_pin);
}