HSE:72M,HSE:64M,HSI:64M
代码实现:
// 时钟设置
//#define MAINCLOCK_72M // HSE,外部时钟
//#define MAINCLOCK_64M_EXTERN // HSE
#define MAINCLOCK_64M_INTERAL // HSI
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
RCC_OSCInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks
*/
#ifdef MAINCLOCK_72M
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
//RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; // 72M
#else
#ifdef MAINCLOCK_64M_EXTERN
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8; // 64M
#else
// MAINCLOCK_64M_INTERAL
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16; // 64M
#endif
#endif
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;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; // 72M/64M
#ifdef MAINCLOCK_72M
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8; // 72 / 8 = 9M
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4; // 72 / 4 = 18M
#else
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8;// 64 / 8 = 8M
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;// 64 / 4 = 16M
#endif
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, SysTick_IRQn_PP, 0);
}
上一篇:STM32之RTC原理
下一篇:STM32 中断实现的程序步骤
推荐阅读最新更新时间:2024-03-16 16:21