为了得到更加精确的RTC时钟,我采用了LXTAL外部晶振,自己动手焊接了32.768KHz和匹配电容。
时钟选择如下:
- #define RTC_CLOCK_SOURCE_LXTAL
- #define BKP_VALUE 0x32F0
- rtc_timestamp_struct rtc_timestamp;
- rtc_parameter_struct rtc_initpara;
- __IO uint32_t prescaler_a = 0, prescaler_s = 0;
- void rtc_setup(void);
- void rtc_show_time(void);
- void rtc_show_timestamp(void);
- uint8_t usart_input_threshold(uint32_t value);
- bool RTC_Timer_Set_Flag=0;
- void rtc_pre_config(void);
- /*!
- \brief RTC configuration function
- \param[in] none
- \param[out] none
- \retval none
- */
- void rtc_pre_config(void)
- {
- #if defined (RTC_CLOCK_SOURCE_IRC40K)
- rcu_osci_on(RCU_IRC40K);
- rcu_osci_stab_wait(RCU_IRC40K);
- rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
-
- prescaler_s = 0x18F;
- prescaler_a = 0x63;
- #elif defined (RTC_CLOCK_SOURCE_LXTAL)
- rcu_osci_on(RCU_LXTAL);
- rcu_osci_stab_wait(RCU_LXTAL);
- rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
- prescaler_s = 0xFF;
- prescaler_a = 0x7F;
- #else
- #error RTC clock source should be defined.
- #endif /* RTC_CLOCK_SOURCE_IRC40K */
- rcu_periph_clock_enable(RCU_RTC);
- rtc_register_sync_wait();
- }
复制代码
然后通过串口可以设置初始化年月日时分秒,如下图所示
设置当下时间为2018-10-11 15:22:00,设置时间的代码如下:
- void rtc_setup(void)
- {
- /* setup RTC time value */
- uint32_t tmp_year=0xFF,tmp_month=0xFF,tmp_date=0xFF,tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
- rtc_initpara.rtc_factor_asyn = prescaler_a;
- rtc_initpara.rtc_factor_syn = prescaler_s;
- //rtc_initpara.rtc_year = 0x18;
- rtc_initpara.rtc_day_of_week = RTC_SATURDAY;
- //rtc_initpara.rtc_month = RTC_APR;
- //rtc_initpara.rtc_date = 0x30;
- rtc_initpara.rtc_display_format = RTC_24HOUR;
- rtc_initpara.rtc_am_pm = RTC_AM;
- RTC_Timer_Set_Flag = 0;
- /* current time input */
- USART1_SendBuffer("=======Configure RTC Time========\n\r",sizeof("=======Configure RTC Time========\n\r"));
-
- USART1_SendBuffer("\n\r please input year:\n\r",sizeof(" please input year:\n\r"));
- while (tmp_year == 0xFF){
- tmp_year = usart_input_threshold(99);
- rtc_initpara.rtc_year = tmp_year;
- }
- usart_data_transmit(EVAL_COM1, tmp_year/16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- usart_data_transmit(EVAL_COM1, tmp_year%16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- USART1_SendBuffer("\n\r please input month:\n\r",sizeof(" please input month:\n\r"));
- while (tmp_month == 0xFF){
- tmp_month = usart_input_threshold(12);
- rtc_initpara.rtc_month = tmp_month;
- }
- usart_data_transmit(EVAL_COM1, tmp_month/16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- usart_data_transmit(EVAL_COM1, tmp_month%16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- USART1_SendBuffer("\n\r please input date:\n\r",sizeof(" please input date:\n\r"));
- while (tmp_date == 0xFF){
- tmp_date = usart_input_threshold(31);
- rtc_initpara.rtc_date = tmp_date;
- }
- usart_data_transmit(EVAL_COM1, tmp_date/16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- usart_data_transmit(EVAL_COM1, tmp_date%16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- USART1_SendBuffer("\n\r please input hour:\n\r",sizeof(" please input hour:\n\r"));
- while (tmp_hh == 0xFF){
- tmp_hh = usart_input_threshold(23);
- rtc_initpara.rtc_hour = tmp_hh;
- }
- usart_data_transmit(EVAL_COM1, tmp_hh/16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- usart_data_transmit(EVAL_COM1, tmp_hh%16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- USART1_SendBuffer("\n\r please input minute:\n\r",sizeof(" please input minute:\n\r"));
- while (tmp_mm == 0xFF){
- tmp_mm = usart_input_threshold(59);
- rtc_initpara.rtc_minute = tmp_mm;
- }
- usart_data_transmit(EVAL_COM1, tmp_mm/16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- usart_data_transmit(EVAL_COM1, tmp_mm%16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- USART1_SendBuffer("\n\r please input second:\n\r",sizeof(" please input second:\n\r"));
- while (tmp_ss == 0xFF){
- tmp_ss = usart_input_threshold(59);
- rtc_initpara.rtc_second = tmp_ss;
- }
- usart_data_transmit(EVAL_COM1, tmp_ss/16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- usart_data_transmit(EVAL_COM1, tmp_ss%16+0x30);
- while(RESET == usart_flag_get(EVAL_COM1, USART_FLAG_TBE));
- /* RTC current time configuration */
- if(ERROR == rtc_init(&rtc_initpara)){
- USART1_SendBuffer("\n\r** RTC time configuration failed! **\n\r",sizeof("\n\r** RTC time configuration failed! **\n\r"));
- }else{
- USART1_SendBuffer("\n\r** RTC time configuration success! **\n\r",sizeof("\n\r** RTC time configuration success! **\n\r"));
- rtc_show_time();
- RTC_BKP0 = BKP_VALUE;
- RTC_Timer_Set_Flag = 1;
- }
- }
复制代码
最后,在TFT上面显示实时时钟,结果如下图,
TFT液晶显示屏的驱动可以参考我的另外一个帖子
液晶显示屏最下面是个滚动字幕,每隔1秒刷新实时时钟。
下面给个视频:
本文来自论坛,点击查看完整帖子内容。