【匠芯创D133CBS】 RTC测试
<p><span style="font-size:16px;">RTC时钟测试。</span></p><p> </p>
<p><span style="font-size:16px;"><strong>一、硬件部分</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">CPU内部集成RTC部分,需要外接晶振,电路部分如下:</span></p>
<p> </p>
<p> </p>
<p><span style="font-size:16px;"><strong>二、程序部分</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">rtc.c</span></p>
<pre>
<code>//rtc.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/time.h>
#include <rtthread.h>
#include "rtdevice.h"
#include <aic_core.h>
#include "hal_adcim.h"
#include "aic_log.h"
#include "hal_gpai.h"
#define AIC_RTC_NAME "rtc"
time_t timestamp;
static void rtc_read_hdl(void *parmeter)
{
while (1)
{
struct tm *tm_local = localtime(&timestamp); // 转换为本地时间
// 打印日期和时间
rt_kprintf("current date and time: %d-%02d-%02d %02d:%02d:%02d\n",
tm_local->tm_year + 1900, tm_local->tm_mon + 1, tm_local->tm_mday,
tm_local->tm_hour, tm_local->tm_min, tm_local->tm_sec);
rt_thread_mdelay(1000);
}
}
inttest_rtc_sample(void)
{
rt_thread_t rtc_thread;
rt_device_t rtc_dev = RT_NULL;
rt_err_t ret;
rtc_dev = rt_device_find(AIC_RTC_NAME);
if(rtc_dev == RT_NULL)
{
rt_kprintf("Failed to open %s device\n", AIC_RTC_NAME);
return -RT_ERROR;
}
ret = rt_device_control(rtc_dev, RT_DEVICE_CTRL_RTC_GET_TIME, &timestamp);
if(ret != RT_EOK)
{
rt_kprintf("get time fail\n");
return -RT_ERROR;
}
rtc_thread = rt_thread_create(
"test_rtc",
rtc_read_hdl,
"rtc",
2048,
RT_THREAD_PRIORITY_MAX/2,
25);
if(rtc_thread != RT_NULL)
{
rt_thread_startup(rtc_thread);
}
return RT_EOK;
}
MSH_CMD_EXPORT_ALIAS(test_rtc_sample, test_rtc_sample, rtc device sample);
</code></pre>
<p> </p>
<p><span style="font-size:16px;"><strong>三、运行结果</strong></span></p>
<p><span style="font-size:16px;">读取RTC日期和时间,通过串口打印输出</span></p>
<p> </p>
<p><span style="font-size:16px;">3.1、设置时间</span></p>
<p><span style="font-size:16px;">执行 date 2024 09 01 22 08 00,设置目前时间</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><span style="font-size:16px;">3.2、运行例程</span></p>
<p><span style="font-size:16px;">执行例程 test_rtc_sample,串口输出读取的日期和时间</span></p>
<p></p>
<p> </p>
页:
[1]