查看: 1130|回复: 0

[分享] 【FRDM-K32L2B3 开发板试用】+RTC电子时钟

[复制链接]
  • TA的每日心情
    奋斗
    2023-2-15 00:12
  • 签到天数: 969 天

    [LV.10]以坛为家III

    175

    主题

    2843

    帖子

    34

    金牌会员

    Rank: 6Rank: 6

    积分
    7092
    最后登录
    2023-4-20
    发表于 2020-9-21 10:37:11 | 显示全部楼层 |阅读模式
    K32L2B3 开发板提供了RTC定时功能,在下载例程后,使用开发板的虚拟串口即可进行输入输出处理,其功能是首先提示出系统的数据,然后要求使用者输入RTC的闹钟延时值,当定时时间到后,即给出提示信息。
    下载程序后,其运行效果如图1和图2所示。
    1.jpg
    1 初始提示信息
    2.jpg
    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 !!!! ");
        }
    }


    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-4-25 21:12 , Processed in 0.107142 second(s), 20 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表