野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 552|回复: 0

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

[复制链接]
发表于 2023-8-19 23:30:07 | 显示全部楼层 |阅读模式

# 前言

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

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

# 1 RASC 配置

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

野火论坛202308192329314990..png

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

野火论坛202308192329382749..png

# 2 代码实现

代码实现如下:

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

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

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

  8. #include "hal_data.h"
  9. #include "app_rtc.h"


  10. #define RTC_DEBUG
  11. #undef RTC_DEBUG

  12. #ifdef RTC_DEBUG
  13. #include <stdio.h>
  14. #endif

  15. #define RTC_ALARM_EN        (COMM_OFF)


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

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

  40.      .sec_match        =  1,
  41.      .min_match        =  0,
  42.      .hour_match       =  0,
  43.      .mday_match       =  0,
  44.      .mon_match        =  0,
  45.      .year_match       =  0,
  46.      .dayofweek_match  =  0,
  47. };
  48. #endif

  49. // notification of rtc
  50. void rtc_notification(rtc_callback_args_t *p_args)
  51. {
  52.     /* TODO: add your own code here */
  53.     (void)p_args;
  54.     // if(p_args->event == RTC_EVENT_PERIODIC_IRQ)
  55.     //     rtc_flag=1;
  56.     // else if(p_args->event == RTC_EVENT_ALARM_IRQ)
  57.     //     rtc_alarm_flag=1;
  58. }


  59. void Rtc_Init(void)
  60. {
  61.     fsp_err_t err = FSP_SUCCESS;

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

  70. #if (RTC_ALARM_EN == COMM_ON)
  71.     /* Set alarm time */
  72.     R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_alarm_time);
  73. #endif
  74. }

  75. // set date-time
  76. void Rtc_SetDateTime(rtc_time_t * const p_time)
  77. {
  78.     R_RTC_CalendarTimeSet(&g_rtc0_ctrl, p_time);
  79. }

  80. // get date-time
  81. rtc_time_t Rtc_GetDateTime(void)
  82. {
  83.     rtc_time_t get_time;

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

  86. #ifdef RTC_DEBUG
  87.     printf("RTC: %d-%d-%d, %d:%d:%d, %dw\n", get_time.tm_year + 1900,
  88.                                            get_time.tm_mon,
  89.                                            get_time.tm_mday,
  90.                                            get_time.tm_hour,
  91.                                            get_time.tm_min,
  92.                                            get_time.tm_sec,
  93.                                            get_time.tm_wday);
  94. #endif

  95.     return get_time;
  96. }
复制代码



# 3 试验效果

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

野火论坛202308192329491323..png



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-2 17:37 , Processed in 0.037827 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表