概述
RTC实时时钟通常是指一个集成电路,RTC本质上是一个独立的定时器,通常情况下需要外接一个32.768KHZ的晶振和匹配电容(10~33pf),由于时间是不停止的,为了满足这一要求,所以RTC实时时钟有两种供电方式:
1)在设备正常运行的时候,RTC实时时钟模块是由MCU主电源进行供电。
2)在主电源停止供电的时候,RTC实时时钟由备份电源(纽扣电池)来进行供电,保证当MCU停止供电的情况下,RTC不受影响,保持正常工作。
核心代码
time_t GetTime(void) {
union {
struct rtc_time rtc;
struct tm tm;
} tm;
int ret = ioctl(fd, RTC_RD_TIME, &tm.rtc);
if (ret < 0) {
throw std::system_error(errno,
std::system_category(),
"ioctl failed");
}
return mktime(&tm.tm);
}
测试代码
#include <iostream>
#include <system_error>
#include <time.h>
#inclu