我在程序運(yùn)行中修改CH32V208的主頻,經(jīng)常遇到 程序跑著跑著不跑了的情況。但是不變頻就沒(méi)問(wèn)題?請(qǐng)問(wèn)變頻有什么注意事項(xiàng)?以下是我的變頻程序
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;
? ? ? /* 您可以有任意數(shù)量的 case 語(yǔ)句 */
? ? ? 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
? ? ? ? ?*/
? }
}