【GD32L233C-START测评】RTC电子时钟
[复制链接]
在GD32L233内部配有RTC计时器,用它来计时在相应函数的配合下可免除计时过程中所遇到的进制处理问题,
在使用串口通信的情况下,其执行效果如图1所示。
图1 运行效果
此外,若将RTC与OLED屏结合起来,则可以轻松地实现电子时钟的功能。
实现图1计时效果的主程序为:
int main(void)
{
int i;
systick_config();
gd_eval_com_init(EVAL_COM);
usart_transmit_config(EVAL_COM, USART_TRANSMIT_ENABLE);
rcu_periph_clock_enable(RCU_PMU);
rcu_periph_clock_enable(RCU_BKP);
pmu_backup_write_enable();
rtc_pre_config();
if(BKP_VALUE != RTC_BKP0) {
rtc_setup();
} else {
if(RESET != rcu_flag_get(RCU_FLAG_PORRST)) {
printf("power on reset occurred....\n\r");
} else if(RESET != rcu_flag_get(RCU_FLAG_EPRST)) {
printf("external reset occurred....\n\r");
}
printf("no need to configure RTC....\n\r");
rtc_show_time();
}
rcu_all_reset_flag_clear();
while(1) {
rtc_show_time();
delay_1ms(1000);
}
}
该程序的处理流程为:
实现进行RTC初始化,然后判别指定的识别值,若未设置初始计时值,则通过串口来读取初始计时值。否则,会在已有的计时基础上进行计时处理。
在设置初始值时,除年份之外其它值均要输出2个字符。在完成日期与时间的设置后,便自动进入到计时状态,见图2所示。
图2 设置初始值
相应的时间设置函数为:
void rtc_setup(void)
{
uint32_t tmp_year = 0xFF, tmp_month = 0xFF, tmp_day = 0xFF;
uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
rtc_initpara.factor_asyn = prescaler_a;
rtc_initpara.factor_syn = prescaler_s;
rtc_initpara.year = 0x16;
rtc_initpara.day_of_week = RTC_SATURDAY;
rtc_initpara.month = RTC_APR;
rtc_initpara.date = 0x30;
rtc_initpara.display_format = RTC_24HOUR;
rtc_initpara.am_pm = RTC_AM;
printf("=======Configure RTC Time========\n\r");
printf(" please set the last two digits of current year:\n\r");
while(tmp_year == 0xFF) {
tmp_year = usart_input_threshold(99);
rtc_initpara.year = tmp_year;
}
printf(" 20%0.2x\n\r", tmp_year);
printf(" please input month:\n\r");
while(tmp_month == 0xFF) {
tmp_month = usart_input_threshold(12);
rtc_initpara.month = tmp_month;
}
printf(" %0.2x\n\r", tmp_month);
printf(" please input day:\n\r");
while(tmp_day == 0xFF) {
tmp_day = usart_input_threshold(31);
rtc_initpara.date = tmp_day;
}
printf(" %0.2x\n\r", tmp_day);
printf(" please input hour:\n\r");
while(tmp_hh == 0xFF) {
tmp_hh = usart_input_threshold(23);
rtc_initpara.hour = tmp_hh;
}
printf(" %0.2x\n\r", tmp_hh);
printf(" please input minute:\n\r");
while(tmp_mm == 0xFF) {
tmp_mm = usart_input_threshold(59);
rtc_initpara.minute = tmp_mm;
}
printf(" %0.2x\n\r", tmp_mm);
printf(" please input second:\n\r");
while(tmp_ss == 0xFF) {
tmp_ss = usart_input_threshold(59);
rtc_initpara.second = tmp_ss;
}
printf(" %0.2x\n\r", tmp_ss);
if(ERROR == rtc_init(&rtc_initpara)) {
printf("\n\r** RTC time configuration failed! **\n\r");
} else {
printf("\n\r** RTC time configuration success! **\n\r");
rtc_show_time();
RTC_BKP0 = BKP_VALUE;
}
}
使用串口实现计时值显示的函数为:
void rtc_show_time(void)
{
rtc_current_time_get(&rtc_initpara);
printf("\r\nCurrent time: 20%0.2x-%0.2x-%0.2x", \
rtc_initpara.year, rtc_initpara.month, rtc_initpara.date);
printf(" : %0.2x:%0.2x:%0.2x \r\n", \
rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second);
}
在该程序的基础上,若要真正实现一个独立的RTC电子时钟,可将其以串口进行时间值的设置的操作改为用按键来方式进行设置。
|