|
這里我們寫一個RCC配置函數(shù)來說明各函數(shù)的用途,其中HSE = 8MHz。
/**
* @說明 配置STM32F407的時鐘系統(tǒng)
* @參數(shù) 無
* @返回 無
* @說明 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);
/*等待外部晶振進(jìn)入穩(wěn)定狀態(tài)*/
while( RCC_WaitForHSEStartUp() != SUCCESS );
/*
**我們要選擇PLL時鐘作為系統(tǒng)時鐘,因此這里先要對PLL時鐘進(jìn)行配置
*/
/*選擇外部晶振作為PLL的時鐘源*/
/* 到這一步為止,已有 HSE_VALUE = 8 MHz.
PLL_VCO input clock = (HSE_VALUE or HSI_VALUE / PLL_M),
根據(jù)文檔,這個值被建議在 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,
這個值要用來計算系統(tǒng)時鐘,我們 令 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;
/*這個系數(shù)用來配置SD卡讀寫,USB等功能,暫時不用,根據(jù)文檔,暫時先設(shè)為7*/
PLL_Q = 7;
/* 配置PLL并將其使能,獲得 168Mhz 的 System Clock 時鐘*/
RCC_PLLConfig(RCC_PLLSource_HSE, PLL_M, PLL_N, PLL_P, PLL_Q);
RCC_PLLCmd(ENABLE);
/*到了這一步,我們已經(jīng)配置好了PLL時鐘。下面我們配置Syetem Clock*/
/*選擇PLL時鐘作為系統(tǒng)時鐘源*/
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/*到了這一步,我們已經(jīng)配置好了系統(tǒng)時鐘,頻率為 168MHz. 下面我們可以對 AHB,APB,外設(shè)等的 時鐘進(jìn)行配置*/
/*時鐘的結(jié)構(gòu)請參考用戶手冊*/
/*首先配置 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);
/*****函數(shù)結(jié)束******/
/*以上函數(shù)可以大體上說明這些庫函數(shù)的作用*/
}
對于 RCC_PLLConfig();函數(shù),大家可能會迷惑。
其函數(shù)原型為:
void RCC_PLLConfig(uint32_t RCC_PLLSource,
uint32_t PLLM,
uint32_t PLLN,
uint32_t PLLP,
uint32_t PLLQ);
迷惑的地方肯定在于后面 4個參數(shù) PLLM / PLLN / PLLP / PLLQ.
在庫函數(shù)源文件 system_stm32f4xx.c 中可以找到這 4個參數(shù)的說明,請看下圖注釋部分:
(下面的是從 庫函數(shù)源文件 stm32f4xx_rcc.c 中找到的。其他函數(shù)可以直接去看庫函數(shù),注釋非常詳細(xì))
/**
* @brief Configures the main PLL clock source, multiplication and division factors.
@簡介 配置主PLL時鐘源,以及分頻因子 (PLL不止一個,還有一個用來為音頻處理提供高質(zhì)量時鐘)
* @note This function must be used only when the main PLL is disabled.
* @注意 這個函數(shù)只能在主PLL失能時才能使用
* @param RCC_PLLSource: specifies the PLL entry clock source.
@參數(shù) RCC_PLLSource:選擇PLL時鐘源
* This parameter can be one of the following values:
這個參數(shù)可以是如下值:
* @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
@參數(shù) PLLM:設(shè)置 PLL VCO 輸入時鐘的 除法因子(division factor)
* This parameter must be a number between 0 and 63.
這個參數(shù) 范圍是 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.
@參數(shù) PLLN 選擇 PLL VCO輸出時鐘的 乘法因子(multiplication factor )
這個參數(shù)的值 介于 192 ~432
* @note You have to set the PLLN parameter correctly to ensure that the VCO
* output frequency is between 192 and 432 MHz.
* @注意 你學(xué)要正確選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}.
@參數(shù) PLLP 選擇 系統(tǒng)時鐘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.
* @注意 你需要正確設(shè)置PLLP,確保系統(tǒng)時鐘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.
@參數(shù) PLLQ 選擇給 OTG FS(USB), SDIO(SD卡讀寫), RNG(隨機(jī)數(shù)發(fā)生器)
時鐘的除法因子,其值介于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,你需要正確設(shè)置PLLQ,確保USB有
48MHz的時鐘。但是對于SDIO,RNG需要一個小于或等于48MHz的時鐘
* @retval None |
|