在使用STM32F030F4P6芯片内部晶振时,需要在启动代码中配置芯片的时钟。以下是使用STM32CubeIDE生成的默认启动代码中配置内部晶振的示例代码,你可以参考:
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/* Configure the main internal regulator output voltage */
__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_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
在这段代码中,首先通过 __HAL_PWR_VOLTAGESCALING_CONFIG() 函数配置芯片电压,并初始化 RCC_OscInitTypeDef 和 RCC_ClkInitTypeDef 结构体。然后,通过 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI 将内部高速时钟(HSI)作为时钟源,通过 RCC_OscInitStruct.HSIState = RCC_HSI_ON 打开内部高速时钟。最后,通过 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI 将内部高速时钟作为系统时钟源,并将其他时钟分频设置为1。
注意,在使用内部晶振时,需要使能内部高速时钟(HSI),并将其作为系统时钟源。如果需要使用其他时钟源,可以根据需要进行修改。同时,根据实际情况,需要配置FLASH的读取等待状态(latency)。 |