3109 views|0 replies

57

Posts

0

Resources
The OP
 

[liklon plays with GD32F350] 5. RTC clock [Copy link]

In the subsequent development, a calendar is needed, so debug the RTC of GD32. According to the actual board, these two crystal oscillators are not soldered, so the RTC clock source can only be the internal 40K There is also such a paragraph in the manual: In summary, the following code is completed:
  1. 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();
复制代码
Further initialize the RTC:
  1. 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); }
复制代码
where calendar_info_t It is a custom structure, and the DEC2BCD and BCD2DEC conversions are as follows:
  1. #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;
复制代码
See the comments of the structure below. The BCD encoding needs to be converted
  1. 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;
复制代码
There are two frequency division parameters in the above initialization code: The next step is to write a function to read the time:
  1. 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); }
复制代码
Test in main function:
  1. 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; } }
复制代码
Test result: The difference is about 110ms in 1s
This post is from GD32 MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list