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

STM32F103 使用HSI配置系统时钟为64MHZ

发布时间:2020-06-18 发布时间:
|
  1. /** 

  2.   * @brief  Sets System clock frequency to 64MHz and configure HCLK, PCLK2  

  3.   *         and PCLK1 prescalers.  

  4.   * @note   OCS is HSI. This function should be used only after reset. 

  5.   * @param  None 

  6.   * @retval None 

  7.   */  

  8. void SetSysClockTo64Mhz(void)  

  9. {  

  10.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;  

  11.     RCC_DeInit();  

  12.     

  13.   /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/      

  14.   /* Enable HSI */      

  15.   RCC->CR |= ((uint32_t)RCC_CR_HSION);  

  16.    

  17.   /* Wait till HSI is ready and if Time out is reached exit */  

  18.   do  

  19.   {  

  20.     HSEStatus = RCC->CR & RCC_CR_HSIRDY;  

  21.     StartUpCounter++;    

  22.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));  

  23.   if ((RCC->CR & RCC_CR_HSIRDY) != RESET)  

  24.   {  

  25.     HSEStatus = (uint32_t)0x01;  

  26.   }  

  27.   else  

  28.   {  

  29.     HSEStatus = (uint32_t)0x00;  

  30.   }    

  31.   if (HSEStatus == (uint32_t)0x01)  

  32.   {  

  33.     /* Enable Prefetch Buffer */  

  34.     FLASH->ACR |= FLASH_ACR_PRFTBE;  

  35.     /* Flash 2 wait state */  

  36.     FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);  

  37.     FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;      

  38.    

  39.     /* HCLK = SYSCLK */  

  40.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;  

  41.         

  42.     /* PCLK2 = HCLK */  

  43.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;  

  44.       

  45.     /* PCLK1 = HCLK */  

  46.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;  

  47.     

  48.     /*  PLL configuration: PLLCLK = HSI/2 * 16 = 64 MHz */  

  49.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |  

  50.                                         RCC_CFGR_PLLMULL));  

  51.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2| RCC_CFGR_PLLMULL16);  

  52.   

  53.     /* Enable PLL */  

  54.     RCC->CR |= RCC_CR_PLLON;  

  55.     /* Wait till PLL is ready */  

  56.     while((RCC->CR & RCC_CR_PLLRDY) == 0)  

  57.     {  

  58.     }  

  59.       

  60.     /* Select PLL as system clock source */  

  61.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));  

  62.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;      

  63.     /* Wait till PLL is used as system clock source */  

  64.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)  

  65.     {  

  66.     }  

  67.   }  

  68.   else  

  69.   { /* If HSE fails to start-up, the application will have wrong clock  

  70.          configuration. User can add here some code to deal with this error */  

  71.   }  

  72. }  



关键字:STM32F103  HSI配置  系统时钟  64MHZ 

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

热门文章 更多
8051单片机的函数发生器的设计