时钟频率的配置-DG32
HXTAL:高速外部时钟,4到32MHz的外部振荡器,可为系统提供精确的主时钟。带有特定频率的晶体必须靠近两个HXTAL的引脚。和晶体连接的外部电阻和电容必须根据所选择的振荡器来调整;
LXTAL:低速外部时钟,一个32.768kHz的低速外部晶体或陶瓷谐振器。它为实时时钟电路提供一个低功耗且精确的时钟源。
IRC8M:高速内部时钟,高速内部8MHz时钟,拥有8MHz的固定频率,设备上电后CPU默认选择的时钟源就是IRC8M时钟;
IRC40K:低速内部时钟,它的时钟频率大约40 kHz,为独立看门狗定时器和实时时钟电路提供时钟;
IRC48M: 有一个固定的频率48MHz,可用作USB时钟或者PLL时钟源
内部时钟配置
void SystemInit (void)
{
/* FPU settings */
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */
#endif
/* reset the RCU clock configuration to the default reset state */
/* Set IRC8MEN bit */
RCU_CTL |= RCU_CTL_IRC8MEN; //打开内部8MHz RC振荡器
while(0U == (RCU_CTL & RCU_CTL_IRC8MSTB)){ //等待内部8MHz RC振荡器稳定
}
RCU_MODIFY(0x50); //AHB时钟分频设置 先二分频再四分频
RCU_CFG0 &= ~RCU_CFG0_SCS; //00:选择 CK_IRC8M 时钟作为 CK_SYS 时钟源
#if (defined(GD32F30X_HD) || defined(GD32F30X_XD))
/* reset HXTALEN, CKMEN and PLLEN bits */
/*外部高速时钟使能位、HXTAL 时钟监视器使能位、PLL使能位 复位,方便后续操作*/
RCU_CTL &= ~(RCU_CTL_PLLEN | RCU_CTL_CKMEN | RCU_CTL_HXTALEN);
/* disable all interrupts */
/*关闭中断位*/
RCU_INT = 0x009f0000U;
#elif defined(GD32F30X_CL)
/* Reset HXTALEN, CKMEN, PLLEN, PLL1EN and PLL2EN bits */
RCU_CTL &= ~(RCU_CTL_PLLEN |RCU_CTL_PLL1EN | RCU_CTL_PLL2EN | RCU_CTL_CKMEN | RCU_CTL_HXTALEN);
/* disable all interrupts */
RCU_INT = 0x00ff0000U;
#endif
/* reset HXTALBPS bit */
RCU_CTL &= ~(RCU_CTL_HXTALBPS); //旁路模式位复位
/* Reset CFG0 and CFG1 registers */
RCU_CFG0 = 0x00000000U;
RCU_CFG1 = 0x00000000U;
/* configure the system clock source, PLL Multiplier, AHB/APBx prescalers and Flash settings */
system_clock_config();
}
外部时钟
AHB总线为系统时钟的1分频
APB1总线为系统时钟的2分频
APB2总线为系统时钟的1分频
例子一
例子二
108=(8/2)*RCU_PLLMUPLX
RCU_PLLMUPLX=27