注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
Justice_Gao的个人空间 https://home.eeworld.com.cn/space-uid-784175.html [收藏] [复制] [分享] [RSS]
日志

【GD32F350开发分享八】RTC年月日时分秒实时时钟,并显示在TFT屏幕上

已有 2678 次阅读2018-10-11 15:49 |个人分类:GD32系列学习


为了得到更加精确的RTC时钟,我采用了LXTAL外部晶振,自己动手焊接了32.768KHz和匹配电容。

时钟选择如下:


  1. #define RTC_CLOCK_SOURCE_LXTAL
  2. #define BKP_VALUE    0x32F0

  3. rtc_timestamp_struct rtc_timestamp;
  4. rtc_parameter_struct rtc_initpara;
  5. __IO uint32_t prescaler_a = 0, prescaler_s = 0;

  6. void rtc_setup(void);
  7. void rtc_show_time(void);
  8. void rtc_show_timestamp(void);
  9. uint8_t usart_input_threshold(uint32_t value);
  10. bool RTC_Timer_Set_Flag=0;
  11. void rtc_pre_config(void);


  12. /*!
  13.     \brief      RTC configuration function
  14.     \param[in]  none
  15.     \param[out] none
  16.     \retval     none
  17. */
  18. void rtc_pre_config(void)
  19. {
  20.     #if defined (RTC_CLOCK_SOURCE_IRC40K)
  21.           rcu_osci_on(RCU_IRC40K);
  22.           rcu_osci_stab_wait(RCU_IRC40K);
  23.           rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
  24.   
  25.           prescaler_s = 0x18F;
  26.           prescaler_a = 0x63;
  27.     #elif defined (RTC_CLOCK_SOURCE_LXTAL)
  28.           rcu_osci_on(RCU_LXTAL);
  29.           rcu_osci_stab_wait(RCU_LXTAL);
  30.           rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
  31.           prescaler_s = 0xFF;
  32.           prescaler_a = 0x7F;
  33.     #else
  34.     #error RTC clock source should be defined.
  35.     #endif /* RTC_CLOCK_SOURCE_IRC40K */

  36.     rcu_periph_clock_enable(RCU_RTC);
  37.     rtc_register_sync_wait();
  38. }
复制代码

然后通过串口可以设置初始化年月日时分秒,如下图所示


设置当下时间为2018-10-11 15:22:00,设置时间的代码如下:

  1. void rtc_setup(void)
  2. {
  3.     /* setup RTC time value */
  4.     uint32_t tmp_year=0xFF,tmp_month=0xFF,tmp_date=0xFF,tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;

  5.     rtc_initpara.rtc_factor_asyn = prescaler_a;
  6.     rtc_initpara.rtc_factor_syn = prescaler_s;
  7.     //rtc_initpara.rtc_year = 0x18;
  8.     rtc_initpara.rtc_day_of_week = RTC_SATURDAY;
  9.     //rtc_initpara.rtc_month = RTC_APR;
  10.     //rtc_initpara.rtc_date = 0x30;
  11.     rtc_initpara.rtc_display_format = RTC_24HOUR;
  12.     rtc_initpara.rtc_am_pm = RTC_AM;
  13.                 RTC_Timer_Set_Flag = 0;
  14.     /* current time input */
  15.     USART1_SendBuffer("=======Configure RTC Time========\n\r",sizeof("=======Configure RTC Time========\n\r"));
  16.    
  17.                 USART1_SendBuffer("\n\r  please input year:\n\r",sizeof("  please input year:\n\r"));
  18.                 while (tmp_year == 0xFF){   
  19.         tmp_year = usart_input_threshold(99);
  20.         rtc_initpara.rtc_year = tmp_year;
  21.     }
  22.                 usart_data_transmit(EVAL_COM1, tmp_year/16+0x30);
  23.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  24.                 usart_data_transmit(EVAL_COM1, tmp_year%16+0x30);
  25.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  26.                 USART1_SendBuffer("\n\r  please input month:\n\r",sizeof("  please input month:\n\r"));
  27.                 while (tmp_month == 0xFF){   
  28.         tmp_month = usart_input_threshold(12);
  29.         rtc_initpara.rtc_month = tmp_month;
  30.     }
  31.                 usart_data_transmit(EVAL_COM1, tmp_month/16+0x30);
  32.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  33.                 usart_data_transmit(EVAL_COM1, tmp_month%16+0x30);
  34.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  35.                 USART1_SendBuffer("\n\r  please input date:\n\r",sizeof("  please input date:\n\r"));
  36.                 while (tmp_date == 0xFF){   
  37.         tmp_date = usart_input_threshold(31);
  38.         rtc_initpara.rtc_date = tmp_date;
  39.     }       
  40.                 usart_data_transmit(EVAL_COM1, tmp_date/16+0x30);
  41.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  42.                 usart_data_transmit(EVAL_COM1, tmp_date%16+0x30);
  43.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  44.                 USART1_SendBuffer("\n\r  please input hour:\n\r",sizeof("  please input hour:\n\r"));
  45.     while (tmp_hh == 0xFF){   
  46.         tmp_hh = usart_input_threshold(23);
  47.         rtc_initpara.rtc_hour = tmp_hh;
  48.     }
  49.                 usart_data_transmit(EVAL_COM1, tmp_hh/16+0x30);
  50.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  51.                 usart_data_transmit(EVAL_COM1, tmp_hh%16+0x30);
  52.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  53.                 USART1_SendBuffer("\n\r  please input minute:\n\r",sizeof("  please input minute:\n\r"));
  54.     while (tmp_mm == 0xFF){   
  55.         tmp_mm = usart_input_threshold(59);
  56.         rtc_initpara.rtc_minute = tmp_mm;
  57.     }
  58.                 usart_data_transmit(EVAL_COM1, tmp_mm/16+0x30);
  59.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  60.                 usart_data_transmit(EVAL_COM1, tmp_mm%16+0x30);
  61.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  62.                 USART1_SendBuffer("\n\r  please input second:\n\r",sizeof("  please input second:\n\r"));
  63.     while (tmp_ss == 0xFF){
  64.         tmp_ss = usart_input_threshold(59);
  65.         rtc_initpara.rtc_second = tmp_ss;
  66.     }
  67.                 usart_data_transmit(EVAL_COM1, tmp_ss/16+0x30);
  68.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
  69.                 usart_data_transmit(EVAL_COM1, tmp_ss%16+0x30);
  70.                 while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));

  71.     /* RTC current time configuration */
  72.     if(ERROR == rtc_init(&rtc_initpara)){   
  73.         USART1_SendBuffer("\n\r** RTC time configuration failed! **\n\r",sizeof("\n\r** RTC time configuration failed! **\n\r"));
  74.     }else{
  75.         USART1_SendBuffer("\n\r** RTC time configuration success! **\n\r",sizeof("\n\r** RTC time configuration success! **\n\r"));
  76.         rtc_show_time();
  77.         RTC_BKP0 = BKP_VALUE;
  78.                                 RTC_Timer_Set_Flag = 1;
  79.     }   
  80. }
复制代码



最后,在TFT上面显示实时时钟,结果如下图,



TFT液晶显示屏的驱动可以参考我的另外一个帖子


液晶显示屏最下面是个滚动字幕,每隔1秒刷新实时时钟。

下面给个视频:




本文来自论坛,点击查看完整帖子内容。

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章