这里以STM32F407为例。
✨如果使用HAL库,可以在STM32CubeMX软件内,在时钟树页面,输入对应的外部时钟参数,然后设置最终的时钟主频后,回车,即可自动完成中间分频和倍频系数的配置。
- 标准库需要自己根据外部晶振时钟频率,自己调整分频和倍频参数,具体可以参考STM32CubeMX时钟树配置参数,填写到使用标准库对应的代码中。这样可以避免自己计算出错,导致时钟配置错误问题。
PLL_M
、PLL_N
、PLL_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 */
PLL_N
:336即可。链接:https://pan.baidu.com/s/1x85NYtxuaiTUAxwtr169Vg
提取码:4mhd