一、使用HSE配置系统时钟的过程
1.开启HSE,并等待HSE稳定
__IO uint32_t HSEStartUpStatus = 0;
RCC_HSEConfig(RCC_HSE_ON);
HSEStatus = RCC_WaitForHSEStartUp();
注:在判断是否HSE启动的时候,不用函数RCC_GetFlagStatus()获取状态位来判断,是因为RCC_WaitForHSEStartUp()函数不仅包含了RCC_GetFlagStatus(),即要检测状态位,也要做超时处理HSE_STARTUP_TIMEOUT
2.设置AHB、APB2、APB1的预分频因子
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
3.设置PLL的时钟来源,和PLL倍频因子,开启PLL,等到PLL稳定
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, pllmul);
RCC_PLLCmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
4.将系统时钟SYSCLK来源设置为PLLCLK
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while (RCC_GetSYSCLKSource() != 0x08)
{
}
二、代码
void HSE_SetSysClock(uint32_t pllmul)
{
__IO uint32_t HSEStartUpStatus = 0;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStatus = RCC_WaitForHSEStartUp();
if (HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, pllmul);
RCC_PLLCmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while (RCC_GetSYSCLKSource() != 0x08)
{
}
}
else{
while (1)
{
}
}
}
|