行业知识
C++操作树莓派的RTC时钟
2026-11-27 22:00:17
来源:互联网转载
概述
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
throw std::system_error(errno,
std::system_category(),
"ioctl failed");
}
return mktime(&tm.tm);
}
测试代码
#include #include #include #include #include #include #include class Rtc {int fd;public:Rtc() {fd = open("/dev/rtc", O_RDWR);if (fd
上一篇:怎么更改手机服务器端口?