在后续的开发中还需要一个日历,那么就调试一下GD32的RTC。
根据实际的板子看,这两个晶振是都没有焊的,所以RTC时钟源也只能是选择内部的40K
在手册中还有这么一段话:
综上两点完成如下代码:
- rcu_periph_clock_enable(RCU_PMU);
- pmu_backup_write_enable();
-
- rcu_osci_on(RCU_IRC40K);
- rcu_osci_stab_wait(RCU_IRC40K);
- rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
- rcu_periph_clock_enable(RCU_RTC);
- rtc_register_sync_wait();
复制代码
进一步对RTC进行初始化:
- void calendar_init(calendar_info_t info)
- {
- rtc_parameter_struct rtc_parameter =
- {
- .rtc_factor_asyn = 0x4e - 1,
- .rtc_factor_syn = 0x200 - 1,
- .rtc_display_format = RTC_24HOUR,
- };
- rtc_parameter.rtc_year = DEC2BCD(info.year);
- rtc_parameter.rtc_month = info.month;
- rtc_parameter.rtc_date = DEC2BCD(info.day);
- rtc_parameter.rtc_day_of_week = info.week;
- rtc_parameter.rtc_hour = info.hour;
- rtc_parameter.rtc_minute = DEC2BCD(info.min);
- rtc_parameter.rtc_second = DEC2BCD(info.second);
- rtc_init(&rtc_parameter);
- }
复制代码
其中calendar_info_t 是自定义的结构体,以及DEC2BCD 和BCD2DEC转换如下:
- #define BCD2DEC(n) ((n >> 4) * 10 + (n & 0xf))
- #define DEC2BCD(n) (((n / 10) << 4) + (n % 10))
- typedef struct
- {
- uint8_t year;
- uint8_t month;
- uint8_t day;
- uint8_t week;
- uint8_t hour;
- uint8_t min;
- uint8_t second;
- }calendar_info_t;
复制代码
看下面这个结构体的注释,提到BCD编码的则需要转换
- typedef struct
- {
- uint8_t rtc_year; /*!< RTC year value: 0x0 - 0x99(BCD format) */
- uint8_t rtc_month; /*!< RTC month value */
- uint8_t rtc_date; /*!< RTC date value: 0x1 - 0x31(BCD format) */
- uint8_t rtc_day_of_week; /*!< RTC weekday value */
- uint8_t rtc_hour; /*!< RTC hour value */
- uint8_t rtc_minute; /*!< RTC minute value: 0x0 - 0x59(BCD format) */
- uint8_t rtc_second; /*!< RTC second value: 0x0 - 0x59(BCD format) */
- uint16_t rtc_factor_asyn; /*!< RTC asynchronous prescaler value: 0x0 - 0x7F */
- uint16_t rtc_factor_syn; /*!< RTC synchronous prescaler value: 0x0 - 0x7FFF */
- uint32_t rtc_am_pm; /*!< RTC AM/PM value */
- uint32_t rtc_display_format; /*!< RTC time notation */
- }rtc_parameter_struct;
复制代码
在上述的初始化代码中有两个分频参数:
接下来是编写读取时间的函数:
- void calendar_get_date(calendar_info_t *pinfo)
- {
- rtc_parameter_struct rtc_parameter;
- rtc_current_time_get(&rtc_parameter);
- pinfo->year = BCD2DEC(rtc_parameter.rtc_year);
- pinfo->month = rtc_parameter.rtc_month;
- pinfo->day = BCD2DEC(rtc_parameter.rtc_date);
- pinfo->week = rtc_parameter.rtc_day_of_week;
- pinfo->hour = rtc_parameter.rtc_hour;
- pinfo->min = BCD2DEC(rtc_parameter.rtc_minute);
- pinfo->second = BCD2DEC(rtc_parameter.rtc_second);
- }
复制代码
在main函数中测试:
- calendar_info.year = 18;
- calendar_info.month = 9;
- calendar_info.day = 5;
- calendar_info.week = 3;
- calendar_info.hour = 15;
- calendar_info.min = 26;
- calendar_info.second = 22;
- calendar_init(calendar_info);
- uart1_write((uint8_t *)"hello\n\r", 7);
- while(1)
- {
- calendar_get_date(&calendar_info);
-
- if(calendar_info.second != second)
- {
- sprintf((char *)tmp, "%2d-%2d-%2d %2d:%2d:%2d %2d\n\r",
- calendar_info.year, calendar_info.month, calendar_info.day, calendar_info.hour, calendar_info.min, calendar_info.second, calendar_info.week);
- uart1_write(tmp, strlen((char *)tmp));
- second = calendar_info.second;
- }
- }
复制代码
测试结果:
大概1s中相差110ms左右
本文来自论坛,点击查看完整帖子内容。