一、Bug背景
在使用寄存器方式点亮LED时遇到的Bug
解决方案:
根据STM32的存储器映射先定义好各个寄存器的地址,并把这些地址定义都写在stm32f4xx.h中。
stm32f4xx.h文件里的全部内容如下:
/*片上外设基地址 */
#define PERIPH_BASE ((unsigned int)0x40000000)
/*总线基地址 */
#define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000)
/*GPIO外设基地址*/
#define GPIOA_BASE (AHB1PERIPH_BASE + 0x0000)
/* GPIOA寄存器地址,强制转换成指针 */
#define GPIOA_MODER *(unsigned int*)(GPIOA_BASE+0x00)
#define GPIOA_OTYPER *(unsigned int*)(GPIOA_BASE+0x04)
#define GPIOA_OSPEEDR *(unsigned int*)(GPIOA_BASE+0x08)
#define GPIOA_PUPDR *(unsigned int*)(GPIOA_BASE+0x0C)
#define GPIOA_IDR *(unsigned int*)(GPIOA_BASE+0x10)
#define GPIOA_ODR *(unsigned int*)(GPIOA_BASE+0x14)
#define GPIOA_BSRR *(unsigned int*)(GPIOA_BASE+0x18)
#define GPIOA_LCKR *(unsigned int*)(GPIOA_BASE+0x1C)
#define GPIOA_AFRL *(unsigned int*)(GPIOA_BASE+0x20)
#define GPIOA_AFRH *(unsigned int*)(GPIOA_BASE+0x24)
/*RCC外设基地址*/
#define RCC_BASE (AHB1PERIPH_BASE + 0x3800)
/*RCC的AHB1时钟使能寄存器地址,强制转换成指针*/
#define RCC_AHB1ENR *(unsigned int*)(RCC_BASE+0x30)
以上代码是stm32f4xx.h文件的内容
定义好各个寄存器地址后,再次编译程序,Bug就解决了。
出现Bug时stm32f4xx.h文件的内容如下: (修改前)
/**
******************************************************************************
* @file stm32f4xx.h
* @author MCD Application Team
* @brief CMSIS STM32F4xx Device Peripheral Access Layer Header File.
*
* The file is the unique include file that the application programmer
* is using in the C source code, usually in main.c. This file contains:
* - Configuration section that allows to select:
* - The STM32F4xx device used in the target application
* - To use or not the peripheral抯 drivers in application code(i.e.
* code will be based on direct access to peripher