我编写了以下代码片段,将系统时钟设置为 32MHz。
#include "stm32f10x.h"
void SystemClock_Config(void);
int main(void)
{
SystemClock_Config();
while(1){}
return 0;
}
void SystemClock_Config(void)
{
RCC->CR |= (1<<16);//HSE clock enable >> High Speed External
while(!(RCC->CR & (1<<17))); //External high-speed clock ready flag
RCC->CR &= ~(1<<24);//PLL OFF
RCC->CFGR &= ~(0xf<<18);
RCC->CFGR |= (0x2<<18); //PLL multiplication facto >> PLL input clock x 4
RCC->CR |= (1<<24); //PLL enable
while(!(RCC->CR & (1<<25))); //PLL clock ready flag
RCC->CFGR |= (1<<1); // System clock Switch >> PLL selected as system clock
while(!(RCC->CFGR & (1<<3))); //System clock switch status >> PLL used as system clock
RCC-> CFGR &= ~(0xf<<4);
RCC-> CFGR |= (0x8<<4); //AHB prescaler >> SYSCLK divided by 2
RCC-> CFGR &= ~(0x7<<11);
RCC-> CFGR |= (0x0<<11); //APB high-speed prescaler (APB2) >> 0xx: HCLK not divided
RCC-> CFGR &= ~(0x7<<8);
RCC-> CFGR |= (0x4<<8);//APB Low-speed prescaler (APB1) >> 100: HCLK divided by 2
RCC-> APB2ENR |= ((1<<2)|(0x1<<0)); //I/O port A clock enable & Alternate function I/O clock enable
//RCC-> APB2ENR |= ((1<<2)); //I/O port A clock enable
RCC-> CFGR &= ~(0xf<<24);
RCC-> CFGR |= (0x4<<24); //Microcontroller clock output >> System clock (SYSCLK) selected
}
我的设置有误吗?或者我必须满足某些条件才能应用这些设置吗? 请指教。谢谢
经过多次尝试,我意识到了问题所在。 问题在于以下行...
RCC->CFGR |= (1<<1); // System clock Switch >> PLL selected as system clock
此行应修改如下
RCC->CFGR |= 0x1; // System clock Switch >> HSE selected as system clock