STM32F4基于标准库外部时钟配置方法

STM32F4基于标准库外部时钟配置方法


  • 这里以STM32F407为例。

  • ✨如果使用HAL库,可以在STM32CubeMX软件内,在时钟树页面,输入对应的外部时钟参数,然后设置最终的时钟主频后,回车,即可自动完成中间分频和倍频系数的配置。

  • 外部16MH时钟频率:
    STM32F4基于标准库外部时钟配置方法_第1张图片

  • 外部8MHz时钟晶振频率配置
    STM32F4基于标准库外部时钟配置方法_第2张图片

  • 外部25MHz时钟晶振频率配置
    STM32F4基于标准库外部时钟配置方法_第3张图片

  • 标准库需要自己根据外部晶振时钟频率,自己调整分频和倍频参数,具体可以参考STM32CubeMX时钟树配置参数,填写到使用标准库对应的代码中。这样可以避免自己计算出错,导致时钟配置错误问题。
  • 主要调整的三个宏参数分别是:PLL_MPLL_NPLL_P
//system_stm32f4xx.c文件
/************************* PLL Parameters *************************************/
#if defined (STM32F40_41xxx) || defined (STM32F427_437xx) || defined (STM32F429_439xx) || defined (STM32F401xx)
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M      8
#else /* STM32F411xE */
#if defined (USE_HSE_BYPASS)
#define PLL_M      8    
#else /* STM32F411xE */   
#define PLL_M      16
#endif /* USE_HSE_BYPASS */
#endif /* STM32F40_41xxx || STM32F427_437xx || STM32F429_439xx || STM32F401xx */  

/* USB OTG FS, SDIO and RNG Clock =  PLL_VCO / PLLQ */
#define PLL_Q      7

#if defined (STM32F40_41xxx)
#define PLL_N    168  		
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P      2
#endif /* STM32F40_41xxx */

#if defined (STM32F427_437xx) || defined (STM32F429_439xx)
#define PLL_N      360
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P      2
#endif /* STM32F427_437x || STM32F429_439xx */

#if defined (STM32F401xx)
#define PLL_N      336
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P      4
#endif /* STM32F401xx */

#if defined (STM32F411xE)
#define PLL_N      400
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P      4   
#endif /* STM32F411xx */
  • ‍说明一点的是,标准库函数中,默认定义的HSE_VALUE参数是8000 000(8MHz),可以在stm32f4xx.h中,自己宏定义HSE_VALUE,并指定自己的外部时钟晶振的频率。
#ifndef __STM32F4xx_H
#define __STM32F4xx_H

#ifdef __cplusplus
 extern "C" {
#endif /* __cplusplus */

#define HSE_VALUE   16000000ul		//自定义外部时钟频率

..............

/**
 * @brief In the following line adjust the value of External High Speed oscillator (HSE)
   used in your application 
   
   Tip: To avoid modifying this file each time you need to use different HSE, you
        can define the HSE value in your toolchain compiler preprocessor.
  */           

#if !defined  (HSE_VALUE) 
  #define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
  
#endif /* HSE_VALUE */

配置的(16MHz)标准库工程

  • 基于正点原子STM32f4工程模版配置的16MHz的工程。当然也可以改回8MHz时钟晶振,只需调整system_stm32f4xx.c文件中的PLL_N:336即可。
链接:https://pan.baidu.com/s/1x85NYtxuaiTUAxwtr169Vg 
提取码:4mhd

你可能感兴趣的:(stm32标准库开发例程,stm32,嵌入式硬件,单片机)