一、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();
- }
六、显示时间
|