K32L2B3 开发板提供了RTC定时功能,在下载例程后,使用开发板的虚拟串口即可进行输入输出处理,其功能是首先提示出系统的数据,然后要求使用者输入RTC的闹钟延时值,当定时时间到后,即给出提示信息。 下载程序后,其运行效果如图1和图2所示。 图1 初始提示信息 图2 输入数值后的提示信息 由于家中的电脑无法进行程序编译,故无法修改RTC的起始时间,使用者则可以酌情对其加以修改。 例程的源代码如下: int main(void) { uint32_t sec; uint32_t currSeconds; uint8_t index; rtc_datetime_t date; rtc_config_t rtcConfig; /* Board pin, clock, debug console init */ BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_InitDebugConsole(); /* Init RTC */ /* * rtcConfig.wakeupSelect = false; * rtcConfig.updateMode = false; * rtcConfig.supervisorAccess = false; * rtcConfig.compensationInterval = 0; * rtcConfig.compensationTime = 0; */ RTC_GetDefaultConfig(&rtcConfig); RTC_Init(RTC, &rtcConfig); #if!(defined(FSL_FEATURE_RTC_HAS_NO_CR_OSCE) &&FSL_FEATURE_RTC_HAS_NO_CR_OSCE) /* If the oscillator has not been enabled.*/ if (0U == (RTC->CR &RTC_CR_OSCE_MASK)) { /* Select RTC clock source */ RTC_SetClockSource(RTC); /* Wait for OSC clock steady. */ EXAMPLE_WaitOSCReady(EXAMPLE_OSC_WAIT_TIME_MS); } #endif /*FSL_FEATURE_RTC_HAS_NO_CR_OSCE */ #if(defined(EXAMPLE_CAP_LOAD_VALUE) && EXAMPLE_CAP_LOAD_VALUE) #if (defined(FSL_FEATURE_RTC_HAS_OSC_SCXP)&& FSL_FEATURE_RTC_HAS_OSC_SCXP) /* Change the RTC oscillator capacity loadvalue. */ RTC_SetOscCapLoad(RTC,EXAMPLE_CAP_LOAD_VALUE); #endif /*FSL_FEATURE_RTC_HAS_OSC_SCXP */ #endif /*EXAMPLE_CAP_LOAD_VALUE */ PRINTF("RTCexample: set up time to wake up an alarm\r\n"); /* Set a start date time and start RT */ date.year = 2014U; date.month = 12U; date.day = 25U; date.hour = 19U; date.minute = 0; date.second = 0; /* RTC time counter has to be stoppedbefore setting the date & time in the TSR register */ RTC_StopTimer(RTC); /* Set RTC time to default */ RTC_SetDatetime(RTC, &date); /* Enable RTC alarm interrupt */ RTC_EnableInterrupts(RTC, kRTC_AlarmInterruptEnable); /* Enable at the NVIC */ EnableIRQ(RTC_IRQn); /* Start the RTC time counter */ RTC_StartTimer(RTC); /* This loop will set the RTC alarm */ while (1) { busyWait = true; index = 0; sec = 0; /* Get date time */ RTC_GetDatetime(RTC, &date); /* print default time */ PRINTF("Currentdatetime: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n", date.year,date.month, date.day, date.hour, date.minute, date.second); /* Get alarm time from user */ PRINTF("Pleaseinput the number of second to wait for alarm \r\n"); PRINTF("Thesecond must be positive value\r\n"); while (index != 0x0D) { index = GETCHAR(); if ((index >= '0') &&(index <= '9')) { PUTCHAR(index); sec = sec * 10 + (index -0x30U); } } PRINTF("\r\n"); /* Read the RTC seconds register to getcurrent time in seconds */ currSeconds = RTC->TSR; /* Add alarm seconds to current time */ currSeconds += sec; /* Set alarm time in seconds */ RTC->TAR = currSeconds; /* Get alarm time */ RTC_GetAlarm(RTC, &date); /* Print alarm time */ PRINTF("Alarmwill occur at: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n", date.year,date.month, date.day, date.hour, date.minute,date.second); /* Wait until alarm occurs */ while (busyWait) { } PRINTF("\r\n Alarm occurs !!!! "); } }
|