0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

基于RASC的keil电子时钟制作(瑞萨RA)(7)----配置RTC时钟及显示时间

嵌入式单片机MCU开发 来源:嵌入式单片机MCU开发 作者:嵌入式单片机MCU开 2023-12-01 15:06 次阅读

概述

本文将详细讲解如何借助e2studio来对瑞萨微控制器进行实时时钟(RTC)的设置和配置,以便实现日历功能和一秒钟产生的中断,从而通过串口输出实时数据。
实时时钟(RTC)模块是一种时间管理外设,主要用于记录和控制日期和时间。与常见的微控制器(MCU)中的定时器不同,RTC时钟提供了两种计时方式:日期模式和计时模式。RTC时钟的常用功能包括设置时间、设定闹钟、配置周期性中断以及启动或停止操作。
通过使用e2studio工具,我们可以轻松地对瑞萨微控制器进行RTC配置,从而实现高精度的时间和日期管理。在本文中,我们将重点讨论如何设置RTC时钟日历和产生一秒钟的中断,使得串口能够实时打印数据。

硬件准备

首先需要准备一个开发板,这里我准备的是芯片型号R7FA2E1A72DFL的开发板:

在这里插入图片描述

在这里插入图片描述

视频教程

https://www.bilibili.com/video/BV1AV41157au/

RTC配置

点击Stacks->New Stack->Timers -> Realtime Clock(r_rtc)。

在这里插入图片描述

RTC属性配置

在这里插入图片描述

其中LOCO为内部低速时钟,需要准确定时还是需要外部低速晶振Sub-clock。

在这里插入图片描述
在这里插入图片描述

设定时间

在启动RTC后,需要为其设定当前时间。您可以使用R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time)函数来实现这一目标。具体的时间参数可以通过修改set_time变量来调整。
在这里插入图片描述

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

在这里插入图片描述

设定周期性中断

如果您想要使用RTC实现固定延迟中断,可以通过R_RTC_PeriodicIrqRateSet(rtc_ctrl_t *const p_ctrl, rtc_periodic_irq_select_t const rate)函数来实现。例如,要设置1秒的周期性中断,您可以使用如下代码:
R_RTC_PeriodicIrqRateSet(&g_rtc0_ctrl, RTC_PERIODIC_IRQ_SELECT_1_SECOND);
每次周期性中断产生时,系统将触发回调函数的事件RTC_EVENT_PERIODIC_IRQ。

设定日历闹钟时间

在启动RTC后,您可以设置日历闹钟时间。通过使用R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_alarm_time)函数,可以设定闹钟时间。具体的时间参数可以通过修改set_alarm_time变量来调整。具体设置方法如下。
在这个示例中,我们仅设置了sec_match为1,因此每隔一分钟,当秒数达到5秒时,闹钟都会触发。如果要实现每天只响铃一次的功能,需要同时将min_match和hour_match设置为1。

//RTC闹钟变量
rtc_alarm_time_t set_alarm_time=
{
     .time.tm_sec  = 55,      /* 秒,范围从 0 到 59 */
     .time.tm_min  = 59,      /* 分,范围从 0 到 59 */
     .time.tm_hour = 23,      /* 小时,范围从 0 到 23*/
     .time.tm_mday = 29,       /* 一月中的第几天,范围从 1 到 31*/
     .time.tm_mon  = 11,      /* 月份,范围从 0 到 11*/
     .time.tm_year = 123,     /* 自 1900 起的年数,2023为123*/
     .time.tm_wday = 6,       /* 一周中的第几天,范围从 0 到 6*/

     .sec_match        =  1,//每次秒到达设置的进行报警
     .min_match        =  0,
     .hour_match       =  0,
     .mday_match       =  0,
     .mon_match        =  0,
     .year_match       =  0,
     .dayofweek_match  =  0,
    };

在这里插入图片描述

回调函数

可以触发进入回调函数的事件如下所示,RTC_EVENT_PERIODIC_IRQ为设置的实时性事件,例如1s一次,RTC_EVENT_ALARM_IRQ为闹钟事件。
在这里插入图片描述

//RTC回调函数
volatile bool rtc_flag = 0;//RTC延时1s标志位
volatile bool rtc_alarm_flag = 0;//RTC闹钟
/* Callback function */
void rtc_callback(rtc_callback_args_t *p_args)
{
    /* TODO: add your own code here */
    if(p_args- >event == RTC_EVENT_PERIODIC_IRQ)
        rtc_flag=1;
    else if(p_args- >event == RTC_EVENT_ALARM_IRQ)
        rtc_alarm_flag=1;
}

