/**
* @說明 配置STM32F407的時鐘系統
* @參數 無
* @返回 無
* @說明 void Clock_Config(void) 按如下表格配置時鐘
*
*==================================================================
* Supported STM32F4xx device revision | Rev A
*-----------------------------------------------------------------------------
* System Clock source | PLL (HSE)
*-----------------------------------------------------------------------------
* SYSCLK(Hz) | 168000000
*-----------------------------------------------------------------------------
* HCLK(Hz) | 168000000
*-----------------------------------------------------------------------------
* AHB Prescaler | 1
*-----------------------------------------------------------------------------
* APB1 Prescaler | 4
*-----------------------------------------------------------------------------
* APB2 Prescaler | 2
*-----------------------------------------------------------------------------
* HSE Frequency(Hz) | 8000000
*-----------------------------------------------------------------------------
* PLL_M |8
*-----------------------------------------------------------------------------
* PLL_N | 336
*-----------------------------------------------------------------------------
* PLL_P | 2
*-----------------------------------------------------------------------------
* PLL_Q |7
*===================================================================
*/
void Clock_Config(void){
ErrorStatus State;
uint32_t PLL_M;
uint32_t PLL_N;
uint32_t PLL_P;
uint32_t PLL_Q;
/*配置前將所有RCC重置爲初始值*/
RCC_DeInit();
/*這裏選擇 外部晶振(HSE)作爲 時鐘源,因此首先打開外部晶振*/
RCC_HSEConfig(RCC_HSE_ON);
/*等待外部晶振進入穩定狀態*/
while( RCC_WaitForHSEStartUp() != SUCCESS );
/*
**我們要選擇PLL時鐘作爲系統時鐘,因此這裏先要對PLL時鐘進行配置
*/
/*選擇外部晶振作爲PLL的時鐘源*/
/* 到這一步爲止,已有 HSE_VALUE = 8 MHz.
PLL_VCO input clock = (HSE_VALUE or HSI_VALUE / PLL_M),
根據文檔,這個值被建議在 1~2MHz,因此我們令 PLL_M = 8,
即 PLL_VCO input clock = 1MHz */
PLL_M = 8;
/* 到這一步爲止,已有 PLL_VCO input clock = 1 MHz.
PLL_VCO output clock = (PLL_VCO input clock) * PLL_N,
這個值要用來計算系統時鐘,我們 令 PLL_N = 336,
即 PLL_VCO output clock = 336 MHz.*/
PLL_N = 336;
/* 到這一步爲止,已有 PLL_VCO output clock = 336 MHz.
System Clock = (PLL_VCO output clock)/PLL_P ,
因爲我們要 SystemClock = 168 Mhz,因此令 PLL_P = 2.
*/
PLL_P = 2;
/*這個係數用來配置SD卡讀寫,USB等功能,暫時不用,根據文檔,暫時先設爲7*/
PLL_Q = 7;
/* 配置PLL並將其使能,獲得 168Mhz 的 System Clock 時鐘*/
RCC_PLLConfig(RCC_PLLSource_HSE, PLL_M, PLL_N, PLL_P, PLL_Q);
RCC_PLLCmd(ENABLE);
/*到了這一步,我們已經配置好了PLL時鐘。下面我們配置Syetem Clock*/
/*選擇PLL時鐘作爲系統時鐘源*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/*到了這一步,我們已經配置好了系統時鐘,頻率爲 168MHz. 下面我們可以對 AHB,APB,外設等的 時鐘進行配置*/
/*時鐘的結構請參考用戶手冊*/
/*首先配置 AHB時鐘(HCLK). 爲了獲得較高的頻率,我們對 SYSCLK 1分頻,得到HCLK*/
RCC_HCLKConfig(RCC_HCLK_Div1);
/*APBx時鐘(PCLK)由AHB時鐘(HCLK)分頻得到,下面我們配置 PCLK*/
/*APB1時鐘配置. 4分頻,即 PCLK1 = 42 MHz*/
RCC_PCLK1Config(RCC_HCLK_Div4);
/*APB2時鐘配置. 2分頻,即 PCLK2 = 84 MHz*/
RCC_PCLK2Config(RCC_HCLK_Div2);
/*****函數結束******/
/*以上函數可以大體上說明這些庫函數的作用*/
}
對於 RCC_PLLConfig();函數,大家可能會迷惑。
其函數原型爲:
void RCC_PLLConfig(uint32_t RCC_PLLSource,
uint32_t PLLM,
uint32_t PLLN,
uint32_t PLLP,
uint32_t PLLQ);
迷惑的地方肯定在於後面 4個參數 PLLM / PLLN / PLLP / PLLQ.
在庫函數源文件 system_stm32f4xx.c 中可以找到這 4個參數的說明,請看下圖註釋部分:
(下面的是從 庫函數源文件 stm32f4xx_rcc.c 中找到的。其他函數可以直接去看庫函數,註釋非常詳細)
/**
* @brief Configures the main PLL clock source, multiplication and division factors.
@簡介 配置主PLL時鐘源,以及分頻因子 (PLL不止一個,還有一個用來爲音頻處理提供高質量時鐘)
* @note This function must be used only when the main PLL is disabled.
* @注意 這個函數只能在主PLL失能時才能使用
* @param RCC_PLLSource: specifies the PLL entry clock source.
@參數 RCC_PLLSource:選擇PLL時鐘源
* This parameter can be one of the following values:
這個參數可以是如下值:
* @arg RCC_PLLSource_HSI: HSI oscillator clock selected as PLL clock entry
選擇HSI作爲PLL時鐘源
* @arg RCC_PLLSource_HSE: HSE oscillator clock selected as PLL clock entry
選擇HSE作爲PLL時鐘源
* @note This clock source (RCC_PLLSource) is common for the main PLL and PLLI2S.
*
* @param PLLM: specifies the division factor for PLL VCO input clock
@參數 PLLM:設置 PLL VCO 輸入時鐘的 除法因子(division factor)
* This parameter must be a number between 0 and 63.
這個參數 範圍是 0 ~ 63
* @note You have to set the PLLM parameter correctly to ensure that the VCO input
* frequency ranges from 1 to 2 MHz. It is recommended to select a frequency
* of 2 MHz to limit PLL jitter.
* @注意 你需要正確選擇 PLLM的值, 使得 VCO輸入頻率 介於 1~2MHz.
建議選擇 2MHz 來限制PLL震盪(jitter?)
* @param PLLN: specifies the multiplication factor for PLL VCO output clock
* This parameter must be a number between 192 and 432.
@參數 PLLN 選擇 PLL VCO輸出時鐘的 乘法因子(multiplication factor )
這個參數的值 介於 192 ~432
* @note You have to set the PLLN parameter correctly to ensure that the VCO
* output frequency is between 192 and 432 MHz.
* @注意 你學要正確選PLLN的大小,以保證VCO輸出時鐘介於 192 ~432MHz
* @param PLLP: specifies the division factor for main system clock (SYSCLK)
* This parameter must be a number in the range {2, 4, 6, or 8}.
@參數 PLLP 選擇 系統時鐘SYSCLK 的除法因子(division factor ),這個
值可以是2,4,6,8
* @note You have to set the PLLP parameter correctly to not exceed 168 MHz on
* the System clock frequency.
* @注意 你需要正確設置PLLP,確保系統時鐘SYSCLK不超過168MHz
* @param PLLQ: specifies the division factor for OTG FS, SDIO and RNG clocks
* This parameter must be a number between 4 and 15.
@參數 PLLQ 選擇給 OTG FS(USB), SDIO(SD卡讀寫), RNG(隨機數發生器)
時鐘的除法因子,其值介於4~15
* @note If the USB OTG FS is used in your application, you have to set the
* PLLQ parameter correctly to have 48 MHz clock for the USB. However,
* the SDIO and RNG need a frequency lower than or equal to 48 MHz to work
* correctly.
* @注意 如果在你的程序中用到 USB OTG FS,你需要正確設置PLLQ,確保USB有
48MHz的時鐘。但是對於SDIO,RNG需要一個小於或等於48MHz的時鐘
* @retval None