This post was last edited by jinglixixi on 2020-11-26 21:02
The GD32307E-START development board has the RTC timing function, but its RTC does not seem to be a real RTC. You will find the clue from the program later.
Combining the serial communication function with the RTC timing function can quickly verify the RTC electronic clock, as shown in the figure below.
In addition, if it is equipped with an OLED screen, it will be more convenient to observe.
RTC electronic clock
So how do we achieve the above functions?
It involves several key functions:
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;
}
}
We have already given you a sneak peek before. You see, its RTC does not have dedicated registers to store the time values of hours, minutes, and seconds. Instead, they are calculated through the timevar variable.
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));
}
The main program to realize the RTC clock display function is:
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();
}
|