RTC时钟测试。
一、硬件部分
二、程序部分
rtc.c
//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(×tamp); // 转换为本地时间
// 打印日期和时间
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);
}
}
int test_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, ×tamp);
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);
三、运行结果
读取RTC日期和时间,通过串口打印输出
3.1、设置时间
执行 date 2024 09 01 22 08 00,设置目前时间
3.2、运行例程
执行例程 test_rtc_sample,串口输出读取的日期和时间