First, in the STM32, there are five clock sources for HSI,HSE,LSI,LSE,PLL.
The ①HSI is a high-speed internal clock with an RC oscillator at a frequency of 8MHz.
The ②HSE is a high-speed external clock that can be connected to a quartz/ceramic resonator or to an external clock source with a frequency range of 4mhz~16mhz.
The ③LSI is a low-speed internal clock, an RC oscillator with a frequency of 40kHz.
The ④LSE is a low-speed external clock with a quartz crystal with a frequency of 32.768kHz.
The ⑤PLL is a phase-locked loop multiplier output whose clock input source can be selected as HSI/2, HSE, or HSE/2. The multiplier can be selected as 2~16 times, but its output frequency must not exceed 72MHz.
Second, on the STM32 if the external crystal oscillator is not used, osc_in and osc_out: If the internal RC oscillator is used instead of the external crystal oscillator, proceed as follows:
① for 100 feet or 144 feet of products, osc_in should be grounded, osc_out should be suspended.
② for products with less than 100 feet, there are 2 ways to connect: The 1th type: osc_in and Osc_out are grounded by 10K resistors respectively. This approach improves EMC performance, and the 2nd is to remap osc_in and osc_out to PD0 and PD1 respectively, and then configure PD0 and PD1 for push-pull output and output ' 0 '. This method reduces the power dissipation and (relative to the above) saves 2 external resistors.
Third, with the HSE clock, the program set the clock parametersProcess:
01, re-set the RCC register to the default value rcc_deinit;
02, open the external high-speed clock crystal HSE rcc_hseconfig (rcc_hse_on);
03, waiting for external high-speed clock crystal oscillator work Hsestartupstatus = Rcc_waitforhsestartup ();
04, set AHB clock rcc_hclkconfig;
05, set the high-speed AHB clock rcc_pclk2config;
06, set the Low speed AHB clock rcc_pclk1config;
07, set the PLL rcc_pllconfig;
08. Turn on the PLL rcc_pllcmd (ENABLE);
09. Wait for the PLL to work while (Rcc_getflagstatus (rcc_flag_pllrdy) = = RESET)
10, set the system clock rcc_sysclkconfig;
11. Determine if the PLL is the system clock while (Rcc_getsysclksource ()! = 0x08)
12. Open the peripheral clock to be used Rcc_apb2periphclockcmd ()/rcc_apb1periphclockcmd ()
The following is the configuration function of RCC in the program of STM32 Software firmware library (using external 8MHz crystal oscillator)
/*******************************************************************************
* Function name:rcc_configuration
* DESCRIPTION:RCC configuration (using external 8MHz crystal oscillator)
* Input: None
* Output: None
* Return: None
*******************************************************************************/
void Rcc_configuration (void)
{
/* Set the peripheral RCC register to the default value */
Rcc_deinit ();
/* Set external high-speed crystal (HSE) */
Rcc_hseconfig (rcc_hse_on); Rcc_hse_on--hse Crystal Open (on)
/* Wait for HSE to start vibration */
Hsestartupstatus = Rcc_waitforhsestartup ();
if (Hsestartupstatus = = SUCCESS)//success:hse Crystal Stable and ready
{
/* Set AHB Clock (HCLK) */
Rcc_hclkconfig (RCC_SYSCLK_DIV1); RCC_SYSCLK_DIV1--AHB Clock = System clock
/* Set high speed AHB clock (PCLK2) */
Rcc_pclk2config (RCC_HCLK_DIV1); RCC_HCLK_DIV1--APB2 Clock = hclk
/* Set Low Speed AHB clock (PCLK1) */
Rcc_pclk1config (RCC_HCLK_DIV2); RCC_HCLK_DIV2--APB1 Clock = HCLK/2
/* Set flash memory delay clock cycles */
Flash_setlatency (flash_latency_2); Flash_latency_2 2 delay Period
/* Select Flash prefetch refers to cached mode */
flash_prefetchbuffercmd (flash_prefetchbuffer_enable); //