在这里插入图片描述
同时在主程序中开启RTC已经设置时间和闹钟。

/**********************RTC开启***************************************/
       /* 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);

       /* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
       R_RTC_ClockSourceSet(&g_rtc0_ctrl);

       /* 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);

       R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_alarm_time);
       uint8_t rtc_second= 0;      //秒
       uint8_t rtc_minute =0;      //分
       uint8_t rtc_hour =0;         //时
       uint8_t rtc_day =0;          //日
       uint8_t rtc_month =0;      //月
       uint16_t rtc_year =0;        //年
       uint8_t rtc_week =0;        //周
       rtc_time_t get_time;

在这里插入图片描述
同时在主函数的while循环中添加打印和中断处理,以及当前时间显示。

if(rtc_flag)
           {
               R_RTC_CalendarTimeGet(&g_rtc0_ctrl, &get_time);//获取RTC计数时间
               rtc_flag=0;
               rtc_second=get_time.tm_sec;//秒
               rtc_minute=get_time.tm_min;//分
               rtc_hour=get_time.tm_hour;//时
               rtc_day=get_time.tm_mday;//日
               rtc_month=get_time.tm_mon;//月
               rtc_year=get_time.tm_year; //年
               rtc_week=get_time.tm_wday;//周
               printf(" %d y %d m %d d %d h %d m %d s %d wn",rtc_year+1900,rtc_month,rtc_day,rtc_hour,rtc_minute,rtc_second,rtc_week);

				//时间显示
               num1=rtc_hour/10;
               num2=rtc_hour%10;

               num3=rtc_minute/10;
               num4=rtc_minute%10;
           }
           if(rtc_alarm_flag)
           {
               rtc_alarm_flag=0;
               printf("/************************Alarm Clock********************************/n");
           }

       R_BSP_SoftwareDelay(10U, BSP_DELAY_UNITS_MILLISECONDS);

在这里插入图片描述
为了快速启动,关闭数码管测试。

在这里插入图片描述

演示效果

设置每过1s打印一次当前时间,设置过1分钟,在10秒时候闹铃。
在这里插入图片描述

更换日期显示。
在这里插入图片描述

数码管显示日期

可以在主程序里面添加显示,让数码管显示日期。

num1=rtc_hour/10;
               num2=rtc_hour%10;

               num3=rtc_minute/10;
               num4=rtc_minute%10;

在这里插入图片描述

主程序

#include "hal_data.h"
#include < stdio.h >
#include "smg.h"
#include "timer_smg.h"

FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER

//数码管变量
uint8_t num1=1,num2=4,num3=6,num4=8;//4个数码管显示的数值
uint8_t num_flag=0;//4个数码管和冒号轮流显示,一轮刷新五次


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


//RTC闹钟变量
rtc_alarm_time_t set_alarm_time=
{
     .time.tm_sec  = 58,      /* 秒,范围从 0 到 59 */
     .time.tm_min  = 59,      /* 分,范围从 0 到 59 */
     .time.tm_hour = 23,      /* 小时,范围从 0 到 23*/
     .time.tm_mday = 29,       /* 一月中的第几天,范围从 1 到 31*/
     .time.tm_mon  = 11,      /* 月份,范围从 0 到 11*/
     .time.tm_year = 123,     /* 自 1900 起的年数,2023为123*/
     .time.tm_wday = 6,       /* 一周中的第几天,范围从 0 到 6*/

     .sec_match        =  1,//每次秒到达设置的进行报警
     .min_match        =  0,
     .hour_match       =  0,
     .mday_match       =  0,
     .mon_match        =  0,
     .year_match       =  0,
     .dayofweek_match  =  0,
    };

//RTC回调函数
volatile bool rtc_flag = 0;//RTC延时1s标志位
volatile bool rtc_alarm_flag = 0;//RTC闹钟
/* Callback function */
void rtc_callback(rtc_callback_args_t *p_args)
{
    /* TODO: add your own code here */
    if(p_args- >event == RTC_EVENT_PERIODIC_IRQ)
        rtc_flag=1;
    else if(p_args- >event == RTC_EVENT_ALARM_IRQ)
        rtc_alarm_flag=1;
}


fsp_err_t err = FSP_SUCCESS;
volatile bool uart_send_complete_flag = false;
void user_uart_callback (uart_callback_args_t * p_args)
{
    if(p_args- >event == UART_EVENT_TX_COMPLETE)
    {
        uart_send_complete_flag = true;
    }
}

