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

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

3天内不再提示

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

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

概述

本文将详细讲解如何借助e2studio来对瑞萨微控制器进行实时时钟(RTC)的设置和配置,以便实现日历功能和一秒钟产生的中断,从而通过串口输出实时数据。

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

硬件准备

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

视频教程

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

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
}

审核编辑:汤梓红

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

    关注

    48

    文章

    6811

    浏览量

    147646
  • 瑞萨
    +关注

    关注

    32

    文章

    22214

    浏览量

    84915
  • RTC
    RTC
    +关注

    关注

    2

    文章

    484

    浏览量

    65444
  • 电子时钟
    +关注

    关注

    11

    文章

    197

    浏览量

    24124
收藏 人收藏

    评论

    相关推荐

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

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

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

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

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

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

    #硬声创作季 单片机电子时钟设计

    时钟电子时钟
    Mr_haohao
    发布于 :2022年10月13日 14:27:32

    LabVIEW制作电子时钟

    前些天看到每日一练里有制作电子时钟,今天换个玩法,使用簇制作
    发表于 12-18 15:58

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

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

    基于单片机的电子时钟设计与制作

    电子DIY》之《单片机实践项目》之基于单片机的电子时钟设计与制作(基于单片机的万年历的设计与制作)添加链接描述
    发表于 01-17 06:05

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

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

    基于RT-Thread和RA6M4的网络天气时钟设计资料推荐

    显示4、实时天气显示(包括天气,温度,湿度,风力等)整体框架硬件框架本设计以开发板RA6M4为主控,使用wifi模块esp8266作为c
    发表于 08-17 14:56

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

    引言 很高兴能有机会参加【RA MCU创意氛围赛】,在以前学习stm32的时候,就是野火的开发板、文档以及视频带我入门的。现在有空体验一下野火的产品——
    发表于 05-21 17:02

    电子时钟设计

    电子时钟设计。
    发表于 04-19 15:17 0次下载

    单片机__电子时钟LCD显示

    单片机__电子时钟LCD显示,可选择调整小时,分钟
    发表于 06-17 15:42 28次下载

    电子时钟设计电子时钟程序相关资料下载

    电子发烧友网站提供《电子时钟设计电子时钟程序相关资料下载.zip》资料免费下载
    发表于 05-26 08:00 115次下载

    项目 | 瑞萨RA2L1--电子时钟

    概述本篇文章主要介绍通过往期内容的学习,使用e2studio对R7FA2L1AB2DFL的开发板进行电子时钟项目开发,主要用到exit、oled、rtc、触摸按键、温湿度传感器模块。
    的头像 发表于 01-11 17:38 1195次阅读
    项目 | 瑞萨<b class='flag-5'>RA</b>2L1--<b class='flag-5'>电子时钟</b>

    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>电路设计指南