1. 程式人生 > >系統時鐘配置

系統時鐘配置

cloc 寄存器 int deinit buffer onf nal max tor

一、修改晶振頻率(STM32F1/4xx.h)

  #define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */

  #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/

二、使用外部晶振作為系統時鐘源

  1.STM32F4xx (system_stm32f4xx.c)

  RCC->PLLCFGR = PLL_M | (PLL_N << 6) | (((PLL_P

>> 1) -1) << 16) | (RCC_PLLCFGR_PLLSRC_HSE) | (PLL_Q << 24);

  2.STM32F1xx(system_stm32f1xx.c) 

  RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);

  修改這幾個分/倍頻系數。

三、配置內部時鐘

  1.STM32F4xx(system_stm32f4xx.c)

  SetSysClock();--> RCC_Configuration(16,336,2,7); (SYSCLK = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N/ PLL_P)

  void RCC_Configuration(uint32_t m, uint32_t n, uint32_t p, uint32_t q)
  {
    __IO uint32_t HSIStartUpStatus = 0;
    RCC_DeInit();
    RCC_HSICmd(ENABLE);
    HSIStartUpStatus = RCC->CR & RCC_CR_HSIRDY;
    if (HSIStartUpStatus == RCC_CR_HSIRDY) {
      RCC->APB1ENR |= RCC_APB1ENR_PWREN;

      PWR->CR |= PWR_CR_VOS;
      RCC_HCLKConfig(RCC_SYSCLK_Div1);
      RCC_PCLK2Config(RCC_HCLK_Div2);
      RCC_PCLK1Config(RCC_HCLK_Div4);
      RCC_PLLConfig(RCC_PLLSource_HSI, m, n, p, q);
      RCC_PLLCmd(ENABLE);
      while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {
      }
      FLASH->ACR = FLASH_ACR_PRFTEN | FLASH_ACR_ICEN |FLASH_ACR_DCEN |FLASH_ACR_LATENCY_5WS;
      RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
      while (RCC_GetSYSCLKSource() != 0x08){
      }
    }
    else{
      while(1) {
      }
    }
  }

  2.STM32F1xx  

  //use HSI as system clock MAX freq = 64M HZ
  void RCC_Configuration()
  {
    RCC_DeInit(); /*將外設RCC寄存器重設為缺省值 */
    RCC_HSICmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY)== RESET);//等待HSI就緒
    RCC_HCLKConfig(RCC_SYSCLK_Div1); /*設置AHB時鐘(HCLK) RCC_SYSCLK_Div1——AHB時鐘 = 系統時*/
    RCC_PCLK2Config(RCC_HCLK_Div1); /* 設置高速AHB時鐘(PCLK2)RCC_HCLK_Div1——APB2時鐘 = HCLK*/
    RCC_PCLK1Config(RCC_HCLK_Div2); /*設置低速AHB時鐘(PCLK1)RCC_HCLK_Div2——APB1時鐘 = HCLK / 2*/
    FLASH_SetLatency(FLASH_Latency_2); /*設置FLASH存儲器延時時鐘周期數FLASH_Latency_2 2延時周期*/
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /*選擇FLASH預取指緩存的模,預取指緩存使能*/
    RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_16);/*設置PLL時鐘源及倍頻系數,頻率為8/2*16=64Mhz*/
    RCC_PLLCmd(ENABLE); /*使能PLL */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) ; /*檢查指定的RCC標誌位(PLL準備好標誌)設置與否*/
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /*設置系統時鐘(SYSCLK) */
    while(RCC_GetSYSCLKSource() != 0x08); /*0x08:PLL作為系統時鐘 */
  }

系統時鐘配置