打印

GD32F103 RTC

[复制链接]
760|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
一、GD的RTC:只有GD32F405、GD32F407和GD32F450带完整的时间和日历功能,而其他系列只带时间功能,即其他系列只能读取并转换时分秒,而不能读取并转换年月日;


二、GD32f103时分秒仅支持BCD码格式;


三、RTC配置程序
void rtc_config(void)
{
            /* 使能 PMU 和 BKPI 时钟 */
    rcu_periph_clock_enable(RCU_BKPI);
    rcu_periph_clock_enable(RCU_PMU);
    /* 允许后备存储管理 */
    pmu_backup_write_enable();

   
    bkp_deinit();

    /* 使能外部低速时钟 */
    rcu_osci_on(RCU_LXTAL);
    /* 等待外部低速时钟稳定 */
    rcu_osci_stab_wait(RCU_LXTAL);
   
    /* 选取外部低速时钟为RTC时钟源 */
    rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);

    /* 使能RTC时钟 */
    rcu_periph_clock_enable(RCU_RTC);

   
    rtc_register_sync_wait();

   
    rtc_lwoff_wait();

    /* 使能秒中断和闹铃中断*/
    rtc_interrupt_enable(RTC_INT_SECOND);
    rtc_interrupt_enable(RTC_INT_ALARM);
        nvic_irq_enable(RTC_IRQn,2,1);
   
    rtc_lwoff_wait();

    /* 设置时钟分频1Hz */
    rtc_prescaler_set(32767);

   
    rtc_lwoff_wait();
               
}


四、设置RTC计数寄存器的值
u32 time_regulate(u32 h,u32 m,u32 s)
{
    u32 tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;   
    tmp_hh = h;   
    tmp_mm = m;
    tmp_ss = s;


    /* 返回存储在RTC计数寄存器上的值 */
    return((tmp_hh*3600 + tmp_mm*60 + tmp_ss));
}


五、设置时间
void time_adjust(u32 h,u32 m,u32 s)
{
   
    rtc_lwoff_wait();
    /* 改变当前时间 */
    rtc_counter_set(time_regulate(h,m,s));
   
    rtc_lwoff_wait();
}


六、显示时间
void time_display(void)
{
    uint32_t thh = 0, tmm = 0, tss = 0,timevar=rtc_counter_get();

    /* 计算小时 */
    thh = timevar / 3600;
    /* 计算分钟 */
    tmm = (timevar % 3600) / 60;
    /* 计算秒 */
    tss = (timevar % 3600) % 60;

    printf(" 时间: %0.2d:%0.2d:%0.2d\r\n", thh, tmm, tss);
}
七、主函数
#include "bitband.h"
#include "delay.h"
#include "usart.h"
#include "led.h"

#include "rtc.h"



int main(void)
{
       
        u16 i=0;
       
        delay_init(96);
        nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
        usart0_init(115200);
       
        led_config();
       
        printf( "\r\n This is a RTC demo...... \r\n" );

    if (bkp_data_read(BKP_DATA_0) != 0x5a5a){
        
        printf("\r\nThis is a RTC demo!\r\n");
        printf("\r\n\n RTC not yet configured....");

        
        rtc_config();

        printf("\r\n RTC configured....");

        
        time_adjust(11,49,00);

        bkp_data_write(BKP_DATA_0, 0x5a5a);
    }else{
        
        if (rcu_flag_get(RCU_FLAG_PORRST) != RESET){
            printf("\r\n\n Power On Reset occurred....");
        }else if (rcu_flag_get(RCU_FLAG_SWRST) != RESET){
            
            printf("\r\n\n External Reset occurred....");
        }
        
        
        rcu_periph_clock_enable(RCU_PMU);
        pmu_backup_write_enable();
        
        printf("\r\n No need to configure RTC....");
        
        rtc_register_sync_wait();

        
        rtc_interrupt_enable(RTC_INT_SECOND);

        
        rtc_lwoff_wait();
    }

#ifdef RTCCLOCKOUTPUT_ENABLE
   
    rcu_periph_clock_enable(RCU_BKPI);
    rcu_periph_clock_enable(RCU_PMU);
   
    pmu_backup_write_enable();

   
    bkp_tamper_detection_disable();

   
    bkp_rtc_calibration_output_enable();
#endif

   
    rcu_all_reset_flag_clear();

   
   

        while(1)
        {
                i++;
                if(i%50==0)
                {
                        led1=!led1;       
                        time_display();
                }
                delay_nms(20);
        }
}
八、结果


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

66

主题

1948

帖子

14

粉丝