hehung 发表于 2023-8-19 23:30:07

【瑞萨RA MCU创意氛围赛】5. RTC实时时钟应用


# 前言

本文讲解如何使用启明RA6M5芯片内部RTC模块实现实时时钟。

RTC是实时时钟模块,设置了初始时间之后会自动累计时间,并且精度很高。

# 1 RASC 配置

配置如下,使能RTC的Realtime Clock Stack。



详细配置如下,可以不用配置回调函数,本例中实际没有使用到。



# 2 代码实现

代码实现如下:

初始化的时候调用 `Rtc_Init`即可,设定初始时间为:2023年7月15日12点三十分0秒。

然后每一秒钟调用一次 `Rtc_GetDateTime`。用来检测RTC累计时间是否正确。

/*
@hehung
2023-2-8
转载请注明出处,版权由@hehung所有
email: 1398660197@qq.com
wechat: hehung95
*/

#include "hal_data.h"
#include "app_rtc.h"


#define RTC_DEBUG
#undef RTC_DEBUG

#ifdef RTC_DEBUG
#include <stdio.h>
#endif

#define RTC_ALARM_EN      (COMM_OFF)


/* rtc_time_t is an alias for the C Standard time.h struct 'tm' */
rtc_time_t set_time =
{
    .tm_sec= 0,      /* 秒,范围从 0 到 59 */
    .tm_min= 30,      /* 分,范围从 0 到 59 */
    .tm_hour = 12,      /* 小时,范围从 0 到 23*/
    .tm_mday = 19,       /* 一月中的第几天,范围从 1 到 31*/
    .tm_mon= 1,      /* 月份,范围从 0 到 11*/
    .tm_year = 123,   /* 自 1900 起的年数,2021为121*/
    .tm_wday = 0,       /* 一周中的第几天,范围从 0 到 6*/
//    .tm_yday=0,         /* 一年中的第几天,范围从 0 到 365*/
//    .tm_isdst=0;      /* 夏令时*/
};

#if (RTC_ALARM_EN == COMM_ON)
rtc_alarm_time_t set_alarm_time=
{
   .time.tm_sec=   5,
   .time.tm_sec= 5,      /* 秒,范围从 0 到 59 */
   .time.tm_min= 30,      /* 分,范围从 0 到 59 */
   .time.tm_hour = 12,      /* 小时,范围从 0 到 23*/
   .time.tm_mday = 20,       /* 一月中的第几天,范围从 1 到 31*/
   .time.tm_mon= 11,      /* 月份,范围从 0 到 11*/
   .time.tm_year = 121,   /* 自 1900 起的年数,2021为121*/
   .time.tm_wday = 5,       /* 一周中的第几天,范围从 0 到 6*/

   .sec_match      =1,
   .min_match      =0,
   .hour_match       =0,
   .mday_match       =0,
   .mon_match      =0,
   .year_match       =0,
   .dayofweek_match=0,
};
#endif

// notification of rtc
void rtc_notification(rtc_callback_args_t *p_args)
{
    /* TODO: add your own code here */
    (void)p_args;
    // if(p_args->event == RTC_EVENT_PERIODIC_IRQ)
    //   rtc_flag=1;
    // else if(p_args->event == RTC_EVENT_ALARM_IRQ)
    //   rtc_alarm_flag=1;
}


void Rtc_Init(void)
{
    fsp_err_t err = FSP_SUCCESS;

    /* Initialize the RTC module*/
    err = R_RTC_Open(&g_rtc0_ctrl, &g_rtc0_cfg);
    /* Handle any errors. This function should be defined by the user. */
    assert(FSP_SUCCESS == err);
    /* R_RTC_CalendarTimeSet must be called at least once to start the RTC */
    R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
    /* Set the periodic interrupt rate to 1 second */
    R_RTC_PeriodicIrqRateSet(&g_rtc0_ctrl, RTC_PERIODIC_IRQ_SELECT_1_SECOND);

#if (RTC_ALARM_EN == COMM_ON)
    /* Set alarm time */
    R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_alarm_time);
#endif
}

// set date-time
void Rtc_SetDateTime(rtc_time_t * const p_time)
{
    R_RTC_CalendarTimeSet(&g_rtc0_ctrl, p_time);
}

// get date-time
rtc_time_t Rtc_GetDateTime(void)
{
    rtc_time_t get_time;

    // get rtc time
    (void)R_RTC_CalendarTimeGet(&g_rtc0_ctrl, &get_time);

#ifdef RTC_DEBUG
    printf("RTC: %d-%d-%d, %d:%d:%d, %dw\n", get_time.tm_year + 1900,
                                           get_time.tm_mon,
                                           get_time.tm_mday,
                                           get_time.tm_hour,
                                           get_time.tm_min,
                                           get_time.tm_sec,
                                           get_time.tm_wday);
#endif

    return get_time;
}



# 3 试验效果

如下,串口打印的时候添加上时间戳功能,可以检测处打印的RTC反馈的时间是否正确,如下图所示,试验成功。





页: [1]
查看完整版本: 【瑞萨RA MCU创意氛围赛】5. RTC实时时钟应用