STM32F10X_MD宏定义、USE_STDPERIPH_DRIVER作用及VS Code编写stm32代码配置指南

STM32 keil设置中的宏定义STM32F10X_MD,USE_STDPERIPH_DRIVER作用以及用VS Code编写stm32代码配置问题

在创建STM32标准库工程模板时,要在keil的C/C++设置中添加下面的宏定义,注意中间是英文逗号隔开

宏定义STM32F10X_MD的作用:

在stm32f10x.h文件中有这样预处理代码

#if !defined (STM32F10X_LD) && !defined (STM32F10X_LD_VL) && !defined (STM32F10X_MD) && !defined (STM32F10X_MD_VL) && !defined (STM32F10X_HD) && !defined (STM32F10X_HD_VL) && !defined (STM32F10X_XL) && !defined (STM32F10X_CL)
 #error "Please select first the target STM32F10x device used in your application (in stm32f10x.h file)"
#endif

意思是说如果有定义STM32F10X_LD、STM32F10X_LD_VL、STM32F10X_MD……其中的一种,defined就是1,!defined就是0,#if判断就为假,就不会执行下面的 #error 语句,如果全部都没定义,那#if判断就为真,执行#error语句

#error命令是C/C++语言的预处理命令之一,当预处理器预处理到#error命令时将停止编译并输出用户自定义的错误消息。

其中这些宏定义也是要根据芯片的容量来选择定义的,STM32F10X_LD为小容量、STM32F10X_MD为中容量、STM32F10X_HD为大容量

这里的容量是指FLASH的大小,判断方法如下:

16KB≤FLASH≤32KB 选择:STM32F10X_LD

64KB≤FLASH≤128KB 选择:STM32F10X_MD

256KB≤FLASH≤512KB 选择:STM32F10X_HD

常用的STM32C8T6是64K,为中容量,STM32F103ZET6为512K,为大容量

所以要定义这个STM32F10X_MD这个宏,才能正常编译,并且stm32f10x.h头文件中有许多与这个宏定义相关的配置,所以这宏定义相当重要

宏定义USE_STDPERIPH_DRIVER的作用:

在stm32f10x.h头文件中有以下语句:

#ifdef USE_STDPERIPH_DRIVER
  #include "stm32f10x_conf.h"
#endif

这段代码的意思是,只有定义了USE_STDPERIPH_DRIVER,才包含stm32f10x_conf.h,从而被main.c用到。

打开stm32f10x_conf.h文件可看到下面代码

/* Includes ------------------------------------------------------------------*/
/* Uncomment/Comment the line below to enable/disable peripheral header file inclusion */
#include "stm32f10x_adc.h"
#include "stm32f10x_bkp.h"
#include "stm32f10x_can.h"
#include "stm32f10x_cec.h"
#include "stm32f10x_crc.h"
#include "stm32f10x_dac.h"
#include "stm32f10x_dbgmcu.h"
#include "stm32f10x_dma.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_flash.h"
#include "stm32f10x_fsmc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_i2c.h"
#include "stm32f10x_iwdg.h"
#include "stm32f10x_pwr.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_rtc.h"
#include "stm32f10x_sdio.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_wwdg.h"
#include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */

这表示stm32f10x_conf.h将所有的外设头文件都包含进来了,如果要用到STM32固件库驱动标准外设,则外设驱动头文件是必不可少的,如stm32f10x_gpio.h等。而这些文件的添加都是在stm32f10x_conf.h中进行。

所以宏定义了USE_STDPERIPH_DRIVER后,就会包含stm32f10x_conf.h头文件,而stm32f10x_conf.h头文件里又包含了固件库的外设头文件;在编写代码时,只需要引入stm32f10x.h头文件就行了

VS Code配置问题

因为keil编写代码不方便,有时候代码提示又没出现,所以就用VS Code来编写代码,然后用keil来编译

使用VS Code打开stm32工程,在使用到标准外设库文件的函数时,如GPIO_SetBits();写了之后发现跳转不过去,按住Ctrl+左键没反应,或者有些函数没出代码提示

这是因为没有在开头引入要调用函数的对应头文件,如stm32f10x_gpio.h

但在keil中又没有在开头引入这些头文件,为什么也可以跳转呢?

这就是上面提到的宏定义USE_STDPERIPH_DRIVER的问题了,因为在keil中设置了宏定义,所以没有手动引入头文件也能跳转

那在VS Code中怎么添加这个预处理宏定义呢?

打开左边文件栏里的c_cpp_properties.json文件

把在keil中定义的两个宏定义复制到defines内容中,添加在下面图中的地方,按照格式改到没报错

然后保存,重启一下VS Code,再开始编写代码,就发现函数可以跳转到对应的函数实现了

物联沃分享整理
物联沃-IOTWORD物联网 » STM32F10X_MD宏定义、USE_STDPERIPH_DRIVER作用及VS Code编写stm32代码配置指南

发表评论