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