#ifdef __GNUC__                                 //串口重定向
    #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
    #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE
{
        err = R_SCI_UART_Write(&g_uart9_ctrl, (uint8_t *)&ch, 1);
        if(FSP_SUCCESS != err) __BKPT();
        while(uart_send_complete_flag == false){}
        uart_send_complete_flag = false;
        return ch;
}

int _write(int fd,char *pBuffer,int size)
{
    for(int i=0;i< size;i++)
    {
        __io_putchar(*pBuffer++);
    }
    return size;
}





/*******************************************************************************************************************//**
 * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function
 * is called by main() when no RTOS is used.
 **********************************************************************************************************************/
void hal_entry(void)
{
    /* TODO: add your own code here */

    /* Open the transfer instance with initial configuration. */
       err = R_SCI_UART_Open(&g_uart9_ctrl, &g_uart9_cfg);
       assert(FSP_SUCCESS == err);



/**********************数码管测试***************************************/
//              ceshi_smg();
/**********************定时器开启***************************************/
    /* Initializes the module. */
    err = R_GPT_Open(&g_timer0_ctrl, &g_timer0_cfg);
    /* Handle any errors. This function should be defined by the user. */
    assert(FSP_SUCCESS == err);
    /* Start the timer. */
    (void) R_GPT_Start(&g_timer0_ctrl);


/**********************RTC开启***************************************/
    /* 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);

    /* Set the RTC clock source. Can be skipped if "Set Source Clock in Open" property is enabled. */
    R_RTC_ClockSourceSet(&g_rtc0_ctrl);

/* 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);

           R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_alarm_time);
           uint8_t rtc_second= 0;      //秒
           uint8_t rtc_minute =0;      //分
           uint8_t rtc_hour =0;         //时
           uint8_t rtc_day =0;          //日
           uint8_t rtc_month =0;      //月
           uint16_t rtc_year =0;        //年
           uint8_t rtc_week =0;        //周
           rtc_time_t get_time;


       while(1)
       {
           if(rtc_flag)
           {
               R_RTC_CalendarTimeGet(&g_rtc0_ctrl, &get_time);//获取RTC计数时间
               rtc_flag=0;
               rtc_second=get_time.tm_sec;//秒
               rtc_minute=get_time.tm_min;//分
               rtc_hour=get_time.tm_hour;//时
               rtc_day=get_time.tm_mday;//日
               rtc_month=get_time.tm_mon;//月
               rtc_year=get_time.tm_year; //年
               rtc_week=get_time.tm_wday;//周
               printf(" %d y %d m %d d %d h %d m %d s %d wn",rtc_year+1900,rtc_month,rtc_day,rtc_hour,rtc_minute,rtc_second,rtc_week);

                //时间显示
               num1=rtc_hour/10;
               num2=rtc_hour%10;

               num3=rtc_minute/10;
               num4=rtc_minute%10;
           }
           if(rtc_alarm_flag)
           {
               rtc_alarm_flag=0;
               printf("/************************Alarm Clock********************************/n");
           }

           R_BSP_SoftwareDelay(10U, BSP_DELAY_UNITS_MILLISECONDS);
       }



#if BSP_TZ_SECURE_BUILD
    /* Enter non-secure code */
    R_BSP_NonSecureEnter();
#endif
}

审核编辑:汤梓红

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 瑞萨
    +关注

    关注

    32

    文章

    22214

    浏览量

    84915
  • 时钟
    +关注

    关注

    10

    文章

    1480

    浏览量

    130306
  • keil
    +关注

    关注

    68

    文章

    1196

    浏览量

    165315
  • RTC
    RTC
    +关注

    关注

    2

    文章

    484

    浏览量

    65454
  • 电子时钟
    +关注

    关注

    11

    文章

    197

    浏览量

    24124
