宏定义寄存器地址
总线基地址
AHB总线的基地址从0x4002 0000开始,原来的基地址0x4001 8000不方便计算偏移地址
外设基地址
寄存器地址列表
在stm32f10x.h中宏定义寄存器地址
// 用来存放STM32寄存器映射的代码
// 外设 perirhral
#define PERIPH_BASE ((unsigned int)0x40000000)
#define APB1PERIPH_BASE PERIPH_BASE
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
#define RCC_BASE (AHBPERIPH_BASE + 0x1000)
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
#define RCC_APB2ENR *(unsigned int*)(RCC_BASE + 0x18)
#define GPIOB_CRL *(unsigned int*)(GPIOB_BASE + 0x00)
#define GPIOB_CRH *(unsigned int*)(GPIOB_BASE + 0x04)
#define GPIOB_ODR *(unsigned int*)(GPIOB_BASE + 0x0C)
编写程序
//main.c
#include"stm32f10x.h"
int main(void)
{
#if 0
//打开GPIOB端口时钟
*(unsigned int *)0x40021018 |= (1<<3);
//配置IO口为输出
*(unsigned int *)0x40010C00 |= ((1)<<(4*0));
*(unsigned int *)0x40010C00 |= ((1)<<(4*1));
*(unsigned int *)0x40010C00 |= ((1)<<(4*5));
//配置ODR寄存器
*(unsigned int *)0x40010C0C &= ~(1<<0);
*(unsigned int *)0x40010C0C &= ~(1<<1);
*(unsigned int *)0x40010C0C &= ~(1<<5);
#else
RCC_APB2ENR |= (1<<3);
GPIOB_CRL &= ~((0x0f)<<0);//端口配置寄存器清零
GPIOB_CRL |= (1<<(4*0));
GPIOB_ODR &= ~(1<<0);
#endif
}
void SystemInit(void)
{
//函数体为空,目的为骗过编译器不报错
}
小知识
红色下线波浪线的解决方法
在代码编写区空白处 鼠标右键 outlinling->hida all outlinling