STM32为什么要在C/C++配置里写USE_STDPERIPH_DRIVER详解

转自;http://openedv.com/forum.php?mod=viewthread&tid=229746&highlight=USE%5C_STDPERIPH%5C_DRIVER

程序的执行是从“main.c”文件开始的,其中必须包含有头文件“stm32f10x.h”。

      我们打开“stm32f10x.h”,按下“Ctrl+F”键,查找USE_STDPERIPH_DRIVER,在“Find What”栏中输入“USE_STDPERIPH_DRIVER”。

      如图所示。点击“Find Next”,出现“USE_STDPERIPH_DRIVER”对应的代码行,重复上边操作三次,第三次的时候我们能在第8296-8298行找到如图所示代码段。

      

    这段代码的意思是,只有用预编译指令预定义了“USE_STDPERIPH_DRIVER”,才会将"stm32f10x_conf.h"包含进“stm32f10x.h”中,从而被"main.c"用到。这就解释了,为什么我们没有在“main.c”中包含"stm32f10x_conf.h",而在编译之后却被包含进了"main.c"中。

      "stm32f10x_conf.h"文件相当于一个开关文件,如果要用到STM32固件库驱动标准外设,则外设驱动头文件是必不可少的。如“stm32f10x_gpio.h”,在"stm32f10x_conf.h"中我们通过代码#include "stm32f10x_gpio.h"来实现这个操作。

      在C/C++预定义中加入“USE_STDPERIPH_DRIVER”就是允许“使用标准外设驱动”了。

2、

     怎么能自动定义USE_STDPERIPH_DRIVER(不用在每个工程都定义USE_STDPERIPH_DRIVER)?

      stm32f10x.h中,有:

  • #if !defined  USE_STDPERIPH_DRIVER  
  • /**
  • * @brief Comment the line below if you will not use the peripherals drivers.
  •    In this case, these drivers will not be included and the application code will  
  •    be based on direct access to peripherals registers  
  •    */  
  •   /*#define USE_STDPERIPH_DRIVER*/  
  • #endif  只要把  /*#define USE_STDPERIPH_DRIVER*/  注释去掉就可以了!

猜你喜欢

转载自blog.csdn.net/Kk_01110001B/article/details/81240025