介紹
RTC (Real-Time Clock)實(shí)時(shí)時(shí)鐘可以提供精確的實(shí)時(shí)時(shí)間,它可以用于產(chǎn)生年、月、日、時(shí)、分、秒等信息。目前實(shí)時(shí)時(shí)鐘芯片大多采用精度較高的晶體振蕩器作為時(shí)鐘源。有些時(shí)鐘芯片為了在主電源掉電時(shí)還可以工作,會(huì)外加電池供電,使時(shí)間信息一直保持有效。
RT-Thread 的 RTC設(shè)備為操作系統(tǒng)的時(shí)間系統(tǒng)提供了基礎(chǔ)服務(wù)。面對(duì)越來越多的 IoT 場(chǎng)景,RTC 已經(jīng)成為產(chǎn)品的標(biāo)配,甚至在諸如 SSL 的安全傳輸過程中,RTC 已經(jīng)成為不可或缺的部分。
步驟
首先啟用軟件模擬 RTC:
然后我們?cè)?main.c 中加入如下內(nèi)容:
#define RTC_NAME "rtc"
#include
static int rtc_sample(int argc, char argv[])
{
rt_err_t ret = RT_EOK;
time_t now;
rt_device_t device = RT_NULL;
/ 尋找設(shè)備 /
device = rt_device_find(RTC_NAME);
if (!device)
{
LOG_E("find %s failed!", RTC_NAME);
return RT_ERROR;
}
/ 初始化RTC設(shè)備 /
if(rt_device_open(device, 0) != RT_EOK)
{
LOG_E("open %s failed!", RTC_NAME);
return RT_ERROR;
}
/ 設(shè)置日期 /
ret = set_date(2018, 12, 3);
if (ret != RT_EOK)
{
rt_kprintf("set RTC date failedn");
return ret;
}
/ 設(shè)置時(shí)間 /
ret = set_time(11, 15, 50);
if (ret != RT_EOK)
{
rt_kprintf("set RTC time failedn");
return ret;
}
/ 延時(shí)3秒 /
rt_thread_mdelay(3000);
/ 獲取時(shí)間 /
now = time(RT_NULL);
rt_kprintf("%sn", ctime(&now));
return ret;
}
/ 導(dǎo)出到 msh 命令列表中 */
MSH_CMD_EXPORT(rtc_sample, rtc sample);
代碼解讀:首先設(shè)置了年月日時(shí)分秒信息為2018年12月3日11:15:50,然后延時(shí) 3 秒后獲取當(dāng)前時(shí)間信息,應(yīng)當(dāng)顯示2018年12月3日11:15:53。
編譯下載運(yùn)行:
-
實(shí)時(shí)時(shí)鐘
+關(guān)注
關(guān)注
4文章
248瀏覽量
65990 -
晶體振蕩器
+關(guān)注
關(guān)注
9文章
633瀏覽量
29265 -
電池供電
+關(guān)注
關(guān)注
0文章
255瀏覽量
22267 -
RTC
+關(guān)注
關(guān)注
2文章
544瀏覽量
67076 -
ART-Pi
+關(guān)注
關(guān)注
0文章
23瀏覽量
1324
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論