11-C语言对寄存器的封装

1:仅通过指针进行封装 :编译可能出错,这个地方只是表示思路

//在基地址基础上加上偏移地址
#define GPIOF_BASE (0x40021400)
#define GPIOF_BASE (0x40021400)
#define GPIOF_BASE (0x40021400)
#define GPIOF_BASE (0x40021400)
#define GPIOF_BASE (0x40021400)
#define GPIOF_BASE (0x40021400)

#define GPIOF_MODER     *(unsigned int *)(GPIOF_BASE+0x40021400)
#define GPIOF_OTYPER     *(unsigned int *)(GPIOF_BASE+0x40021400)
#define GPIOF_OSPEEDR     *(unsigned int *)(GPIOF_BASE+0x40021400)
#define GPIOF_PUPDR     *(unsigned int *)(GPIOF_BASE+0x40021400)
#define GPIOF_IDR     *(unsigned int *)(GPIOF_BASE+0x40021400)
#define GPIOF_ODR     *(unsigned int *)(GPIOF_BASE+0x40021400)
#define GPIOF_BSRR     *(unsigned int *)(GPIOF_BASE+0x40021400)
#define GPIOF_LCKR     *(unsigned int *)(GPIOF_BASE+0x40021400)
#define GPIOF_AFRL     *(unsigned int *)(GPIOF_BASE+0x40021400)
#define GPIOF_AFRH     *(unsigned int *)(GPIOF_BASE+0x40021400)

2:通过指针和结构体进行封装 

//a---f定义非常多
//每个寄存器都占用4个字节,32位,连续的
//定义结构体
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;
    uint32_t BSSR;
    uint32_t LCKR;
    uint16_t AFRL;
    uint16_t AFRH;
}GPIO_TypeDef;

#define GPIOA_BASE  (0x40020000)
#define GPIOB_BASE  (0x40020400)
#define GPIOC_BASE  (0x40020800)
#define GPIOD_BASE  (0x40020C00)
#define GPIOE_BASE  (0x40021000)
#define GPIOF_BASE  (0x40021400)

#define GPIOA    (GPIO_TyoeDef *)GPIOA_BASE
#define GPIOB    (GPIO_TyoeDef *)GPIOB_BASE
#define GPIOC    (GPIO_TyoeDef *)GPIOC_BASE
#define GPIOD    (GPIO_TyoeDef *)GPIOD_BASE
#define GPIOE    (GPIO_TyoeDef *)GPIOE_BASE
#define GPIOF    (GPIO_TyoeDef *)GPIOF_BASE



GPIOF->ODR=0xFFFF;//操作寄存器,

3.在51单片机里面的操作 

4.在stm32里面的操作 

之所以在最前面加*是因为,不加*为地址 ,地址不能直接赋值;加上为指针,指针可以直接赋值。 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值