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

本文档介绍了如何在GD32F330项目中利用内置RTC模块设置及显示日期和时间。通过配置RCU时钟源,初始化RTC并设置亚秒、日期和时间,然后使用串口和OLED进行显示。代码示例包括RTC初始化、串口配置时钟、时钟显示以及OLED显示功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在项目中增加一个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最终显示效果:

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值