STM32F10X库函数逻辑

本文详细介绍了STM32中如何通过定义基地址和偏移量来访问APB1、APB2和APB外围总线上的外设,如RCC和GPIOB。通过对RCC_APB2ENR寄存器的配置启用GPIOB,展示了结构体指针的使用,为外设的初始化和功能实现提供基础。
摘要由CSDN通过智能技术生成
#define PERIPH_BASE    ((unsigned int)0x40000000)  //定义外围总线基地址
	
#define APB1PERIPH_BASE  (PERIPH_BASE)             //APB1总线开始与外围总线基地址

#define APB2PERIPH_BASE  (PERIPH_BASE +0x10000)	    //APB2外围总线偏移0x10000

#define APBPERIPH_BASE  (PERIPH_BASE +0x20000)	    //APB外围总线偏移0x20000
	


#define RCC_BASE    (APBPERIPH_BASE+0x1000)         //RCC基地址为APB总线地址偏移0x1000

#define GPIOB_BASE   (APB2PERIPH_BASE+0x0C00)              //GPIOB_基地址为APB2总线地址偏移



#define RCC_APB2ENR    *(unsigned int*)(RCC_BASE+0x18)      //强制类型转换+解引用
	
//#define GPIOB_CRL    *(unsigned int*)(GPIOB_BASE)     //对应寄存器的配置
//#define GPIOB_CRH    *(unsigned int*)(GPIOB_BASE+0x04) 
//#define GPIOB_ODR    *(unsigned int*)(GPIOB_BASE+0x0C) 
   

typedef struct                                           //定义结构体类型
{
    unsigned int CRL;
	unsigned int CRH;
	unsigned int IDR;
	unsigned int ODR;
	unsigned int BSRR;
	unsigned int BRR;
	unsigned int LCKR;
}GPIO_TypeDef;

typedef struct                                            //定义结构体类型
{
    unsigned int CR;
	unsigned int CFGR;
	unsigned int CIR;
	unsigned int APB2RSTR;
	unsigned int APB1RSTR;
	unsigned int AHNEMR;
	unsigned int APB2ENR;
	unsigned int APB1ENR;
	unsigned int BDCR;
	unsigned int CSR;
}RCC_TypeDef;


#define GPIOB ((GPIO_TypeDef*)GPIOB_BASE)                //GPIOB基地址强制类型转换为GPOIO_TYPEDEF类型的结构体指针,指向GPIOB区地址
#define RCC  ((RCC_TypeDef*)RCC_BASE)                    //RCC基地址强制类型转换

基地址>>寄存器>>结构体指针>>外设封装函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值