32单片机RTC时间接续,掉电时间保存

本文讨论了在GD32单片机中确保RTC在掉电后时间功能仍能正常工作的方法,强调了区分首次启动和后续启动以决定RTC和MCU的配置,推荐使用RTC计数器值而非Flash标志位作为判断依据,并提供了相关的代码示例和时间处理函数。
摘要由CSDN通过智能技术生成

1、实现思路

前提:首先要实现RTC掉电之后时间还能继续走,RTC电池是必要的

说明:设备第一次启动需要初始化配置RTC,但当二次启动再重新配置RTC会导致RTC计数器置零,所以传统的程序流程是不行的,我们需要知道设备是第一次启动还是二次启动,来判断是否需要重新初始化配置RTC。另外RTC电池会给RTC功能部分供电不代表会给MCU供电,即使是二次启动不需要再初始化RTC,并不代表就不需要初始化MCU了,MCU的部分RTC相关的功能还是需要进行配置才能获取到RTC时间。

2、实现思路

方式一:

要区分是否是第一次启动,可能很多人第一个想法就是使用Flash,第一次启动在flash保存一个标志位,第二次启动就可以读取到标志位。

说明:实际并不推荐这种方式,如果将来RTC电池没电了,设备上电如果不初始化RTC,那么时间就不会走了,与时间相关的功能都将失效,甚至是设备死机。

方式二:

通过读取rtc计数器值判断,如果rtc没有被配置过那么rtc计数器值就是0,然后再判断要如何配置RTC及相关功能。

说明:推荐的方式,如果配置过rtc计数器开始工作,那么计数器值就不会是0,即使rtc电池没电也不过是时间无法保存,每次上电时间都会重置罢了。

方式三:

使用方式一和方式二结合的方式,通过在flash设置标志位和rtc计数器相结合的方式,第一次启动设置标志位,适用于设备带恢复出厂设置功能的需求,如果标志位未设置就初始化RTC,如果设置了再判断rtc计数器的值,这样如果我们需要重置设备,只需要清除标志位,就可以重置时间,当然也可以选择不使用flash保存标志位,直接将rtc计数器置零或其他默认值的方式也可以,根据自己需求进行选择。

代码样例:

以GD32单片机为例

RTC配置代码:

//MCU配置
void rtc_reconfig(void)
{
	/* enable PMU and BKPI clocks */
    rcu_periph_clock_enable(RCU_BKPI);
    rcu_periph_clock_enable(RCU_PMU);
    /* allow access to BKP domain */
    pmu_backup_write_enable();
    /* enable the RTC second interrupt*/
    rtc_interrupt_enable(RTC_INT_SECOND);
    rtc_interrupt_enable(RTC_INT_ALARM);
    /* wait until last write operation on RTC registers has finished */
    rtc_lwoff_wait();
	nvic_int_enable();
}
//RTC+MCU初始化
void rtc_configuration(void)
{
	time_t ltime;
    /* enable PMU and BKPI clocks */
    rcu_periph_clock_enable(RCU_BKPI);
    rcu_periph_clock_enable(RCU_PMU);
    /* allow access to BKP domain */
    pmu_backup_write_enable();

    /* reset backup domain */
    bkp_deinit();

    /* enable LXTAL */
    rcu_osci_on(RCU_LXTAL);
    /* wait till LXTAL is ready */
    rcu_osci_stab_wait(RCU_LXTAL);
    
    /* select RCU_LXTAL as RTC clock source */
    rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);

    /* enable RTC Clock */
    rcu_periph_clock_enable(RCU_RTC);

    /* wait for RTC registers synchronization */
    rtc_register_sync_wait();

    /* wait until last write operation on RTC registers has finished */
    rtc_lwoff_wait();

    /* enable the RTC second interrupt*/
    rtc_interrupt_enable(RTC_INT_SECOND);
    rtc_interrupt_enable(RTC_INT_ALARM);
    /* wait until last write operation on RTC registers has finished */
    rtc_lwoff_wait();

    /* set RTC prescaler: set RTC period to 1s */
    rtc_prescaler_set(32767);
	rtc_str2tm("2024/01/16 10:21:00",&ltime);
    /* wait until last write operation on RTC registers has finished */
    rtc_lwoff_wait();
    /* change the current time */
    rtc_counter_set(ltime);
    rtc_lwoff_wait();
    /* set the alarm time = currenttime + 10 second*/
    //rtc_alarm_config((time_time2val(global_datetime.time)+10)%0x00015180);
    /* wait until last write operation on RTC registers has finished */
    //rtc_lwoff_wait();
	nvic_int_enable();
}

