注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
Justice_Gao的个人空间 https://home.eeworld.com.cn/space-uid-784175.html [收藏] [复制] [分享] [RSS]
日志

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

已有 5910 次阅读2018-9-5 19:55 |个人分类:GD32系列学习

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

  1. /* system frequency define */
  2. #define __IRC8M           (IRC8M_VALUE)            /* internal 8 MHz RC oscillator frequency */
  3. #define __HXTAL           (HXTAL_VALUE)            /* high speed crystal oscillator frequency */
  4. #define __SYS_OSC_CLK     (__IRC8M)                /* main oscillator frequency */

  5. /* select a system clock by uncommenting the following line */
  6. #if defined (GD32F330)
  7. //#define __SYSTEM_CLOCK_8M_HXTAL              (__HXTAL)
  8. //#define __SYSTEM_CLOCK_8M_IRC8M              (__IRC8M)
  9. //#define __SYSTEM_CLOCK_72M_PLL_HXTAL         (uint32_t)(72000000)
  10. //#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2    (uint32_t)(72000000)
  11. //#define __SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2     (uint32_t)(72000000)
  12. #define __SYSTEM_CLOCK_84M_PLL_HXTAL           (uint32_t)(84000000)
  13. //#define __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2    (uint32_t)(84000000)
  14. #endif /* GD32F330 */

  15. #if defined (GD32F350)
  16. //#define __SYSTEM_CLOCK_8M_HXTAL              (__HXTAL)
  17. #define __SYSTEM_CLOCK_8M_IRC8M              (__IRC8M)
  18. //#define __SYSTEM_CLOCK_72M_PLL_HXTAL         (uint32_t)(72000000)
  19. //#define __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2    (uint32_t)(72000000)
  20. //#define __SYSTEM_CLOCK_84M_PLL_HXTAL         (uint32_t)(84000000)
  21. //#define __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2    (uint32_t)(84000000)
  22. //#define __SYSTEM_CLOCK_96M_PLL_HXTAL         (uint32_t)(96000000)
  23. //#define __SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2      (uint32_t)(96000000)
  24. //#define __SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2     (uint32_t)(96000000)
  25. //#define __SYSTEM_CLOCK_108M_PLL_HXTAL        (uint32_t)(108000000)
  26. //#define __SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2   (uint32_t)(108000000)
  27. #endif /* GD32F350 */

  28. #define SEL_IRC8M       0x00
  29. #define SEL_HXTAL       0x01
  30. #define SEL_PLL         0x02

  31. /* set the system clock frequency and declare the system clock configuration function */
  32. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  33. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_HXTAL;
  34. static void system_clock_8m_hxtal(void);

  35. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  36. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_HXTAL;
  37. static void system_clock_72m_hxtal(void);

  38. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  39. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2;
  40. static void system_clock_72m_irc8m(void);

  41. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2)
  42. uint32_t SystemCoreClock = __SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2;
  43. static void system_clock_72m_irc48m(void);

  44. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  45. uint32_t SystemCoreClock = __SYSTEM_CLOCK_84M_PLL_HXTAL;
  46. static void system_clock_84m_hxtal(void);

  47. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  48. uint32_t SystemCoreClock = __SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2;
  49. static void system_clock_84m_irc8m(void);

  50. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  51. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_HXTAL;
  52. static void system_clock_96m_hxtal(void);

  53. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  54. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2;
  55. static void system_clock_96m_irc8m(void);

  56. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2)
  57. uint32_t SystemCoreClock = __SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2;
  58. static void system_clock_96m_irc48m(void);

  59. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  60. uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_HXTAL;
  61. static void system_clock_108m_hxtal(void);

  62. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  63. uint32_t SystemCoreClock = __SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2;
  64. static void system_clock_108m_irc8m(void);

  65. #else
  66. uint32_t SystemCoreClock = __SYSTEM_CLOCK_8M_IRC8M;
  67. static void system_clock_8m_irc8m(void);
  68. #endif /* __SYSTEM_CLOCK_8M_HXTAL */

  69. /* configure the system clock */
  70. static void system_clock_config(void);

  71. /*!
  72.     \brief      setup the microcontroller system, initialize the system
  73.     \param[in]  none
  74.     \param[out] none
  75.     \retval     none
  76. */
  77. void SystemInit (void)
  78. {
  79.     /* enable IRC8M */
  80.     RCU_CTL0 |= RCU_CTL0_IRC8MEN;
  81.     while(0U == (RCU_CTL0 & RCU_CTL0_IRC8MSTB)){
  82.     }
  83.     /* reset RCU */
  84.     RCU_CFG0 &= ~(RCU_CFG0_SCS | RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC |\
  85.                   RCU_CFG0_ADCPSC | RCU_CFG0_CKOUTSEL | RCU_CFG0_CKOUTDIV | RCU_CFG0_PLLDV);
  86.     RCU_CFG0 &= ~(RCU_CFG0_PLLSEL | RCU_CFG0_PLLMF | RCU_CFG0_PLLMF4 | RCU_CFG0_PLLDV);
  87. #if (defined(GD32F350))
  88.     RCU_CFG0 &= ~(RCU_CFG0_USBFSPSC);
  89.     RCU_CFG2 &= ~(RCU_CFG2_CECSEL | RCU_CFG2_USBFSPSC2);
  90. #endif /* GD32F350 */
  91.     RCU_CTL0 &= ~(RCU_CTL0_HXTALEN | RCU_CTL0_CKMEN | RCU_CTL0_PLLEN | RCU_CTL0_HXTALBPS);
  92.     RCU_CFG1 &= ~(RCU_CFG1_PREDV | RCU_CFG1_PLLMF5 | RCU_CFG1_PLLPRESEL);
  93.     RCU_CFG2 &= ~(RCU_CFG2_USART0SEL | RCU_CFG2_ADCSEL);
  94.     RCU_CFG2 &= ~RCU_CFG2_IRC28MDIV;
  95.     RCU_CFG2 &= ~RCU_CFG2_ADCPSC2;
  96.     RCU_CTL1 &= ~RCU_CTL1_IRC28MEN;
  97.     RCU_ADDCTL &= ~RCU_ADDCTL_IRC48MEN;
  98.     RCU_INT = 0x00000000U;
  99.     RCU_ADDINT = 0x00000000U;
  100.     /* configure system clock */
  101.     system_clock_config();
  102. }

  103. /*!
  104.     \brief      configure the system clock
  105.     \param[in]  none
  106.     \param[out] none
  107.     \retval     none
  108. */
  109. static void system_clock_config(void)
  110. {
  111. #ifdef __SYSTEM_CLOCK_8M_HXTAL
  112.     system_clock_8m_hxtal();
  113. #elif defined (__SYSTEM_CLOCK_72M_PLL_HXTAL)
  114.     system_clock_72m_hxtal();
  115. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC8M_DIV2)
  116.     system_clock_72m_irc8m();
  117. #elif defined (__SYSTEM_CLOCK_72M_PLL_IRC48M_DIV2)
  118.     system_clock_72m_irc48m();
  119. #elif defined (__SYSTEM_CLOCK_84M_PLL_HXTAL)
  120.     system_clock_84m_hxtal();
  121. #elif defined (__SYSTEM_CLOCK_84M_PLL_IRC8M_DIV2)
  122.     system_clock_84m_irc8m();
  123. #elif defined (__SYSTEM_CLOCK_96M_PLL_HXTAL)
  124.     system_clock_96m_hxtal();
  125. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC8M_DIV2)
  126.     system_clock_96m_irc8m();
  127. #elif defined (__SYSTEM_CLOCK_96M_PLL_IRC48M_DIV2)
  128.     system_clock_96m_irc48m();
  129. #elif defined (__SYSTEM_CLOCK_108M_PLL_HXTAL)
  130.     system_clock_108m_hxtal();
  131. #elif defined (__SYSTEM_CLOCK_108M_PLL_IRC8M_DIV2)
  132.     system_clock_108m_irc8m();
  133. #else
  134.     system_clock_8m_irc8m();
  135. #endif /* __SYSTEM_CLOCK_8M_HXTAL */
  136. }
复制代码



本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章