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

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*/)  /* 时间戳转时间结构体  */

相关推荐

  1. 【ESP32 IDF】RTC时钟

    2024-01-17 06:36:03       42 阅读
  2. 时光流转:探秘STM32实时时钟RTC

    2024-01-17 06:36:03       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-17 06:36:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-17 06:36:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-17 06:36:03       82 阅读
  4. Python语言-面向对象

    2024-01-17 06:36:03       91 阅读

热门阅读

  1. 开源世界许可证Copyleft GPL LGPL MIT BSD Apache

    2024-01-17 06:36:03       65 阅读
  2. PTA——7-4 奇葩楼层 (15 分)

    2024-01-17 06:36:03       59 阅读
  3. [leetcode~数位动态规划] 2719. 统计整数数目 hard

    2024-01-17 06:36:03       53 阅读
  4. Vue和小程序的区别

    2024-01-17 06:36:03       55 阅读
  5. RPC原理介绍与使用(@RpcServiceAnnotation)

    2024-01-17 06:36:03       50 阅读
  6. SSL VPN简介

    2024-01-17 06:36:03       57 阅读