瑞萨e2studio.19--RTC时钟日历&闹钟&周期性中断
- 概述
- 视频教学
- csdn课程
- 样品申请
- 完整代码下载
- 硬件准备
- 新建工程
- 工程模板
- 保存工程路径
- 芯片配置
- 工程模板选择
- 时钟配置
- uart配置
- uart属性配置
- 设置e2studio堆栈
- e2studio的重定向printf设置
- printf输出重定向到串口
- RTC配置
- RTC属性配置
- R_RTC_Open()函数原型
- R_RTC_ClockSourceSet()函数原型
- R_RTC_CalendarTimeSet()函数原型
- R_RTC_PeriodicIrqRateSet()函数原型
- R_RTC_CalendarAlarmSet()函数原型
- R_RTC_CalendarTimeGet()函数原型
- 设定时间
- 设定周期性中断
- 设定日历闹钟时间
- 演示效果
- 完整代码
- 最后
概述
本文将详细讲解如何借助e2studio来对瑞萨微控制器进行实时时钟(RTC)的设置和配置,以便实现日历功能和一秒钟产生的中断,从而通过串口输出实时数据。
实时时钟(RTC)模块是一种时间管理外设,主要用于记录和控制日期和时间。与常见的微控制器(MCU)中的定时器不同,RTC时钟提供了两种计时方式:日期模式和计时模式。RTC时钟的常用功能包括设置时间、设定闹钟、配置周期性中断以及启动或停止操作。
通过使用e2studio工具,我们可以轻松地对瑞萨微控制器进行RTC配置,从而实现高精度的时间和日期管理。在本文中,我们将重点讨论如何设置RTC时钟日历和产生一秒钟的中断,使得串口能够实时打印数据。最近在弄ST和瑞萨RA的课程,需要RA样片的可以加群申请:6_15061293 。
视频教学
https://www.bilibili.com/video/BV1Bh411L7eC/
瑞萨e2studio(19)----Code Flash&Data Flash读写
csdn课程
csdn课程更加详细。
https://edu.csdn.net/course/detail/36131
样品申请
https://www.wjx.top/vm/wBbmSFp.aspx#
完整代码下载
https://download.csdn.net/download/qq_24312945/87749512
硬件准备
首先需要准备一个开发板,这里我准备的是芯片型号R7FA4M2AD3CFP的开发板:
新建工程
工程模板
保存工程路径
芯片配置
本文中使用R7FA4M2AD3CFP来进行演示。
工程模板选择
时钟配置
开发板上的外部高速晶振为12M,需要修改XTAL为12M.
uart配置
点击Stacks->New Stack->Driver->Connectivity -> UART (r_sci_uart)。
uart属性配置
设置e2studio堆栈
e2studio的重定向printf设置
C++ 构建->设置->GNU ARM Cross C Linker->Miscellaneous去掉Other linker flags中的 “–specs=rdimon.specs”
printf输出重定向到串口
打印最常用的方法是printf,所以要解决的问题是将printf的输出重定向到串口,然后通过串口将数据发送出去。
注意一定要加上头文件#include <stdio.h>
#include <stdio.h>
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;
}
RTC配置
点击Stacks->New Stack->Driver->Timers -> Realtime Clock(r_rtc)。
RTC属性配置
R_RTC_Open()函数原型
故可以用R_RTC_Open()函数进行初始化和开启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);
R_RTC_ClockSourceSet()函数原型
设置日历时间。
/* 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()函数原型
故可以用R_RTC_CalendarTimeSet()函数进行设置当前日历时间。
/* R_RTC_CalendarTimeSet must be called at least once to start the RTC */
R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time);
R_RTC_PeriodicIrqRateSet()函数原型
故可以用R_RTC_PeriodicIrqRateSet()函数进行设置周期中断。
/* Set the periodic interrupt rate to 1 second */
R_RTC_PeriodicIrqRateSet(&g_rtc0_ctrl, RTC_PERIODIC_IRQ_SELECT_1_SECOND);
R_RTC_CalendarAlarmSet()函数原型
故可以用R_RTC_CalendarAlarmSet()函数进行设置闹钟。
R_RTC_CalendarAlarmSet(&g_rtc0_ctrl, &set_alarm_time1);
R_RTC_CalendarTimeGet()函数原型
故可以用R_RTC_CalendarTimeGet ()函数进行获取RTC计数时间。
R_RTC_CalendarTimeGet(&g_rtc0_ctrl, &get_time);//获取RTC计数时间
设定时间
在启动RTC后,需要为其设定当前时间。您可以使用R_RTC_CalendarTimeSet(&g_rtc0_ctrl, &set_time)函数来实现这一目标。具体的时间参数可以通过修改set_time变量来调整。具体设置方法如下:
/* 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 = 20, /* 一月中的第几天,范围从 1 到 31*/
.tm_mon = 11, /* 月份,范围从 0 到 11*/
.tm_year = 121, /* 自 1900 起的年数,2021为121*/
.tm_wday = 5, /* 一周中的第几天,范围从 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_alarm_time_t set_alarm_time=
{
.time.tm_sec = 10, /* 秒,范围从 0 到 59 */
.time.tm_min = 30, /* 分,范围从 0 到 59 */
.time.tm_hour = 12, /* 小时,范围从 0 到 23*/
.time.tm_mday = 2, /* 一月中的第几天,范围从 1 到 31*/
.time.tm_mon = 5, /* 月份,范围从 0 到 11*/
.time.tm_year = 123, /* 自 1900 起的年数,2021为123*/
.time.tm_wday = 2, /* 一周中的第几天,范围从 0 到 6*/
.sec_match = 1,
.min_match = 0,
.hour_match = 0,
.mday_match = 0,
.mon_match = 0,
.year_match = 0,
.dayofweek_match = 0,
};
演示效果
设置每过1s打印一次当前时间,设置过1分钟,在10秒时候闹铃。
完整代码
/* 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 = 2, /* 一月中的第几天,范围从 1 到 31*/
.tm_mon = 5, /* 月份,范围从 0 到 11*/
.tm_year = 123, /* 自 1900 起的年数,2021为121*/
.tm_wday = 2, /* 一周中的第几天,范围从 0 到 6*/
// .tm_yday=0, /* 一年中的第几天,范围从 0 到 365*/
// .tm_isdst=0; /* 夏令时*/
};
rtc_alarm_time_t set_alarm_time=
{
.time.tm_sec = 10, /* 秒,范围从 0 到 59 */
.time.tm_min = 30, /* 分,范围从 0 到 59 */
.time.tm_hour = 12, /* 小时,范围从 0 到 23*/
.time.tm_mday = 2, /* 一月中的第几天,范围从 1 到 31*/
.time.tm_mon = 5, /* 月份,范围从 0 到 11*/
.time.tm_year = 123, /* 自 1900 起的年数,2021为123*/
.time.tm_wday = 2, /* 一周中的第几天,范围从 0 到 6*/
.sec_match = 1,
.min_match = 0,
.hour_match = 0,
.mday_match = 0,
.mon_match = 0,
.year_match = 0,
.dayofweek_match = 0,
};
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;
}
/*******************************************************************************************************************//**
* 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 */
err = R_SCI_UART_Open(&g_uart9_ctrl, &g_uart9_cfg);
assert(FSP_SUCCESS == err);
printf("hello\n");
/* 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 w\n",rtc_year+1900,rtc_month,rtc_day,rtc_hour,rtc_minute,rtc_second,rtc_week);
}
if(rtc_alarm_flag)
{
rtc_alarm_flag=0;
printf("/************************Alarm Clock********************************/\n");
}
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
最后
以上的代码会在Q群里分享。QQ群:615061293。
或者关注微信公众号『记帖』,持续更新文章和学习资料,可加作者的微信交流学习!