宏定义寄存器地址

image.png

总线基地址
image.png
AHB总线的基地址从0x4002 0000开始,原来的基地址0x4001 8000不方便计算偏移地址
image.png
外设基地址
image.png
寄存器地址列表
image.png
image.png
在stm32f10x.h中宏定义寄存器地址

  1. // 用来存放STM32寄存器映射的代码
  2. // 外设 perirhral
  3. #define PERIPH_BASE ((unsigned int)0x40000000)
  4. #define APB1PERIPH_BASE PERIPH_BASE
  5. #define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
  6. #define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
  7. #define RCC_BASE (AHBPERIPH_BASE + 0x1000)
  8. #define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
  9. #define RCC_APB2ENR *(unsigned int*)(RCC_BASE + 0x18)
  10. #define GPIOB_CRL *(unsigned int*)(GPIOB_BASE + 0x00)
  11. #define GPIOB_CRH *(unsigned int*)(GPIOB_BASE + 0x04)
  12. #define GPIOB_ODR *(unsigned int*)(GPIOB_BASE + 0x0C)

编写程序

  1. //main.c
  2. #include"stm32f10x.h"
  3. int main(void)
  4. {
  5. #if 0
  6. //打开GPIOB端口时钟
  7. *(unsigned int *)0x40021018 |= (1<<3);
  8. //配置IO口为输出
  9. *(unsigned int *)0x40010C00 |= ((1)<<(4*0));
  10. *(unsigned int *)0x40010C00 |= ((1)<<(4*1));
  11. *(unsigned int *)0x40010C00 |= ((1)<<(4*5));
  12. //配置ODR寄存器
  13. *(unsigned int *)0x40010C0C &= ~(1<<0);
  14. *(unsigned int *)0x40010C0C &= ~(1<<1);
  15. *(unsigned int *)0x40010C0C &= ~(1<<5);
  16. #else
  17. RCC_APB2ENR |= (1<<3);
  18. GPIOB_CRL &= ~((0x0f)<<0);//端口配置寄存器清零
  19. GPIOB_CRL |= (1<<(4*0));
  20. GPIOB_ODR &= ~(1<<0);
  21. #endif
  22. }
  23. void SystemInit(void)
  24. {
  25. //函数体为空,目的为骗过编译器不报错
  26. }

小知识

红色下线波浪线的解决方法

在代码编写区空白处 鼠标右键 outlinling->hida all outlinling
10.寄存器映射代码讲解 - 图7