示例#1
0
/*!
 * @brief RTC init.
 * @param uint32_t instance     : RTC instance
 */
void rtcInit(uint32_t instance, rtc_datetime_t *date)
{
#ifndef FRDM_K22F120M
    // Configure RTC pins
    configure_rtc_pins(instance);
#endif

    // select the 1Hz for RTC_CLKOUT
    CLOCK_SYS_SetRtcOutSrc(kClockRtcoutSrc1Hz);

    RTC_DRV_Init(instance);

    RTC_DRV_SetDatetime(instance, date);
}
/*!
* @brief RTC in alarm mode.
*
* This function demostrates how to use RTC as an alarm clock.
*/
int main(void)
{
    uint32_t sec;
    uint32_t currSeconds;
    rtc_datetime_t date;

    // Init hardware.
    hardware_init();

    PRINTF("RTC example: set up time to wake up an alarm\r\n");

    // Init RTC
    RTC_DRV_Init(RTC_INSTANCE);

    /* Need to check this here as the RTC_DRV_Init() may have issued a software reset on the
     * module clearing all prior RTC OSC related setup */
    BOARD_InitRtcOsc();

    // 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;

    // Set RTC time to default
    RTC_DRV_SetDatetime(RTC_INSTANCE, &date);

    while (1)
    {
        busyWait = true;
        // Get date time.
        RTC_DRV_GetDatetime(RTC_INSTANCE, &date);

        // print default time
        PRINTF("Current datetime: %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
        sec = 0;
        PRINTF("Please input the number of second to wait for alarm \r\n");
        PRINTF("The second must be positive value\r\n");
        while (sec < 1)
        {
            SCANF("%d",&sec);
        }

        // Get current date time.
        RTC_DRV_GetDatetime(RTC_INSTANCE, &date);
        // Convert current date time to seconds
        RTC_HAL_ConvertDatetimeToSecs(&date, &currSeconds);

        // Add sec to currSeconds
        currSeconds += sec;
        // Convert sec to date type
        RTC_HAL_ConvertSecsToDatetime(&currSeconds, &date);

        // Set alarm time
        if(!RTC_DRV_SetAlarm(RTC_INSTANCE, &date, true))
        {
            PRINTF("Failed to set alarm. \r\n");
            continue;
        }
        // Print alarm time
        PRINTF("Alarm will be occured at: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n",
                        date.year, date.month, date.day,
                        date.hour, date.minute, date.second);

        // Wait until alarm occures
        while(busyWait)
        {}

        PRINTF("\r\n Alarm occured !!!! ");
    }

}
示例#3
0
/*!
* @brief RTC in alarm mode.
*
* This function demostrates how to use RTC as an alarm clock.
*/
void main(void)
{
    uint32_t sec;
    uint32_t currSeconds;
    rtc_datetime_t date;

    // Init hardware.
    hardware_init();

    // Call this function to initialize the console UART.  This function
    // enables the use of STDIO functions (printf, scanf, etc.)
    dbg_uart_init();

    printf("RTC example: set up time to wake up an alarm\r\n");

    // Init RTC
    RTC_DRV_Init(RTC_INSTANCE);
    // 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;

    // Set RTC time to default
    RTC_DRV_SetDatetime(RTC_INSTANCE, &date);

    while (1)
    {
        // Get date time.
        RTC_DRV_GetDatetime(RTC_INSTANCE, &date);

        // print default time
        printf("Current datetime: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n",
                        date.year, date.month, date.day,
                        date.hour, date.minute, date.second);

        // Convert current date time to seconds
        RTC_HAL_ConvertDatetimeToSecs(&date, &currSeconds);

        // Get alarm time from user
        sec = 0;
        printf("Please input the number of second to wait for alarm \r\n");
        printf("The second must be positive value\r\n");
        while (sec < 1)
        {
            scanf("%d",&sec);
        }

        // Add curr_seconds
        sec += currSeconds;
        // Convert sec to date type
        RTC_HAL_ConvertSecsToDatetime(&sec, &date);

        // Set alarm time
        if(!RTC_DRV_SetAlarm(RTC_INSTANCE, &date, true))
        {
            printf("Failed to set alarm. \r\n");
            continue;
        }

        // Print alarm time
        printf("Alarm will be occured at: %04hd-%02hd-%02hd %02hd:%02hd:%02hd\r\n",
                        date.year, date.month, date.day,
                        date.hour, date.minute, date.second);

        // Wait until alarm occures
        while(busyWait)
        {}

        printf("\r\n Alarm occured !!!! ");
    }

}