#error “Please select first the target STM32F4xx device used in your application (in stm32f4xx.h

本文介绍了如何解决在使用STM32F4xx微控制器时遇到的错误,问题出在没有正确选择应用中的目标设备。解决方案是根据STM32的存储器映射在stm32f4xx.h文件中定义寄存器地址,通过修改该头文件内容来修复Bug。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值