时钟,并使MCO1/2输出系统时钟"/>
使用固件库配置HSE系统时钟,并使MCO1/2输出系统时钟
16-使用HSE配置系统时钟,并使用MCO输出
【基于野火STM32F407ZGT6】
一、固件库配置系统时钟HSE
(一)配置流程
- 复位RCC
- HSE 外部高速时钟使能
- 等待外部高速时钟设置成功
- 选择电压调节
- 配置系统时钟
- 配置main PLL
- 使能main PLL
- 等待main PLL 启动成功
(二)配置代码
HSE 系统时钟配置函数
void HSE_SetSysClock( uint32_t PLLM, uint32_t PLLN, uint32_t PLLP, uint32_t PLLQ)
{ErrorStatus HSE_ErrorStatus = ERROR;// 复位RCC所有寄存器RCC_DeInit();//使能HSERCC_HSEConfig(RCC_HSE_ON);HSE_ErrorStatus = RCC_WaitForHSEStartUp();if (HSE_ErrorStatus == SUCCESS){/* Select regulator voltage output Scale 1 mode *//*选择电压调节为1 */RCC->APB1ENR |= RCC_APB1ENR_PWREN;PWR->CR |= PWR_CR_VOS;//配置系统时钟RCC_HCLKConfig(RCC_SYSCLK_Div1); //168MHzRCC_PCLK1Config(RCC_HCLK_Div4); //42MHzRCC_PCLK2Config(RCC_HCLK_Div2); //84MHz//配置锁相环倍频输出因子,PLL//固件库的方式配置RCC_PLLConfig(RCC_PLLSource_HSE, PLLM, PLLN, PLLP, PLLQ);//使能PLLRCC_PLLCmd(ENABLE);while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET){}/* Configure Flash prefetch, Instruction ache, Data cache and wait state *//* 配置FLASH 预取 */FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;//配置系统时钟来源RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//检查当前系统时钟是否为PLLCKwhile( RCC_GetSYSCLKSource() != 0x08){}}else {}
}
(三)详细解释和注释
特有名词解释:
- HSE: HIGH SPEED EXTERNAL
- RCC: RESET CLOCK CONTROL
- HCLK: High-speed Clock
- PCLK: Peripheral Clock
- APB:APB(Advanced Peripheral Bus),片上外设总线。主要用于慢速片上外设与ARM核的通讯
- AHB:AHB(Advanced High performance Bus),高性能总线。主要用于系统高性能、高时钟速率模块间通信,。
重要函数
/*** @brief Configures the main PLL clock source, multiplication and division factors.* @note This function must be used only when the main PLL is disabled.* * @param RCC_PLLSource: specifies the PLL entry clock source.* This parameter can be one of the following values:* @arg RCC_PLLSource_HSI: HSI oscillator clock selected as PLL clock entry* @arg RCC_PLLSource_HSE: HSE oscillator clock selected as PLL clock entry* @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* This parameter must be a number between 0 and 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.* * @param PLLN: specifies the multiplication factor for PLL VCO output clock* This parameter must be a number between 50 and 432.* @note You have to set the PLLN parameter correctly to ensure that the VCO* output frequency is between 100 and 432 MHz.* * @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}.* @note You have to set the PLLP parameter correctly to not exceed 168 MHz on* the System clock frequency.* * @param PLLQ: specifies the division factor for OTG FS, SDIO and RNG clocks* This parameter must be a number between 4 and 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.* * @retval None*/
void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t PLLM, uint32_t PLLN, uint32_t PLLP, uint32_t PLLQ);
二、 MCO输出配置
(一)资源
- MCO1 → PA8
- MCO2 → PC9
(二)配置MCO1&MCO2的GPIO
void MCO1_GPIO_INIT(void) //MCO1 -> PA8
{//1. 开外设时钟//2. 定义一个GPIO初始化结构体//3. 配置GPIO初始化结构体的成员//4. 调用GPIO初始化函数,把配置好的结构体成员的参数写入寄存器RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIO_InitStruct;GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;GPIO_Init(GPIOA,&GPIO_InitStruct);}void MCO2_GPIO_INIT(void) //MCO2 -> PC9
{//1. 开外设时钟//2. 定义一个GPIO初始化结构体//3. 配置GPIO初始化结构体的成员//4. 调用GPIO初始化函数,把配置好的结构体成员的参数写入寄存器RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);GPIO_InitTypeDef GPIO_InitStruct;GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;GPIO_Init(GPIOC,&GPIO_InitStruct);}
三、总览
(一)main代码
#include "stm32f4xx.h" //包含了基本的寄存器映射文件#include "bsp_led.h"
#include "bsp_clk_config.h"
void Delay(uint32_t count)
{for(;count!=0;count--);
}
int main()
{LED_GPIO_Config();HSE_SetSysClock(25, 336, 2, 7); // M=25, N=336,P=2(AHB,APBx来源),Q=7(Q输出一定为48MHz)RCC_MCO1Config(RCC_MCO1Source_PLLCLK, RCC_MCO1Div_1);RCC_MCO2Config(RCC_MCO2Source_SYSCLK, RCC_MCO2Div_1);while(1){GPIO_ResetBits(GPIOF,GPIO_Pin_6);Delay(0xfffff);GPIO_SetBits(GPIOF,GPIO_Pin_6);Delay(0xfffff);}
}
FROM : =4
Time : 2020年12月15日 17:10:52
更多推荐
使用固件库配置HSE系统时钟,并使MCO1/2输出系统时钟
发布评论