void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 8; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { Error_Handler(); } }逐句注释这段函数
时间: 2024-03-20 17:42:17 浏览: 206
好的,我来逐句注释这段函数:
```c
void SystemClock_Config(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage */
__HAL_RCC_PWR_CLK_ENABLE(); // 使能电源时钟
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); // 配置主内部稳压器的输出电压
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure. */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; // RCC振荡器类型为HSE
RCC_OscInitStruct.HSEState = RCC_HSE_ON; // 使能HSE时钟源
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; // 使能PLL
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; // PLL时钟源为HSE
RCC_OscInitStruct.PLL.PLLM = 8; // PLL输入时钟频率除以M (M=8)
RCC_OscInitStruct.PLL.PLLN = 336; // PLL倍频系数N为336
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; // PLL输出时钟频率除以P (P=2)
RCC_OscInitStruct.PLL.PLLQ = 4; // PLL输出时钟频率除以Q (Q=4)
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) // 初始化RCC振荡器
{
Error_Handler(); // 如果初始化失败,则调用错误处理函数
}
/** Initializes the CPU, AHB and APB buses clocks */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; // 配置系统时钟和总线时钟类型
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; // 系统时钟源为PLL
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // AHB总线时钟为系统时钟
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; // APB1总线时钟为系统时钟的1/4
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; // APB2总线时钟为系统时钟的1/2
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) // 初始化CPU、AHB和APB总线时钟
{
Error_Handler(); // 如果初始化失败,则调用错误处理函数
}
}
```
这段代码主要是对 STM32 微控制器的时钟进行配置,包括配置内部稳压器输出电压、配置 RCC 振荡器、配置 PLL 和配置 CPU、AHB 和 APB 总线时钟。其中,PLL 是锁相环,可以通过调节 PLLM、PLLN、PLLP、PLLQ 等参数来调节系统时钟频率。
阅读全文