Seven, STM32 clock system

1. Clock tree introduction

 

There are a total of five clock sources, divided by speed, of which 1, 2, 3 are high-speed clock sources, and 4, 5 are low-speed clock sources. According to the internal and external division of the chip, 1, 5 are internal clock sources, 3 and 4 are external clock sources

Clock source No. 1 HSI is the internal high-speed clock source, RC oscillator, 8M frequency, can be used as the system clock, or an input of the PLL phase-locked loop

The No. 3 clock source HSE is an external high-speed clock source, provided by an external crystal oscillator, which is 23,24 pins, and an external 4-16M crystal oscillator or clock provides the clock source

Clock source No. 5 LSI is the internal low-speed clock source, the frequency is 40K, RC oscillator, can provide the clock source of independent watchdog or RPC

The 4th clock source LSE is an external low-speed clock source, the external pins are PC14, PC15, and a 32.768K crystal oscillator is connected to the Puzhong development board, which is the clock for RTC

The clock source PLL 2 is to multiply the input clock frequency, because the system clock is up to 72M, so the clock frequency needs to be multiplied and output. It can be the internal clock source No. 1 HSI after dividing by two to provide the clock source, or the external clock source No. 3 HSE without dividing or dividing by two.

The function of the system initialization function SystemInit is to determine the output frequency of the PLL and the system clock of the peripheral. The clock frequency after SystemInit():

SYSCLK (system clock)=72MHz

AHB bus clock (HCLK=SYSCLK) =72MHz

APB1 bus clock (PCLK1=SYSCLK/2)=36MHz

APB2 bus clock (PCLK2=SYSCLK/1)=72MHz

PLL main clock = 72MHz

MCO can provide a clock for the outside. The PA8 pin is used on the common board. The source of MCO can be PLL frequency divider (72M/2), HSI (8M), HSE (8M) and SYSCLK (72M)

The frequency of APB2 bus (maximum 72M) is greater than the frequency of APB1 bus (maximum 36M)

Two, clock configuration function introduction

(1) Clock enable configuration function

The stm32f10x_rcc.c file contains functions for clock configuration, enable, and acquisition. The ones ending with Cmd are usually enable functions, those ending with Config are configuration functions, and those ending with Init are initialization functions.

Clock source enable function:

RCC_HSICmd (FunctionalState NewState), the parameter is ENABLE enable or DISABLE disable

RCC_LSICmd(FunctionalState NewState)

RCC_PLLCmd(FunctionalState NewState)

Peripheral enable function:

RCC_RTCCLKCmd(FunctionalState NewState)

RCC_AHBPeriphClockCmd(FunctionalState NewState)

RCC_APBxPeriphClockCmd(FunctionalState NewState)

(2) Clock source and multiplication factor related configuration functions

RCC_HSEConfig

RCC_LSEConfig

RCC_PLLConfig

RCC_MCOConfig

RCC_SYSCLKConfig specifies the source of the system clock

RCC_HCLKConfig

RCC_PCLK1Config

RCC_PCLK2Config

RCC_RTCCLKConfig

RCC_ADCCLKConfig

RCC_USBCLKConfig

(3) Peripheral reset function

RCC_APB1PeriphResetCmd

RCC_APB2PeriphResetCmd

(4) State parameter acquisition function

RCC_GetSYSCLKSource

RCC_GetClocksFreq

RCC_GetFlagStatus

RCC_ClearFlag

(5) RCC interrupt related functions

RCC_ITConfig

RCC_ClearITPendingBit

RCC GetITStatus
 

3. System clock setting steps

The frequency multiplication factor of PLLMUL is 2~16. By modifying the frequency multiplication factor, the system clock can be changed, and a system clock function can be customized

void RCC_HSE_Config(u32 div,u32 pllm)//自定义系统时间(可以修改时钟)
{
	RCC_DeInit();//将外设RCC寄存器重设为缺省值,也就是复位状态
	RCC_HSEConfig(RCC_HSE_ON);//开启外部高速晶振(HSE)
	if(RCC_WaitForHSEStartUp()==SUCCESS)//等待HSE起振
	{
		RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB时钟(HCLK),1分频就是72M
		RCC_PCLK1Config(RCC_HCLK_Div2);//设置低速AHB时钟(PCLK1),设置APB1的时钟为二分频就是36M
		RCC_PCLK2Config(RCC_HCLK_Div1);//设置高速AHB时钟(PCLK2),设置APB2的时钟为一分频72M
		RCC_PLLConfig(div,pllm);//设置PLL时钟源及倍频系数
		RCC_PLLCmd(ENABLE) ;//使能或者失能PLL
		while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//检查指定的RCc标志位设置与否,PLL就绪
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置系统时钟SYSCLK
		while(RCC_GetSYSCLKSource()!=0x08);//返回用作系统时钟的时钟源,0x08: PLL作为系统时钟
	}
}

div is the frequency division coefficient of the clock source, and pllm is the frequency multiplication coefficient. div is the clock source of 8M, pllm is the frequency multiplication factor of 2-16. For example, RCC_HSE_Config (RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); that is, the 8M clock source is divided by 1, and the pllm multiplication factor is 9, then the system clock source is 8M/1*9=72M; another example is RCC_HSE_Config (RCC_PLLSource_HSE_Div2, RCC_PLLMul_9); Divide by 2, pllm multiplication factor is 9, then the system clock source is 8M/2*9=36M

 

Four, programming

Observe the modification of the system clock through the blinking of the LED light, first realize the program to light up the LED light. Six, use the library function to light up an LED_ not silly gown-CSDN blog , and then add the code in the main.c program

#include "stm32f10x.h"
#include "led.h"

//延时函数
void delay(u32 i)
{
	while(i--);
}
void RCC_HSE_Config(u32 div,u32 pllm)//自定义系统时间(可以修改时钟)
{
	RCC_DeInit();//将外设RCC寄存器重设为缺省值,也就是复位状态
	RCC_HSEConfig(RCC_HSE_ON);//开启外部高速晶振(HSE)
	if(RCC_WaitForHSEStartUp()==SUCCESS)//等待HSE起振
	{
		RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB时钟(HCLK)
		RCC_PCLK1Config(RCC_HCLK_Div2);//设置低速AHB时钟(PCLK1)
		RCC_PCLK2Config(RCC_HCLK_Div1);//设置高速AHB时钟(PCLK2)
		RCC_PLLConfig(div,pllm);//设置PLL时钟源及倍频系数
		RCC_PLLCmd(ENABLE) ;//使能或者失能PLL
		while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);//检查指定的RCc标志位设置与否,PLL就绪
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置系统时钟SYSCLK)
		while(RCC_GetSYSCLKSource()!=0x08);//返回用作系统时钟的时钟源,0x08: PLL作为系统时钟
	}
}
int main()
{
	//第一次系统时钟
	RCC_HSE_Config(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//72M
	//第二次系统时钟
	//RCC_HSE_Config(RCC_PLLSource_HSE_Div2,RCC_PLLMul_9);//36M
	
	LED_Init();
	
	while(1)
	{
		GPIO_ResetBits(GPIOC,GPIO_Pin_0);
		delay(6000000);
		GPIO_SetBits(LED_PORT,GPIO_Pin_0);
		delay(6000000);
	}
}

Set the system clock to 72M for the first time, observe the result of the LED light flickering, set the clock to 36M for the second time, observe again, you can find that the frequency of the LED light flickers slower

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_40836442/article/details/110132529