实际使用中,我们会用到RTC的例子,这里以simple为例,介绍RTC的使用方法 找到SDK目录下examples\peripheral\rtc\example_rtc.c 1)初始化 void example_rtc(void) { struct tm tm = {0}; // Select 32k clock source pmu_select_32k(PMU_32K_SEL_32768HZ_XTAL);//如果外部有32768HZ晶体这里放开, //pmu_select_32k(PMU_32K_SEL_RC);//如果是使用 PMU_32K_SEL_RC,这里由外部电路决定选择哪一函数 // Check reboot reason if(pmu_reboot_reason() == PMU_REBOOT_FROM_ULTRA_DEEP_SLEEP) { /* * If wakeup from ultra-deep-sleep, RTC should be running * * If rtc_time_set(&tm, NULL) and pmu_xtal32k_enable_in_deep_sleep(true), RTC can come into deep-sleep mode */ // restore alarm callback rtc_alarm_set(NULL, rtc_alarm_handler); // Run rtc_start(); // Get reboot time rtc_time_get(&tm); log_debug("reboot_time: %s\n", asctime(&tm)); } else { // 2017.6.13 20:15:00 tm.tm_year = 2017 - 1900; tm.tm_mon = 6 - 1; tm.tm_mday = 13; tm.tm_hour = 20; tm.tm_min = 15; tm.tm_sec = 0; // Init RTC rtc_init(); //Set time rtc_time_set(&tm, NULL); // rtc_time_set(&tm, rtc_second_handler); // Set alarm tm.tm_sec = 30; rtc_alarm_set(&tm, rtc_alarm_handler); // Run rtc_start(); } } 2)RTC使用秒中断方式
void rtc_second_handler(const struct tm *tm) { log_debug("time: %s\n", asctime(tm)); } { // 配置 // 2017.6.13 20:15:00 tm.tm_year = 2017 - 1900; tm.tm_mon = 6 - 1; tm.tm_mday = 13; tm.tm_hour = 20; tm.tm_min = 15; tm.tm_sec = 0; // Init RTC rtc_init(); //Set time rtc_time_set(&tm, NULL); rtc_time_set(&tm, rtc_second_handler); // Set alarm // tm.tm_sec = 30; // rtc_alarm_set(&tm, rtc_alarm_handler); // Run rtc_start(); } 3)RTC使用闹钟方式 uint32_t idAf = 0; void rtc_alarm_handler(const struct tm *_tm) { log_debug("alarm: %s\n", asctime(_tm)); rtc_alarm_get(&tm); tm.tm_sec = 30+30*idAf; rtc_alarm_set(&tm, rtc_alarm_handler); } { // 配置 // 2017.6.13 20:15:00 tm.tm_year = 2017 - 1900; tm.tm_mon = 6 - 1; tm.tm_mday = 13; tm.tm_hour = 20; tm.tm_min = 15; tm.tm_sec = 0; // Init RTC rtc_init(); //Set time rtc_time_set(&tm, NULL); // rtc_time_set(&tm, rtc_second_handler); // Set alarm tm.tm_sec = 30;//30S一次 rtc_alarm_set(&tm, rtc_alarm_handler); // Run rtc_start(); } |