单片机 > 单片机程序设计 > 详情

STM32:外部晶振时钟配置

发布时间:2024-08-27 发布时间:
|

void SystemClock_Config(void)  

{  

    RCC_DeInit();  

    RCC_HSEConfig(RCC_HSE_ON); //INPUT HSE = 24M      

    ErrorStatus HSEStartUpStatus = RCC_WaitForHSEStartUp();  

    if(HSEStartUpStatus == SUCCESS)   

    {  

        FLASH_PrefetchBufferCmd(ENABLE);    //M0 defined   

        FLASH_SetLatency(FLASH_Latency_1);  //M0 defined   

        RCC_PREDIV1Config(RCC_PREDIV1_Div1);  

        RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_2);   //PLL CLK = HSE *RCC_PLLSource_PREDIV1*2 = 48M    

        RCC_PLLCmd(ENABLE);                                   //enable PLL CLK        

        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)    //WAIT PLL CLK ready   

        {  

        }  

        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //SELECT SYSCLK = PLLCLK =48M  

        // *           - 0x00: HSI used as system clock  

        // *           - 0x04: HSE used as system clock    

        // *           - 0x08: PLL used as system clock  

        // *           - 0x0C: HSI48 used as system clock, applicable only for STM32F072 devices    

        while(RCC_GetSYSCLKSource()!=0x08)  

        {  

        }  

        RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB CLK(HCLK) = SYS CLK = 48M  

        RCC_PCLKConfig(RCC_HCLK_Div1);   //APB CLK(PCLK) = HCLK = 48M  

        RCC_USARTCLKConfig(RCC_USART1CLK_PCLK);  // UART2 CLK = 48M  

        RCC_USARTCLKConfig(RCC_USART2CLK_PCLK);  // UART2 CLK = 48M  

        RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);    // I2C CLK =48M  

        RCC_USBCLKConfig(RCC_USBCLK_PLLCLK);     // USB CLK = 48M  

    }  

}  



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
STM32学习笔记4:外部中断