stm32CUBEIDE 生成的时钟配置代码详解

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};    //声明并初始化
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};    //声明并初始化
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};  //声明并初始化

  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE
                              |RCC_OSCILLATORTYPE_LSE;  //晶振类型
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;  //打开HSE
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; //对应框1
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;  //打开LSE
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;  //打开HSI ?为什么要打开?难道为了
//HSE出现故障时,能切换到HSI?
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;  //打开LSI ,IWDG需要 
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; //打开PLL
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; //对应框3
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; //对应框4
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; //SYSCLK来源于PLLCLK  //对应框5
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;//AHB来源于SYSCLK÷2,对应框6

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;//APB1来源于HCLK÷1,对应框7

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; //APB2来源于HCLK÷2,对应框8



  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_ADC;
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;   //RTC时钟来源于LSE ,对应框9 
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;     //ADC分频6,对应框10
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
  /** Enables the Clock Security System 
  */
  HAL_RCC_EnableCSS(); //打开时钟安全监测
}

stm32CUBEIDE 生成的时钟配置代码详解_第1张图片

你可能感兴趣的:(stm32)