实用的时间转换函数:

避免大家重复造轮子,直接贴出来供大家使用

//获取某年多少天
uint16_t rtc_getyday(uint16_t year)
{
	
	uint16_t max_day = 0;
	if(year%400==0){
		max_day = 366;
	}else if(year%4==0 && year%100!=0){
		max_day = 366;
	}else{
		max_day = 365;
	}
	return max_day;
}
//获取某年的某月有多少天
uint16_t rtc_getmday(uint16_t year,uint16_t mon)
{
	uint16_t max_day = 0;
	switch(mon){
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			max_day = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			max_day = 30;
			break;
		case 2:
			if(rtc_getyday(year)==366){
				max_day = 29;
			}else{
				max_day = 28;
			}
			break;
		default:
			return 0;
			break;
	}
}
//将字符时间转换为时间戳
int rtc_str2tm(const char * ctime,time_t * ltm)
{
	uint32_t totalday;
	time_t timeval=0;
	int i;
	struct tm tmptm = {0};
	*ltm = 0;
	sscanf(ctime,"%04u/%02u/%02u %02u:%02u:%02u",&tmptm.tm_year,&tmptm.tm_mon,&tmptm.tm_mday,&tmptm.tm_hour,&tmptm.tm_min,&tmptm.tm_sec);
	if(tmptm.tm_year>2100){
		return -1;
	}
	if(tmptm.tm_mon>12){
		return -1;
	}
	if(tmptm.tm_mday>rtc_getmday(tmptm.tm_year,tmptm.tm_mon)){
		return -1;
	}
	if(tmptm.tm_hour>=24){
		return -1;
	}
	if(tmptm.tm_min>=60){
		return -1;
	}
	if(tmptm.tm_sec>=60){
		return -1;
	}
	for(i=1970;i<tmptm.tm_year;i++){
		totalday = rtc_getyday(i);
		timeval += totalday*60*60*24;
	}
	for(i=1;i<tmptm.tm_mon;i++){
		totalday = rtc_getmday(tmptm.tm_year,i);
		timeval += totalday*60*60*24;
	}
	tmptm.tm_mday--;
	timeval += tmptm.tm_mday*60*60*24;
	timeval += tmptm.tm_hour*60*60;
	timeval += tmptm.tm_min*60;
	timeval += tmptm.tm_sec;
	
	*ltm = timeval;
	return 0;
}

C语言常用时间函数库说明:

需要包含头文件time.h

/*   time.h   */

typedef unsigned int time_t;     /* 时间戳 date/time in unix secs past 1-Jan-70 */

struct tm {
    int tm_sec;   /* 秒 seconds after the minute, 0 to 60
                     (0 - 60 allows for the occasional leap second) */
    int tm_min;   /* 分 minutes after the hour, 0 to 59 */
    int tm_hour;  /* 时 hours since midnight, 0 to 23 */
    int tm_mday;  /* 日 day of the month, 1 to 31 */
    int tm_mon;   /* 月 months since January, 0 to 11 */
    int tm_year;  /* 年 years since 1900 */
    int tm_wday;  /* 周几 days since Sunday, 0 to 6 */
    int tm_yday;  /* 一年的第几天 days since January 1, 0 to 365 */
    int tm_isdst; /* 夏令时 Daylight Savings Time flag */
};

time_t mktime(struct tm * /*timeptr*/) /* 时间结构体转时间戳  */

struct tm *localtime(const time_t * /*timer*/)  /* 时间戳转时间结构体  */

  • 9
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值