本帖最后由 jinglixixi 于 2020-11-26 08:28 编辑
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());
- }
- }
- }
复制代码 实现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();
- }
复制代码
|