收藏 人收藏

    评论

    相关推荐

    电子时钟制作(瑞萨RA)(6)----配置RTC时钟显示时间

    本文将详细讲解如何借助e2studio来对瑞萨微控制器进行实时时钟RTC)的设置和配置,以便实现日历功能和一秒钟产生的中断,从而通过串口输出实时数据。
    的头像 发表于 12-01 14:09 321次阅读
    <b class='flag-5'>电子时钟</b><b class='flag-5'>制作</b>(瑞萨<b class='flag-5'>RA</b>)(6)----<b class='flag-5'>配置</b><b class='flag-5'>RTC</b><b class='flag-5'>时钟</b>及<b class='flag-5'>显示</b><b class='flag-5'>时间</b>

    基于RASCkeil电子时钟制作(瑞萨RA)(1)----安装RASC

    RA Smart Configurator"是一种基于"灵活组合软件"概念的代码生成辅助工具。它可以自动生成微控制器的初始配置程序。该工具提供了基本的引脚配置功能,并提
    的头像 发表于 12-01 14:39 347次阅读
    基于<b class='flag-5'>RASC</b>的<b class='flag-5'>keil</b><b class='flag-5'>电子时钟</b><b class='flag-5'>制作</b>(瑞萨<b class='flag-5'>RA</b>)(1)----安装<b class='flag-5'>RASC</b>

    基于RASCkeil电子时钟制作(瑞萨RA)(2)----配置keil以及使用串口进行打印

    本篇文章主要介绍了一种基于瑞萨RA系列微控制器的电子时钟制作方法,重点关注如何利用瑞萨RA Smart Configurator生成串口配置
    的头像 发表于 12-01 14:47 389次阅读
    基于<b class='flag-5'>RASC</b>的<b class='flag-5'>keil</b><b class='flag-5'>电子时钟</b><b class='flag-5'>制作</b>(瑞萨<b class='flag-5'>RA</b>)(2)----<b class='flag-5'>配置</b><b class='flag-5'>keil</b>以及使用串口进行打印

    基于RA6M5开发板的低功耗电子时钟设计

    本项目是基于启明RA6M5开发板搭载2.4寸液晶屏的电子时钟,该电子时钟有两个模式——正常模式和低功耗模式,可以通过开发板的按键改变时钟模式。
    的头像 发表于 12-25 12:26 504次阅读
    基于<b class='flag-5'>RA</b>6M5开发板的低功耗<b class='flag-5'>电子时钟</b>设计

    电子时钟LABVIEW数码显示制作实例

    `电子时钟LABVIEW数码显示制作`
    发表于 04-18 11:05

    RTC时钟芯片在电子时钟中的作用

    很多电子爱好者都热衷制作电子时钟来练手,这些时钟会使用数码管、点阵屏、LCD液晶屏、OLED屏、TFT屏等显示模块,所使用的
    发表于 02-11 07:12

    RA4系列开发板体验】10. 我的试用总结

    KEIL下UART实现printf与scanf重定向【RA4系列开发板体验】4. PWM驱动LED【
    发表于 12-10 22:34

    RA4系列开发板体验】1、开发板要来了(1)

    了,先体现进行一下准备工作。由于板子还没有到手,咱们主要先了解一下开发环境。RA系列的单片机有一个自己的开发环境e2 studio,这个是官方首推的,是
    发表于 12-12 17:04

    RA4系列开发板体验】体验过程

    、使用 RASC 生成 Keil 工程+点亮LED参照“ 【RA4系列开发板体验】2. 使用RASC
    发表于 12-18 16:20

    RA4系列开发板体验】+ 开发环境搭建

    习惯。笔者多年来都是用keil的,keil的优势在于换一个芯片,只需要重新安装一个pack,非常方便,不需要再进行额外的配置,学习成本低。
    发表于 12-21 23:50

    RA4M2设计挑战赛】1. RASC配置FreeRTOS

    RA4系列开发板体验】3. KEIL下UART实现printf与scanf重定向【RA4系列开发板体验】4. PWM驱动LED【
    发表于 02-11 19:17

    RA MCU创意氛围赛】以RA2E的车载VFD屏幕时钟

    是 R7FA4M2AD3CFP ,如片如下: 经过对照野火发布的的文档进行一段时间的学习野火-
    发表于 05-21 17:02

    FPB-RA6E1快速原型板】简单开箱和RASC+Keil开发环境搭建

    ,看看和GitHub下载页面上的是否一致: 另外,RA系列MCU Keil支持包,可以在Keil官网找到下载页面: MDK5 - Re
    发表于 05-22 23:13

    51单片机带闹钟可调时间电子时钟程序设计-keil工程-dsz

    51单片机带闹钟可调时间电子时钟程序设计-keil工程。
    发表于 05-09 10:59 73次下载

    RX和RA系列主时钟电路和子时钟电路设计指南

    电子发烧友网站提供《RX和RA系列主时钟电路和子时钟电路设计指南.pdf》资料免费下载
    发表于 02-19 10:20 1次下载
    RX和<b class='flag-5'>RA</b>系列主<b class='flag-5'>时钟</b>电路和<b class='flag-5'>子时钟</b>电路设计指南