【GD32307E-START】+ RTC电子时钟
[复制链接]
本帖最后由 jinglixixi 于 2020-11-26 21:02 编辑
GD32307E-START开发板是具备RTC计时功能的,但它的RTC似乎并不是真正的RTC,后面大家从程序中就会发现端倪。
将串行通讯功能和RTC计时功能相结合,能快速地验证 RTC电子时钟,见下图所示。
此外,若为它配上一个OLED屏,那观察起来就更方便了。
RTC电子时钟
那该如何实现上面的功能的呢?
它会涉及到几个关键函数:
void time_display(uint32_t timevar)
{
uint32_t thh = 0, tmm = 0, tss = 0;
if(timevarp!=timevar)
{
/* compute hours */
thh = timevar / 3600;
/* compute minutes */
tmm = (timevar % 3600) / 60;
/* compute seconds */
tss = (timevar % 3600) % 60;
printf(" Time: %0.2d:%0.2d:%0.2d\r\n", thh, tmm, tss);
timevarp=timevar;
}
}
前面我们已经卖过关子了,你看它的RTC是没有专门的寄存器来存放时、分、秒的时间值,而是通过timevar变量来解算出的。
void time_show(void)
{
printf("\n\r");
timedisplay = 1;
/* infinite loop */
while (1)
{
/* if 1s has paased */
if (timedisplay == 1)
{
/* display current time */
time_display(rtc_counter_get());
}
}
}
uint32_t time_regulate(void)
{
uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
printf("\r\n==============Time Settings=====================================");
/*printf("\r\n Please Set Hours");
while (tmp_hh == 0xFF)
{
tmp_hh = usart_scanf(23);
}
printf(": %d", tmp_hh);
printf("\r\n Please Set Minutes");
while (tmp_mm == 0xFF)
{
tmp_mm = usart_scanf(59);
}
printf(": %d", tmp_mm);
printf("\r\n Please Set Seconds");
while (tmp_ss == 0xFF)
{
tmp_ss = usart_scanf(59);
}
printf(": %d", tmp_ss);*/
tmp_hh = 23;
tmp_mm = 59;
tmp_ss = 59;
printf(": %d", tmp_hh);
printf(": %d", tmp_mm);
printf(": %d", tmp_ss);
/* return the value store in RTC counter register */
return((tmp_hh*3600 + tmp_mm*60 + tmp_ss));
}
实现RTC时钟显示功能的主程序为:
int main(void)
{
/* configure systick */
systick_config();
/* configure EVAL_COM1 */
gd_eval_com_init(EVAL_COM1);
/* NVIC config */
nvic_configuration();
printf( "\r\nThis is a RTC demo...... \r\n" );
if (bkp_read_data(BKP_DATA_0) != 0xA5A5)
{
// backup data register value is not correct or not yet programmed
//(when the first time the program is executed)
printf("\r\nThis is a RTC demo!\r\n");
printf("\r\n\n RTC not yet configured....");
// RTC configuration
rtc_configuration();
printf("\r\n RTC configured....");
// adjust time by values entred by the user on the hyperterminal
time_adjust();
bkp_write_data(BKP_DATA_0, 0xA5A5);
}
// display time in infinite loop
time_show();
}
|