7027|2

98

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

【GD32F350开发分享二】GD32F350时钟配置,非常重要 [复制链接]

大家应该都知道,微控制器最重要的就是时钟频率,时钟频率会影响指令周期、定时器、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. }
复制代码



此帖出自GD32 MCU论坛

最新回复

nmg
谢谢分享 眼熟,原来是参与过美信大赛,期待后续  详情 回复 发表于 2018-9-22 23:58
点赞 关注(1)
 

回复
举报

5260

帖子

239

TA的资源

管理员

沙发
 
谢谢分享
眼熟,原来是参与过美信大赛,期待后续
此帖出自GD32 MCU论坛
加EE小助手好友,
入技术交流群
EE服务号
精彩活动e手掌握
EE订阅号
热门资讯e网打尽
聚焦汽车电子软硬件开发
认真关注技术本身

点评

哈哈 被你发现了  详情 回复 发表于 2018-9-27 20:56
 
 
 

回复

98

帖子

0

TA的资源

一粒金砂(中级)

板凳
 
nmg 发表于 2018-9-22 23:58
谢谢分享
眼熟,原来是参与过美信大赛,期待后续

哈哈 被你发现了
此帖出自GD32 MCU论坛
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表