rtc是linux系统中的一个时间设备,可以open打开,通过ioctl设置频率,然后就可以进行循环read操作,每次read的耗时是(1/频率 单位:秒)
先上代码
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/rtc.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#define FREQ 2048
#define USEC_PER_SECOND 1000000
typedef int MILLSEC;
int g_fd = 0;
int calc_cnt(MILLSEC millseconds )
{
return (int)(millseconds * 1000.0 / USEC_PER_SECOND * FREQ + 0.5); //add 0.5 to meet precision in common
}
void rtc_open()
{
g_fd = open ("/dev/rtc", O_RDONLY);
if(g_fd < 0)
{
perror("open");
exit(errno);
}
printf("opened.\n");
}
void set_freq()
{
/*Set the freq*/
if(ioctl(g_fd, RTC_IRQP_SET, FREQ ) < 0)
{
perror("ioctl(RTC_IRQP_SET)");
close(g_fd);

本文介绍了如何在Linux系统下利用RTC(Real Time Clock)设备创建精确定时器。通过打开RTC设备,设置频率,并进行循环读取操作,每次read间隔时间与设置的频率成倒数关系。
最低0.47元/天 解锁文章
569

被折叠的 条评论
为什么被折叠?



