一、RTC概述
-
背景
-
定义
实时时钟的缩写是RTC(Real_Time Clock)。RTC 是集成电路,通常称为
-
本质
一个32位的计数器,只能向上计数(输入频率低的定时器,可以记录很久的时间,有电子的,可以MCU掉电继续记录时间)
-
应用场景
日历:记录一个时间(年月日,时分秒)
闹钟:设定特定时间进入中断(处理特定事情:软件复位,软件唤醒,退出待机...)
RTC_Second(秒中断)
RTC_Overflow(溢出事件/时间无效)
RTC_Alarm(闹钟中断)
二、RTC应用
模块配置
初始化
void RTC_Init(void)
{
/* Initialize RTC instance
* - See RTC configuration component for options
*/
RTC_DRV_Init(RTCTIMER1, &rtcTimer1_Config0);
/* Set the time and date */
RTC_DRV_SetTimeDate(RTCTIMER1, &rtcTimer1_StartTime0);
/* Start the RTC counter */
RTC_DRV_StartCounter(RTCTIMER1);
}
测试代码
void RTC_Get_CurrentTime(uint16_t year,uint16_t month,uint16_t day,uint16_t hour,uint16_t minutes,uint8_t seconds)
{
rtc_timedate_t tempTime = {0};
RTC_DRV_GetCurrentTimeDate(RTCTIMER1, &tempTime);
year = tempTime.year;
month = tempTime.month;
day = tempTime.day;
hour = tempTime.hour;
minutes = tempTime.minutes;
seconds = tempTime.seconds;
}
void RTC_Set_CurrentTime(uint16_t year,uint16_t month,uint16_t day,uint16_t hour,uint16_t minutes,uint8_t seconds)
{
rtc_timedate_t tempTime = {0};
tempTime.year = year;
tempTime.month = month;
tempTime.day = day;
tempTime.hour = hour;
tempTime.minutes = minutes;
tempTime.seconds = seconds;
RTC_DRV_StopCounter(RTCTIMER1);
RTC_DRV_SetTimeDate(RTCTIMER1, &tempTime);
RTC_DRV_StartCounter(RTCTIMER1);
}
void RTC_test(void)
{
///放在周期中跑,看打印时间
rtc_timedate_t tempTime;
RTC_DRV_GetCurrentTimeDate(RTCTIMER1, &tempTime);
u1_Printf("Current time:%d/%d/%d %d:%d:%d \r\n",tempTime.year,tempTime.month,tempTime.day,tempTime.hour,tempTime.minutes,tempTime.seconds);
}