stm32f103rct6系统时钟初始化代码
时间: 2023-06-24 07:07:34 浏览: 265
下面是一个简单的stm32f103rct6系统时钟初始化代码的示例,使用的是PLL时钟源,SYSCLK频率为72MHz:
```
#include "stm32f10x.h"
void RCC_Configuration(void)
{
/* Enable HSE */
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
uint32_t StartUpCounter = 0;
uint32_t HSEStatus = 0;
do
{
HSEStatus = RCC->CR & RCC_CR_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != 0x5000));
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* Flash 2 wait state */
FLASH->ACR |= FLASH_ACR_LATENCY_2;
/* HCLK = SYSCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
/* PCLK1 = HCLK/2 */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9);
/* Enable PLL */
RCC->CR |= RCC_CR_PLLON;
/* Wait till PLL is ready */
while((RCC->CR & RCC_CR_PLLRDY) == 0);
/* Select PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08);
}
else
{
/* If HSE fails to start-up, the application will have wrong clock
configuration. User can add here some code to deal with this error */
}
}
```
在这个代码中,我们首先使能了HSE(高速外部时钟),然后等待HSE就绪。接下来,我们设置了FLASH等待状态为2,以确保在72MHz时钟下运行时Flash操作正确。
然后,我们设置了AHB总线(HCLK)的分频系数为1,APB2总线(PCLK2)的分频系数为1,APB1总线(PCLK1)的分频系数为2。
然后,我们将PLL时钟源设置为HSE,并将倍频系数设置为9,以获得72MHz的SYSCLK。然后我们使能PLL,并等待它就绪。
最后,我们将SYSCLK时钟源设置为PLL,并等待它稳定。
阅读全文