The previous section details the system default clock configuration, and the number of clock output, which is the default configuration, but the actual use of the clock default clock does not meet the requirements, so you need to know how to call the library function to configure.
The best information is to consult the STM32F2XX_RCC.C file, which has a variety of function functions, and has detailed comments, which let us such a novice to get started is very fast.
//VCO = PLL input Clock (HSE or HSI)/pllm//Octave Voltage Post Value v = VCO * Plln//SYSCLK = V/PLLP//Pll48ck = v/pllq#definePLLM 25//Divide factor#definePlln 240//Multiply frequency factor#definePLLP 2//Divide factor#definePLLQ 5//Divide factorvu16 adc_convertedvalue[2];//defining arrays, storing converted data for PA6 and PA7floatPa6_ad_value;//Pressure sensorfloatPa7_ad_value;//Lithium battery voltagevoidDelay (__io uint32_t ncount);voidSetClock (void);intCheckdatas (void);/*Private Functions---------------------------------------------------------*//** * @brief Main program. * @param None * @retval None*/intMainvoid) {unsignedinti =0; Rcc_clockstypedef rcc_clocks; SetClock (); Usart_config (); Rcc_getclocksfreq (&rcc_clocks); printf ("\r\nsysclk_frequency =%d mhz\n", Rcc_clocks. sysclk_frequency/1000000); printf ("\r\nhclk_frequency =%d mhz\n", Rcc_clocks. hclk_frequency/1000000); printf ("\r\npclk1_frequency =%d mhz\n", Rcc_clocks. pclk1_frequency/1000000); printf ("\r\npclk2_frequency =%d mhz\n", Rcc_clocks. pclk2_frequency/1000000);
Above is the main function part of the code, the clock set Setclock () function, after set up, you can call the library function Rcc_getclockfreq (&rcc_clocks) to check the configured clock is correct. Before using this function, define a RCC_CLOCKTYPEDEF rcc_clocks structure variable that can be passed through the parameter to access the struct member variable in the rcc_clocktypedef.
Next introduce the Setclocks () function, read the Chinese annotation directly
voidSetClock (void) {rcc_deinit ();//Reset All configuration Clocks on the system//Rcc_hsicmd (DISABLE);//Rcc_deinit () after the HSI as the system clock, in which the call is not in effectRcc_lsicmd (DISABLE);//close the internal low-speed clockRcc_plli2scmd (DISABLE);//Close Plli2s ClockRcc_rtcclkcmd (DISABLE);//disables the RTC clockRcc_hseconfig (rcc_hse_on);//external Clock Enable while(! Rcc_waitforhsestartup ());//Waits for HSE start-uprcc_pllconfig (Rcc_pllsource_hse, PLLM, Plln, PLLP, PLLQ);//Configuring the PLL clockRcc_pllcmd (ENABLE);//PLL Clock Enable while((RCC->CR & rcc_cr_pllrdy) = =0);//wait for the PLL clock to be ready.Rcc_sysclkconfig (RCC_SYSCLKSOURCE_PLLCLK);//PLL CLK as System clock SYSCLK =120mRcc_hsicmd (DISABLE);//close the internal high-speed clock, if the internal high-speed clock as the system clock, this function does not take effectRcc_hclkconfig (RCC_SYSCLK_DIV1);//AHB = sysclk non-crossover 120MRcc_pclk1config (RCC_HCLK_DIV4);//PCLK1 4 divide = 30MRcc_pclk2config (RCC_HCLK_DIV2);//PCLK2 2 divide = 60M}
It is noted here that the HSE clock enables not to use Rcc_hsecmd (enable) function, but instead uses the Rcc_hseconfig (rcc_hse_on), so that after the can be sure to wait for the HSE start-up,
Likewise, after the PLL is enabled, it is also necessary to wait for a while when configuring the clock is best set according to the clock tree structure, first what is configured in what, then the PLL clock is first configured in enable, note the order.
Also note that when the internal high-speed clock is not required, use Rcc_hsicmd (DISABLE), can not be called with the comment of the function, that is, "if the internal high-speed clock as the system clock, the clock can not stop" that is, when the clock as the system clock, Calling this function is not valid, and after using Rcc_deinit (), the internal high-speed clock is used as the system clock, so the function is placed after the Rcc_sysclkconfig () function.
The data printed on the serial port conforms to the theoretical value, such as:
Novice words may not care about those clocks off, but on the project, considering the low power consumption of the unnecessary clock to turn off, save electricity, and when in hibernation, you can also consider reducing the main clock, because the faster the frequency, the more electricity.
Configuration of the STM32F2 series clocks