1.思维导图
1.1根据思维导图在库函数中找相关函数来调用
2.代码部分
/*
*
*关于在调试过程中遇到的问题:
*1.忘记在C/C++选项卡中Include Path中添加./clk文件夹路径
* 导致用不了stm32f4xx_rcc.h,编写过程中调用的函数报错;
*2.调用函数时,参数设置问题,看清楚参数说明;
*3.函数调用顺序:比如RCC_PLLConfig和RCC_PLLCmd,在RCC_PLLConfig函数说明中有这么一句话
* @note This function must be used only when the main PLL is disabled.
*/
#include "bsp_rccclkconfig.h"
void HSE_SetSysClk(uint32_t m,uint32_t n,uint32_t p,uint32_t q)
{
__IO uint32_t HSEStartUPStatus = 0; //用来记录HSE启动的状态
//1.使能HSE,等待HSE稳定
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUPStatus = RCC_WaitForHSEStartUp();//将HSE返回值StartUp赋给HSEStartStatus
if(HSEStartUPStatus == SUCCESS)//HSE启动成功
{
/* 选择电压调节器输出模式为1,使能电源时钟
Select regulator voltage output Scale 1 mode */
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
PWR->CR |= PWR_CR_VOS;
//2.设置AHB、APB2、APB1的预分频因子
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div4);
//3.设置PLL时钟来源
//根据函数说明,RCC_PLLConfig须在PLL使能之前配置
//This function must be used only when the main PLL is disabled.
RCC_PLLConfig(RCC_PLLSource_HSE, m, n, p, q);
//4.使能PLL,并等其稳定
RCC_PLLCmd(ENABLE);
//4-1.等待PLL时钟稳定
//@brief Checks whether the specified RCC flag is set or not.
//RCC_FLAG_HSERDY
//@retval: RESET or SET. RCCRCC_GetFlagStatus(RCC_FLAG_HSERDY) The new state of RCC_FLAG (SET or RESET).
//判断返回状态
while( (RCC_GetFlagStatus(RCC_FLAG_HSERDY)) == RESET )
{
}
/* 为了达到更高的频率Enable the Over-drive to extend the clock frequency to 180 Mhz */
PWR->CR |= PWR_CR_ODEN;
while((PWR->CSR & PWR_CSR_ODRDY) == 0)
{
}
PWR->CR |= PWR_CR_ODSWEN;
while((PWR->CSR & PWR_CSR_ODSWRDY) == 0)
{
}
/*配置预取值,指令缓存,数据缓存和等待周期Configure Flash prefetch, Instruction cache, Data cache and wait state */
FLASH->ACR = FLASH_ACR_PRFTEN |
FLASH_ACR_ICEN |
FLASH_ACR_DCEN |
FLASH_ACR_LATENCY_5WS;
//5.选取PLLCLK作为SYSCLK系统时钟
//Configures the system clock (SYSCLK).
//@arg RCC_SYSCLKSource_PLLCLK: PLL selected as system clock source (RCC_SYSCLKSource_PLLPCLK for STM32F446xx devices)
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//5-1.如果SYSCLK没有被选取为系统时钟,进入while循环
while( (RCC_GetSYSCLKSource() ) != 0x08)
{
}
}
else
{
}
}