[STM32F0] STM32F030 使用内部时钟和外部时钟

[复制链接]
454|0
 楼主| 梅花香自123 发表于 2021-3-22 23:15 | 显示全部楼层 |阅读模式

  1. static void SetSysClock(void)
  2. {
  3.   __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
  4.   
  5.   /* SYSCLK, HCLK, PCLK configuration ----------------------------------------*/
  6. #if defined (PLL_SOURCE_HSI)
  7.   /* At this stage the HSI is already enabled */

  8.   /* Enable Prefetch Buffer and set Flash Latency */
  9.   FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;

  10.   /* HCLK = SYSCLK */
  11.   RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  12.       
  13.   /* PCLK = HCLK */
  14.   RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;

  15.   /* PLL configuration = (HSI/2) * 12 = ~48 MHz */
  16.   RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  17.   RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSI_Div2 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL12);
  18.             
  19.   /* Enable PLL */
  20.   RCC->CR |= RCC_CR_PLLON;

  21.   /* Wait till PLL is ready */
  22.   while((RCC->CR & RCC_CR_PLLRDY) == 0)
  23.   {
  24.   }

  25.   /* Select PLL as system clock source */
  26.   RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  27.   RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   

  28.   /* Wait till PLL is used as system clock source */
  29.   while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  30.   {
  31.   }
  32. #else
  33. #if defined (PLL_SOURCE_HSE)
  34.   /* Enable HSE */   
  35.   RCC->CR |= ((uint32_t)RCC_CR_HSEON);
  36. #elif defined (PLL_SOURCE_HSE_BYPASS)
  37.   /* HSE oscillator bypassed with external clock */   
  38.   RCC->CR |= (uint32_t)(RCC_CR_HSEON | RCC_CR_HSEBYP);
  39. #endif /* PLL_SOURCE_HSE */
  40.    
  41.   /* Wait till HSE is ready and if Time out is reached exit */
  42.   do
  43.   {
  44.     HSEStatus = RCC->CR & RCC_CR_HSERDY;
  45.     StartUpCounter++;  
  46.   } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));

  47.   if ((RCC->CR & RCC_CR_HSERDY) != RESET)
  48.   {
  49.     HSEStatus = (uint32_t)0x01;
  50.   }
  51.   else
  52.   {
  53.     HSEStatus = (uint32_t)0x00;
  54.   }  

  55.   if (HSEStatus == (uint32_t)0x01)
  56.   {
  57.     /* Enable Prefetch Buffer and set Flash Latency */
  58.     FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;

  59.     /* HCLK = SYSCLK */
  60.     RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
  61.       
  62.     /* PCLK = HCLK */
  63.     RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV1;

  64.     /* PLL configuration = HSE * 6 = 48 MHz */
  65.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
  66.     RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
  67.             
  68.     /* Enable PLL */
  69.     RCC->CR |= RCC_CR_PLLON;

  70.     /* Wait till PLL is ready */
  71.     while((RCC->CR & RCC_CR_PLLRDY) == 0)
  72.     {
  73.     }

  74.     /* Select PLL as system clock source */
  75.     RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
  76.     RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;   

  77.     /* Wait till PLL is used as system clock source */
  78.     while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL)
  79.     {
  80.     }
  81.   }
  82.   else
  83.   { /* If HSE fails to start-up, the application will have wrong clock
  84.          configuration. User can add here some code to deal with this error */
  85.   }  
  86. #endif /* PLL_SOURCE_HSI */  
  87. }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

102

主题

1216

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部