先来个理论知识:
RTC,英文是Real-time clock,翻译过来就是“实时时钟”,即单片机实时时钟,指可以像时钟一样输出实际时间的电子设备,一般为集成电路,因此也称为时钟芯片。RTC主要功能是为我们提供时间、日历及闹钟等与时间相关的服务。目前实时时钟芯片大多采用精度较高的晶体振荡器作为时钟源。有些时钟芯片为了在主电源掉电时,还可以工作,需要外加电池供电。
RTC一般都是用的32.768KHZ的晶振作为时钟。
框架:
晶振原理:
引脚:
实时截图:
代码:
RTC初始
RTC_REGS->MODE2.RTC_CTRLA |= (uint16_t)RTC_MODE2_CTRLA_SWRST_Msk;
while((RTC_REGS->MODE2.RTC_SYNCBUSY & RTC_MODE2_SYNCBUSY_SWRST_Msk) == RTC_MODE2_SYNCBUSY_SWRST_Msk)
{
/* Wait for synchronization after Software Reset */
}
RTC_REGS->MODE2.RTC_CTRLA = (uint16_t)(RTC_MODE2_CTRLA_MODE(2UL) | RTC_MODE2_CTRLA_PRESCALER(0xBUL) | RTC_MODE2_CTRLA_CLOCKSYNC_Msk | RTC_MODE2_CTRLA_ENABLE_Msk );
while((RTC_REGS->MODE2.RTC_SYNCBUSY & RTC_MODE2_SYNCBUSY_ENABLE_Msk) == RTC_MODE2_SYNCBUSY_ENABLE_Msk)
{
/* Wait for Synchronization after Enabling RTC */
}
RTC_REGS->MODE2.RTC_INTENSET = 0x100U;
到设定就反转灯
if (int_cause & RTC_CLOCK_INT_MASK_ALARM0)
{
alarm_triggered = true;
LED_Toggle();
}
主循环:
RTC_RTCCTimeGet(&sys_time);
if(old_second!=sys_time.tm_sec)
{
printf("System time is: %02d:%02d:%02d\r\n",sys_time.tm_hour, sys_time.tm_min, sys_time.tm_sec);
old_second=sys_time.tm_sec;
}
if(alarm_triggered == true)
{
printf("\n\rAlarm Triggered in %02d:%02d:%02d,\r\n",sys_time.tm_hour, sys_time.tm_min, sys_time.tm_sec);
if(++old_alarm_min>59)
{
old_alarm_min=0;
if(++old_alarm_hour>23)
{
old_alarm_hour=0;
}
}
alarm_time.tm_hour = old_alarm_hour;
alarm_time.tm_min = old_alarm_min;
RTC_RTCCAlarm0Set(&alarm_time, RTC_ALARM_MASK_HHMMSS);
printf("\n\rNext Alarm Triggered in %02d:%02d:%02d,\r\n",alarm_time.tm_hour, alarm_time.tm_min, alarm_time.tm_sec);
alarm_triggered = false;
实时时钟到设定时,用LED翻转视频:
34ccb91ba624abcf0a3f874cd1b7228e