我在程序运行中修改CH32V208的主频,经常遇到 程序跑着跑着不跑了的情况。但是不变频就没问题?请问变频有什么注意事项?以下是我的变频程序
void MySystemInit (uint8_t clock) //clock:144 主频144MHz, clock:32 主频32MHz,
{
RCC->CTLR |= (uint32_t)0x00000001;
RCC->CFGR0 &= (uint32_t)0xF0FF0000;
RCC->CTLR &= (uint32_t)0xFEF6FFFF;
RCC->CTLR &= (uint32_t)0xFFFBFFFF;
RCC->CFGR0 &= (uint32_t)0xFF00FFFF;
RCC->INTR = 0x009F0000;
switch(clock){
case 144 :
MySetSysClockTo144_HSE();
SystemCoreClock=144000000;
break; /* 可选的 */
case 72 :
MySetSysClockTo72_HSE();
SystemCoreClock=72000000;
break; /* 可选的 */
case 32 :
MySetSysClockToHSE();//32MHz
SystemCoreClock=((uint32_t)32000000);
break;
/* 您可以有任意数量的 case 语句 */
default : /* 可选的 */
MySetSysClockTo144_HSE();
SystemCoreClock=144000000;
;
}
USART_Printf_Init( 1000000 );
SystemCoreClockUpdate();
Delay_Init();
}
static void MySetSysClockToHSE(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
RCC->CTLR |= ((uint32_t)RCC_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTLR & RCC_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CTLR & RCC_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV1;
/* Select HSE as system clock source
* CH32V20x_D6 (HSE=8MHZ)
* CH32V20x_D8 (HSE=32MHZ)
* CH32V20x_D8W (HSE=32MHZ)
*/
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
/* Wait till HSE is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
{
}
}
else
{
/* If HSE fails to start-up, the application will have wrong clock
* configuration. User can add here some code to deal with this error
*/
}
}