STM32F1改用内部时钟HSI,配置系统时钟64M

参考:https://blog.csdn.net/zxh1592000/article/details/78644

工程下载链接:https://download.csdn.net/download/weixin_42225238/10934926

因项目需要改用内部时钟配置系统时钟,使用8MHSI,配置64M系统时钟。此文记录修改过程。

1. 修改在SystemInit (void)中被调用的SetSysClock()函数。

/* If none of the define above is enabled, the HSI is used as System clock
    source (default after reset) */ 

在外部晶振失效时,系统也是可以工作的,此时的系统时钟是8MHZ,此时外设时钟需要更改相应配置。

原工程配置72M时钟,定义了SYSCLK_FREQ_72MHz,

第一步注释SYSCLK_FREQ_72MHz。

第二步增加SetSysClockTo64();函数

static void SetSysClockTo64()
{
    __IO uint32_t HSIStartUpStatus = 0;

    HSIStartUpStatus = RCC->CR & RCC_CR_HSIRDY;

    if (HSIStartUpStatus == RCC_CR_HSIRDY) 
    {
        /* Enable Prefetch Buffer */
        FLASH->ACR |= FLASH_ACR_PRFTBE;

        /* Flash 2 wait state */
        FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
        FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;    

        RCC_HCLKConfig(RCC_SYSCLK_Div1);

        RCC_PCLK2Config(RCC_HCLK_Div1);

        RCC_PCLK1Config(RCC_HCLK_Div2);

        RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_16);

        RCC_PLLCmd(ENABLE);
     
        while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) 
        {}
     
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        while (RCC_GetSYSCLKSource() != 0x08){
        }
    } else  {
        while (1)  {

       }
    }
 } 

第三步修改SysTick_Config(64000);

测试点灯函数,g_led_single_flashing(); 1s翻转,成功。

void g_led_single_flashing(void)
{
    static u32 tick_last = 0;
    
    if(tick > tick_last+1000)
    {
        tick_last = tick;
        l_gpio_toggle_pin(LED_PORT, LED_PIN);     
    }
}

 

/**
  * @brief  Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
  * @param  None
  * @retval None
  */
static void SetSysClock(void)
{
#ifdef SYSCLK_FREQ_HSE
  SetSysClockToHSE();
#elif defined SYSCLK_FREQ_24MHz
  SetSysClockTo24();
#elif defined SYSCLK_FREQ_36MHz
  SetSysClockTo36();
#elif defined SYSCLK_FREQ_48MHz
  SetSysClockTo48();
#elif defined SYSCLK_FREQ_56MHz
  SetSysClockTo56();  
#elif defined SYSCLK_FREQ_72MHz
  SetSysClockTo72();
#endif
#ifdef SYSCLK_FREQ_64MHz
   SetSysClockTo64();
#endif
 
 /* If none of the define above is enabled, the HSI is used as System clock
    source (default after reset) */ 
}

 

 

 

 

 

 

你可能感兴趣的:(时钟)