RTC是常用外设之一,这里展示用RT-Thread Studio 定配置与基本应用:
RT-Thread Setinngs
使用RTC
保存后退出
创建示例代码
在appliecation目录下新建rtc_sample.c,并输入如下代码:
#include <rtthread.h>
#include <rtdevice.h>
#define RTC_NAME "rtc"
static int rtc_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
time_t now;
rt_device_t device = RT_NULL;
device = rt_device_find(RTC_NAME);
if(!device)
{
rt_kprintf("find %s failed!", RTC_NAME);
return RT_ERROR;
}
if(rt_device_open(device, 0) != RT_EOK)
{
rt_kprintf("open %s failed!", RTC_NAME);
return RT_ERROR;
}
ret = set_date(2023, 2, 13);
if (ret != RT_EOK) {
rt_kprintf("set RTC date failed\n");
return ret;
}
ret = set_time(16, 7, 13);
if (ret != RT_EOK) {
rt_kprintf("set RTC time failed\n");
return ret;
}
now = time(RT_NULL);
rt_kprintf("%s\n",ctime(&now));
rt_thread_mdelay(10000);
now = time(RT_NULL);
rt_kprintf("%s\n",ctime(&now));
return ret;
}
MSH_CMD_EXPORT(rtc_sample, rtc sample);
程序的功能是初始化RTC 并且设置日期与时间,读取时间一次,延时10秒再读取一次,验证程序是否对。
测试
编译代码后下载到开发,打终串口终端,按help:
rtc_sample出现在命令列表中。
输入rtc_sample:
看到时间是对的。
|