随笔 - 51  文章 - 0  评论 - 0  阅读 - 5496

GD32开发板【GD32330C-START】RTC功能使用

 在项目中增加一个RTC的显示功能,阅读官方技术手册发现, gd32本身自带RTC功能。GD32F330的RTC模块提供了一个包含日期(年/月/日)和时间(时/分/秒/亚秒)的日历功能。除亚秒用二进制码显示外,时间和日期都以BCD码的形式显示。RTC可以进行夏令时补偿。RTC可以工作在省电模式下,并通过软件配置来智能唤醒。RTC支持外接更高精度的低频时钟,用以达到更高的日历精度。
       于是使用该功能进行时钟显示,通过串口进行配置,最后可以通过串口和OLED进行日期和时间的显示。
       在工程目录Drivers文件夹下添加rtc源文件和头文件,主要代码如下。
初始化代码:

void Init_RTC(void)
{
/* enable PMU clock */
rcu_periph_clock_enable(RCU_PMU);
/* enable the access of the RTC registers */
pmu_backup_write_enable();
rcu_osci_on(RCU_IRC40K);
rcu_osci_stab_wait(RCU_IRC40K);
rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
prescaler_s = 0x18F;
prescaler_a = 0x63;
rcu_periph_clock_enable(RCU_RTC);
rtc_register_sync_wait();
}

串口配置时钟代码:

/*!
rief use hyperterminal to setup RTC time and alARM
param[in] none
param[out] none
etval none
*/
void rtc_setup(void)
{
/* setup RTC time value */
uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
rtc_initpara.rtc_factor_asyn = prescaler_a;
rtc_initpara.rtc_factor_syn = prescaler_s;
rtc_initpara.rtc_year = 0x18;
rtc_initpara.rtc_day_of_week = RTC_FRIDAY;
rtc_initpara.rtc_month = RTC_SEP;
rtc_initpara.rtc_date = 0x27;
rtc_initpara.rtc_display_format = RTC_24HOUR;
rtc_initpara.rtc_am_pm = RTC_AM;

串口输出时钟显示代码:

/*!
rief display the current time
param[in] none
param[out] none
etval none
*/
void rtc_show_time(void)
{
uint32_t time_subsecond = 0;
uint8_t subsecond_ss = 0,subsecond_ts = 0,subsecond_hs = 0;
rtc_current_time_get(&rtc_initpara);
/* get the subsecond value of current time, and convert it into fractional format */
time_subsecond = rtc_subsecond_get();
subsecond_ss=(1000-(time_subsecond*1000+1000)/400)/100;
subsecond_ts=(1000-(time_subsecond*1000+1000)/400)%100/10;
subsecond_hs=(1000-(time_subsecond*1000+1000)/400)%10;
printf("Current time: 20%0.2x-%0.2x-%0.2x %0.2x:%0.2x:%0.2x .%d%d%d
",
rtc_initpara.rtc_year, rtc_initpara.rtc_month , rtc_initpara.rtc_date,
rtc_initpara.rtc_hour, rtc_initpara.rtc_minute, rtc_initpara.rtc_second,
subsecond_ss, subsecond_ts, subsecond_hs);
}

OLED显示代码:

OLED_DisBMP(48u,0u,80u,4u,(uint8_t *)CLOCK_logo);
sprintf(outputstring,"Date:20%0.2x-%0.2x-%0.2x",rtc_info.rtc_year,rtc_info.rtc_month , rtc_info.rtc_date);
OLED_DisString(4u,4u,(uint8_t *)outputstring);
sprintf(outputstring,"Time:%0.2x:%0.2x:%0.2x",rtc_info.rtc_hour, rtc_info.rtc_minute, rtc_info.rtc_second);
OLED_DisString(4u,6u,(uint8_t *)outputstring);

串口配置时钟和输出显示效果:

 OLED最终显示效果:

 

posted on   慧茗子  阅读(388)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示