请选择 进入手机版 | 继续访问电脑版

你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32库函数配置

[复制链接]
STMCU小助手 发布时间:2022-1-16 19:32
stm32 固件库V3.0以上的版本,main等源文件中不再直接包含stm32f10x_conf.h,而是stm32f10x.h,stm32f10x.h则定义了启动设置,以及所有寄存器宏定义,此文件中需要注意的有:
使用V3.0以上版本固件库的方法如下:
1.选择device(配置函数STM32F10x.h,具体配置方法如下)
在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)
  /* #define STM32F10X_LD */     /*!< STM32F10X_LD: STM32 Low density devices */
  /* #define STM32F10X_LD_VL */  /*!< STM32F10X_LD_VL: STM32 Low density Value Line devices */  
  /* #define STM32F10X_MD */     /*!< STM32F10X_MD: STM32 Medium density devices */
  /* #define STM32F10X_MD_VL */  /*!< STM32F10X_MD_VL: STM32 Medium density Value Line devices */  
  /* #define STM32F10X_HD */     /*!< STM32F10X_HD: STM32 High density devices */
  /* #define STM32F10X_HD_VL */  /*!< STM32F10X_HD_VL: STM32 High density value line devices */  
  /* #define STM32F10X_XL */     /*!< STM32F10X_XL: STM32 XL-density devices */
  /* #define STM32F10X_CL */     /*!< STM32F10X_CL: STM32 Connectivity line devices */
#endif
该代码的是让用户根据自己所使用的芯片的存储器(flash)大小,选择相应的flash编程算法,如果用户使用的是大容量存储芯片(如STM32F103VCT6),则只需要将对应大大容量存储器前面的屏蔽符去掉即可,去掉后为:
#define STM32F10X_HD      /*!< STM32F10X_HD: STM32 High density devices */
其它部分代码不变。
如果使用的是中等容量的存储器芯片(如stm32f103c8t6),同样是将对应代码前面的屏蔽符去掉即可,如:
#define STM32F10X_MD      /*!< STM32F10X_MD: STM32 Medium density devices */
2 M& M" F% k2 m! V+ _! m+ I+ z
2. 时钟频率配置(配置函数:system_stm32f10x.c,具体配置方法如下
#if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL)
/* #define SYSCLK_FREQ_HSE    HSE_VALUE */
#define SYSCLK_FREQ_24MHz  24000000
#else
/* #define SYSCLK_FREQ_HSE    HSE_VALUE */
/* #define SYSCLK_FREQ_24MHz  24000000 */
/* #define SYSCLK_FREQ_36MHz  36000000 */
/* #define SYSCLK_FREQ_48MHz  48000000 */
/* #define SYSCLK_FREQ_56MHz  56000000 */
#define SYSCLK_FREQ_72MHz  72000000
#endif
由上述代码可见默认是使用72MHz的时钟频率,如果需要使用其它频率,只需做相应的修改即可,如果使用的是72MHz时钟频率可以不用配置此项。
3. 选择外部时钟(配置函数stm32f10x.h,具体配置方法如下:)
#if !defined  HSE_VALUE
#ifdef STM32F10X_CL   
  #define HSE_VALUE    ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */
#else
  #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
#endif /* STM32F10X_CL */
#endif /* HSE_VALUE */
注意STM32F10X_CL是stm32f105 和stm32f107 互联型的device,用到此器件外部要选用25MHz的晶体,由于前面的代买没有取消  /* #define STM32F10X_CL */     /*!< STM32F10X_CL: STM32 Connectivity line devices */的注释,所以此处默认的外部8MHz的晶体。
4. 开启外设总开关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
默认情况下STM32的标准是关闭的,如果不适用片内外设,则不要取消  /*#define USE_STDPERIPH_DRIVER*/的注释
,如果需要使用STM32的标准外设则需要开启固件外设,即去掉前面的屏蔽符,修改后如下:
#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
' u) @. T  q* I/ I3 _# Z) z( g
5. 最后一步是开启需要使用的外设(配置函数:stm32f10x_config.h,具体配置步骤如下:)
/* Includes ------------------------------------------------------------------*/
/* Uncomment the line below to enable 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) */
默认是关闭所有外设的,用户需要使用哪个外设,就将该外设前面的注释去掉即可。
3 G% Y: _( D. h, |5 ]1 m: U1 r
到这里库函数的配置就全部介绍完毕了,建一个工程需要修改这么多配置是不是太麻烦了呢,的确是比较麻烦,最后再给大家介绍一种简单的配置方法,用宏定义配置这些参数,keil MDk 支持在编译器中添加宏定义,这里就以keil MDK为例,给大家介绍。
先在keil MDK中点击tagart option选项,弹出如下图所示窗口:
+ R- p  ]7 ?- r1 B+ `
然后点击C/C++选项,弹出如下窗口:
3 m( z, B( ]1 T3 Q
最后在Difine的方框中添加上需要声明的宏定义即可。

1 [7 n9 f! h8 @
! X2 x5 X7 z3 @8 r9 g
这里总结一下一般工程中需要添加的宏定义:
1. STM32F10X_HD   //选择用户所使用芯片的存储器容量(这里选择的是大容量存储)
2. USE_STDPERIPH_DRIVER //打开标准外设总开关
3. SYSCLK_FREQ_72MHz     //选择时钟频率(默认也是该选项)
4. HSE_VALUE      //选择使用外部高速时钟(默认也是该选项)

, l4 L* j0 R) ^* o6 ?  T" N+ F
注意:stm32f10x.h文件的最后有这样的代码:
#ifdef USE_STDPERIPH_DRIVER
  #include "stm32f10x_conf.h"
#endif
stm32f10x_conf.h中包含了所有外设的头文件,因此任意源文件只要包含了stm32f10x.h,就可以在源文件调用任意外设的函数。
若有外设为使用到,在stm32f10x_conf.h注释相应部分,项目编译时就不会在编译去掉的外设。
  K1 _8 v% ]2 W8 q
9 O  V. j+ g3 e& c0 Y. G! v
收藏 评论0 发布时间:2022-1-16 19:32

举报

0个回答
关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版