C语言对寄存器的封装
#define GPIOF_BASE (0x40021400) //设置GPIOF的基地址
#define GPIOF_MODER *( unsigned int *)(GPIOF_BASE + 0X00) //设置偏移地址
#define GPIOF_OTYPER *( unsigned int *)(GPIOF_BASE + 0X04) //设置偏移地址
#define GPIOF_OSPEEDR *( unsigned int *)(GPIOF_BASE + 0X08) //设置偏移地址
#define GPIOF_PUPDR *( unsigned int *)(GPIOF_BASE + 0X0C) //设置偏移地址
#define GPIOF_IDR *( unsigned int *)(GPIOF_BASE + 0X10) //设置偏移地址
#define GPIOF_ODR *( unsigned int *)(GPIOF_BASE + 0X14) //设置偏移地址
#define GPIOF_BSRR *( unsigned int *)(GPIOF_BASE + 0X18) //设置偏移地址
#define GPIOF_LCKR *( unsigned int *)(GPIOF_BASE + 0X1C) //设置偏移地址
#define GPIOF_AFRL *( unsigned int *)(GPIOF_BASE + 0X20) //设置偏移地址
#define GPIOF_AFRH *( unsigned int *)(GPIOF_BASE + 0X24) //设置偏移地址
如果一个一个封装,仅从GPIOA-GPIOF的封装代码量太多,这里采用结构体的形式进行封装,下面这一段代码实现了对所有GPIO寄存器的封装
#ifndef __STM32F4XX_H
#define __STM32F4XX_H
#define RCC_BASE (( unsigned int )0x40023800)
#define RCC_AHB1ENR (*( unsigned int *)(RCC_BASE+0x30))
typedef unsigned int uint32_t;
typedef unsigned short int uint16_t;
typedef struct
{
uint32_t MODER;
uint32_t OTYPER;
uint32_t OSPEEDR;
uint32_t PUPDR;
uint32_t IDR;
uint32_t ODR;
uint16_t BSRRL;
uint16_t BSRRH;
uint32_t LCKR;
uint32_t AFRL;
uint32_t AFRH;
}GPIO_TypeDef;
#define GPIOA_BASE (( uint32_t )0x40020000)
#define GPIOB_BASE (( uint32_t )0x40020400)
#define GPIOC_BASE (( uint32_t )0x40020800)
#define GPIOD_BASE (( uint32_t )0x40020C00)
#define GPIOE_BASE (( uint32_t )0x40021000)
#define GPIOF_BASE (( uint32_t )0x40021400)
#define GPIOG_BASE (( uint32_t )0x40021800)
#define GPIOH_BASE (( uint32_t )0x40021C00)
#define GPIOI_BASE (( uint32_t )0x40022000)
#define GPIOA ((GPIO_TypeDef *)GPIOA_BASE)
#define GPIOB ((GPIO_TypeDef *)GPIOB_BASE)
#define GPIOC ((GPIO_TypeDef *)GPIOC_BASE)
#define GPIOD ((GPIO_TypeDef *)GPIOD_BASE)
#define GPIOE ((GPIO_TypeDef *)GPIOE_BASE)
#define GPIOF ((GPIO_TypeDef *)GPIOF_BASE)
#define GPIOG ((GPIO_TypeDef *)GPIOG_BASE)
#define GPIOH ((GPIO_TypeDef *)GPIOH_BASE)
#define GPIOI ((GPIO_TypeDef *)GPIOI_BASE)
#endif /* __STM32F4XX_H */