本帖最后由 jinglixixi 于 2020-11-26 08:28 編輯
GD32307E-START開發板是具備RTC計時功能的,但它的RTC似乎并不是真正的RTC,后面大家從程序中就會發現端倪。 將串行通訊功能和RTC計時功能相結合,能快速地驗證 RTC電子時鐘,見下圖所示。 此外,若為它配上一個OLED屏,那觀察起來就更方便了。
1.jpg (40.03 KB, 下載次數: 66)
下載附件
2020-11-26 00:58 上傳
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();
- }
復制代碼
|