【GD32F350开发分享二】GD32F350时钟配置,非常重要

大家应该都知道,微控制器最重要的就是时钟频率,时钟频率会影响指令周期、定时器、RTC等等所有涉及时钟的总线。
GD32F350开发板的外部高速时钟8MHZ晶振和外部低速时钟32.768KHZ晶振没有焊接,大家有需要自行焊接。
我用的是内部时钟8MHZ,当然,如果需要用时钟频率进行定时器中断设置的,建议使用外部晶振,因为外部晶振精度高,偏移量小,但是缺点在于功耗就大了,内部时钟功耗低,但是精度会差一些;
以下是我的时钟配置

  1.  
  2. /* system frequency define */
  3. #define __IRC8M           (IRC8M_VALUE)            /* internal 8 MHz RC oscillator frequency */
  4. #define __HXTAL           (HXTAL_VALUE)            /* high speed crystal oscillator frequency */
  5. #define __SYS_OSC_CLK     (__IRC8M)                /* main oscillator frequency */
  6.  
  7. /* select a system clock by uncommenting the following line */
  8. #if defined (GD32F330)
  9. //#define __SYSTEM_CLOCK_8M_HXTAL              (__HXTAL)
  10. //#define __SYSTEM_CLOCK_8M_IRC8M              (__IRC8M)
  11. //#define __SYSTEM_CLOCK_72M_PLL_HXTAL         (uint32_t)(72000000)
  12. //#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2    (uint32_t)(72000000)
  13. //#define __SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2     (uint32_t)(72000000)
  14. #define __SYSTEM_CLOCK_84M_PLL_HXTAL           (uint32_t)(84000000)
  15. //#define __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2    (uint32_t)(84000000)
  16. #endif /* GD32F330 */
  17.  
  18. #if defined (GD32F350)
  19. //#define __SYSTEM_CLOCK_8M_HXTAL              (__HXTAL)
  20. #define __SYSTEM_CLOCK_8M_IRC8M              (__IRC8M)
  21. //#define __SYSTEM_CLOCK_72M_PLL_HXTAL         (uint32_t)(72000000)
  22. //#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2    (uint32_t)(72000000)
  23. //#define __SYSTEM_CLOCK_84M_PLL_HXTAL         (uint32_t)(84000000)
  24. //#define __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2    (uint32_t)(84000000)
  25. //#define __SYSTEM_CLOCK_96M_PLL_HXTAL         (uint32_t)(96000000)
  26. //#define __SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2      (uint32_t)(96000000)
  27. //#define __SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2     (uint32_t)(96000000)
  28. //#define __SYSTEM_CLOCK_108M_PLL_HXTAL        (uint32_t)(108000000)
  29. //#define __SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2   (uint32_t)(108000000)
  30. #endif /* GD32F350 */
  31.  
  32. #define SEL_IRC8M       0x00
  33. #define SEL_HXTAL       0x01
  34. #define SEL_PLL         0x02
  35.  
  36. /* set the system clock frequency and declare the system clock configuration function */
  37. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  38. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_HXTAL;
  39. static void system_clock_8m_hxtal(void);
  40.  
  41. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  42. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_HXTAL;
  43. static void system_clock_72m_hxtal(void);
  44.  
  45. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  46. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2;
  47. static void system_clock_72m_irc8m(void);
  48.  
  49. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2)
  50. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2;
  51. static void system_clock_72m_irc48m(void);
  52.  
  53. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  54. uint32_t SystemCoreClock = __SYSTEM_CLOCK_84M_PLL_HXTAL;
  55. static void system_clock_84m_hxtal(void);
  56.  
  57. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  58. uint32_t SystemCoreClock = __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2;
  59. static void system_clock_84m_irc8m(void);
  60.  
  61. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  62. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_HXTAL;
  63. static void system_clock_96m_hxtal(void);
  64.  
  65. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  66. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2;
  67. static void system_clock_96m_irc8m(void);
  68.  
  69. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2)
  70. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2;
  71. static void system_clock_96m_irc48m(void);
  72.  
  73. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  74. uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_HXTAL;
  75. static void system_clock_108m_hxtal(void);
  76.  
  77. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  78. uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2;
  79. static void system_clock_108m_irc8m(void);
  80.  
  81. #else
  82. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_IRC8M;
  83. static void system_clock_8m_irc8m(void);
  84. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  85.  
  86. /* configure the system clock */
  87. static void system_clock_config(void);
  88.  
  89. /*!
  90.     \brief      setup the microcontroller system, initialize the system
  91.     \param[in]  none
  92.     \param[out] none
  93.     \retval     none
  94. */
  95. void SystemInit (void)
  96. {
  97.     /* enable IRC8M */
  98.     RCU_CTL0 |= RCU_CTL0_IRC8MEN;
  99.     while(0U == (RCU_CTL0 & RCU_CTL0_IRC8MSTB)){
  100.     }
  101.     /* reset RCU */
  102.     RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  103.                   RCU_CFG0_ADCPSC | RCU_CFG0_CKOUTSEL | RCU_CFG0_CKOUTDIV | RCU_CFG0_PLLDV);
  104.     RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  105. #if (defined(GD32F350))
  106.     RCU_CFG0 &= ~(RCU_CFG0_USBFSPSC);
  107.     RCU_CFG2 &= ~(RCU_CFG2_CECSEL | RCU_CFG2_USBFSPSC2);
  108. #endif /* GD32F350 */
  109.     RCU_CTL0 &= ~(RCU_CTL0_HXTALEN | RCU_CTL0_CKMEN | RCU_CTL0_PLLEN | RCU_CTL0_HXTALBPS);
  110.     RCU_CFG1 &= ~(RCU_CFG1_PREDV | RCU_CFG1_PLLMF5 | RCU_CFG1_PLLPRESEL);
  111.     RCU_CFG2 &= ~(RCU_CFG2_USART0SEL | RCU_CFG2_ADCSEL);
  112.     RCU_CFG2 &= ~RCU_CFG2_IRC28MDIV;
  113.     RCU_CFG2 &= ~RCU_CFG2_ADCPSC2;
  114.     RCU_CTL1 &= ~RCU_CTL1_IRC28MEN;
  115.     RCU_ADDCTL &= ~RCU_ADDCTL_IRC48MEN;
  116.     RCU_INT = 0x00000000U;
  117.     RCU_ADDINT = 0x00000000U;
  118.     /* configure system clock */
  119.     system_clock_config();
  120. }
  121.  
  122. /*!
  123.     \brief      configure the system clock
  124.     \param[in]  none
  125.     \param[out] none
  126.     \retval     none
  127. */
  128. static void system_clock_config(void)
  129. {
  130. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  131.     system_clock_8m_hxtal();
  132. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  133.     system_clock_72m_hxtal();
  134. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  135.     system_clock_72m_irc8m();
  136. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2)
  137.     system_clock_72m_irc48m();
  138. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  139.     system_clock_84m_hxtal();
  140. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  141.     system_clock_84m_irc8m();
  142. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  143.     system_clock_96m_hxtal();
  144. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  145.     system_clock_96m_irc8m();
  146. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2)
  147.     system_clock_96m_irc48m();
  148. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  149.     system_clock_108m_hxtal();
  150. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  151.     system_clock_108m_irc8m();
  152. #else
  153.     system_clock_8m_irc8m();
  154. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  155. }

复制代码


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高咩咩在人间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值