HSE_SetSysCLK是野火教程里的,不懂的去这 16-RCC(第3节)使用HSE配置系统时钟并使用MCO输出监控系统时钟_哔哩哔哩_bilibili
HSE_AutoSetHSE的算法部分是自己写的,用了一个转接数组。C语言不支持bool所以自己定义了一个boolK代替bool。
AutoHSE.h:
/**
*@file AutoHSE.h
*@version 自定
*/#ifndef __AUTOSETHSE_H
#define __AUTOSETHSE_Htypedef enum Bool_
{False = 0,True = !False
}boolK;void HSE_SetSysCLK(uint32_t RCC_PLLMul_x);
boolK HSE_AutoSetHSE(uint32_t Soften);#endif /*! __AUTOSETHSE_H */
AutoHSE.c:
#include "AutoHSE.h"uint32_t RCC_PLLMul_X[15] = {
RCC_PLLMul_2,
RCC_PLLMul_3,
RCC_PLLMul_4,
RCC_PLLMul_5,
RCC_PLLMul_6,
RCC_PLLMul_7,
RCC_PLLMul_8,
RCC_PLLMul_9,
RCC_PLLMul_10,
RCC_PLLMul_11,
RCC_PLLMul_12,
RCC_PLLMul_13,
RCC_PLLMul_14,
RCC_PLLMul_15,
RCC_PLLMul_16
};void HSE_SetSysCLK(uint32_t RCC_PLLMul_x)
{ErrorStatus HSEStatus; //typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;??????????RCC_DeInit(); //??RCC_HSEConfig(RCC_HSE_ON); //??HSEHSEStatus=RCC_WaitForHSEStartUp(); // ??HSE??if(HSEStatus==SUCCESS){FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //?????FLASH_SetLatency(FLASH_Latency_2); // 72M???2???RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK???RCC_PCLK1Config(RCC_HCLK_Div2); //PCLK1???RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2???RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_x); //?????RCC_PLLCmd(ENABLE); //??while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET){}; //??PLL??RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //??????while(RCC_GetSYSCLKSource()!=0x08);}else{}
}boolK HSE_AutoSetHSE(uint32_t Soften)
{if(Soften > 128 || Soften < 2){return False;}else{HSE_SetSysCLK(RCC_PLLMul_X[(Soften-(Soften%8))/8-2]);return True;}return False;
}// _____END_________
亲测,有用:D,喜欢就点个赞,支持一下我这个新手吧qwq.