一、时钟系统框图讲解
无论是小型单片机还是像STM32这样高级单片机,它们工作的核心都是大规模的时序逻辑电路,而驱动时序逻辑电路的关键则是准确而又稳定的时钟源。它的作用就像小学在操场上做广播体操时候播放的北京音乐,用于协调和同步各单元运行,为时序电路提供基本的脉冲信号。
微控制器的运行必须要依赖周期性的时钟脉冲来驱动。往往由一个外部晶体振荡器提供时钟输入开始,最终转换为多个外部设备的周期性运作为末,这种时钟“能量”扩散流动的路径,犹如大树的养分通过主干流向各个分支,因此常称之为“时钟树”。
STM32微控制器的时钟树是可以配置的,时钟输入源与最终到达外设处的时钟速率不再有固定的关系。
二、时钟配置相关函数介绍
typedef struct
{
__IO uint32_t CR;
__IO uint32_t PLLCFGR;
__IO uint32_t CFGR; /*!< RCC clock configuration register, Address offset: 0x08 */
__IO uint32_t CIR; /*!< RCC clock interrupt register, Address offset: 0x0C */
__IO uint32_t AHB1RSTR; /*!< RCC AHB1 peripheral reset register, Address offset: 0x10 */
__IO uint32_t AHB2RSTR; /*!< RCC AHB2 peripheral reset register, Address offset: 0x14 */
__IO uint32_t AHB3RSTR; /*!< RCC AHB3 peripheral reset register, Address offset: 0x18 */
uint32_t RESERVED0; /*!< Reserved, 0x1C */
__IO uint32_t APB1RSTR; /*!< RCC APB1 peripheral reset register, Address offset: 0x20 */
__IO uint32_t APB2RSTR; /*!< RCC APB2 peripheral reset register, Address offset: 0x24 */
uint32_t RESERVED1[2]; /*!< Reserved, 0x28-0x2C */
__IO uint32_t AHB1ENR; /*!< RCC AHB1 peripheral clock register, Address offset: 0x30 */
__IO uint32_t AHB2ENR; /*!< RCC AHB2 peripheral clock register, Address offset: 0x34 */
__IO uint32_t AHB3ENR; /*!< RCC AHB3 peripheral clock register, Address offset: 0x38 */
uint32_t RESERVED2; /*!< Reserved, 0x3C */
__IO uint32_t APB1ENR; /*!< RCC APB1 peripheral clock enable register, Address offset: 0x40 */
__IO uint32_t APB2ENR; /*!< RCC APB2 peripheral clock enable register, Address offset: 0x44 */
uint32_t RESERVED3[2]; /*!< Reserved, 0x48-0x4C */
__IO uint32_t AHB1LPENR; /*!< RCC AHB1 peripheral clock enable in low power mode register, Address offset: 0x50 */
__IO uint32_t AHB2LPENR; /*!< RCC AHB2 peripheral clock enable in low power mode register, Address offset: 0x54 */
__IO uint32_t AHB3LPENR; /*!< RCC AHB3 peripheral clock enable in low power mode register, Address offset: 0x58 */
uint32_t RESERVED4; /*!< Reserved, 0x5C */
__IO uint32_t APB1LPENR; /*!< RCC APB1 peripheral clock enable in low power mode register, Address offset: 0x60 */
__IO uint32_t APB2LPENR; /*!< RCC APB2 peripheral clock enable in low power mode register, Address offset: 0x64 */
uint32_t RESERVED5[2];
__IO uint32_t BDCR;
__IO uint32_t CSR;
uint32_t RESERVED6[2];
__IO uint32_t SSCGR;
__IO uint32_t PLLI2SCFGR;
__IO uint32_t PLLSAICFGR;
__IO uint32_t DCKCFGR;
} RCC_TypeDef;
对照《STM32F4中文参考手册》6.3节 P114~170页,了解这些寄存器的作用和配置方法。
1.时钟使能配置:
RCC_HSICmd, RCC_LSICmd, RCC_PLLCmd, RCC_PLLI2SCmd, RCC_PLLSAICmd, RCC_RTCCLKCmd,RCC_AHBxPeriphClockCmd
RCC_APBxPeriphClockCmd
2.时钟源和时钟相关配置:
RCC_HSEConfig, RCC_LSEConfig, RCC_PLLConfig, RCC_PLLI2SConfig, RCC_PLLSAIConfig, RCC_MCO1Config, RCC_MCO2Config,
RCC_SYSCLKConfig, RCC_HCLKConfig,RCC_PCLK1Config,RCC_PCLK2Config,RCC_RTCCLKConfig,RCC_I2SCLKConfig
3.外设复位函数
RCC_AHB1PeriphResetCmd,RCC_AHB2PeriphResetCmd,RCC_AHB3PeriphResetCmd,RCC_APB1PeriphResetCmd,RCC_APB2PeriphResetCmd
4.状态参数获取参数:
RCC_GetSYSCLKSource,RCC_GetClocksFreq,RCC_GetFlagStatus,RCC_ClearFlag
5.RCC中断相关函数 :
RCC_ITConfig,RCC_ClearITPendingBit,RCC_GetITStatus
三、系统时钟初始化函数解读
系统时钟初始化函数:
SystemInit()初始化之后的状态:
SYSCLK(系统时钟) =168MHz
AHB总线时钟(HCLK=SYSCLK) =168MHz
APB1总线时钟(PCLK1=SYSCLK/4) =42MHz
APB2总线时钟(PCLK2=SYSCLK/2) =84MHz
PLL主时钟 =168MHz