nrf52832日历calendar驱动

找了很多代码,包括官方的代码,但是都无法在最新的sdk(17.1)内使用,编译通过但是无法正常运行。

突发奇想,直接用app_timer的RTC得了。

前提是,一定得使能app_timer。

#include "nrf_calendar.h"
#include "nrf.h"
#include "drv_rtc.h"

static struct tm time_struct, m_tm_return_time;
static time_t m_time, m_last_calibrate_time = 0;
static float m_calibrate_factor = 0.0f;
static drv_rtc_t m_rtc_inst = DRV_RTC_INSTANCE(1);

#define NRF_CLOCK_LFCLK_RC CLOCK_LFCLKSRC_SRC_RC

void nrf_cal_set_time(uint32_t year, uint32_t month, uint32_t day, uint32_t hour, uint32_t minute, uint32_t second)
{
static time_t uncal_difftime, difftime, newtime;
time_struct.tm_year = year - 1900;
time_struct.tm_mon = month;
time_struct.tm_mday = day;
time_struct.tm_hour = hour;
time_struct.tm_min = minute;
time_struct.tm_sec = second;
newtime = mktime(&time_struct);
//CAL_RTC->TASKS_CLEAR = 1;

// Calculate the calibration offset
if(m_last_calibrate_time != 0)
{
difftime = newtime - m_last_calibrate_time;
uncal_difftime = m_time - m_last_calibrate_time;
m_calibrate_factor = (float)difftime / (float)uncal_difftime;
}

// Assign the new time to the local time variables
m_time = m_last_calibrate_time = newtime;
}

struct tm *nrf_cal_get_time(void)
{
time_t return_time;
return_time = m_time + drv_rtc_counter_get(&m_rtc_inst) / 16384;
m_tm_return_time = *localtime(&return_time);
return &m_tm_return_time;
}

struct tm *nrf_cal_get_time_calibrated(void)
{
time_t uncalibrated_time, calibrated_time;
if(m_calibrate_factor != 0.0f)
{
uncalibrated_time = m_time + drv_rtc_counter_get(&m_rtc_inst) / 16384;
calibrated_time = m_last_calibrate_time + (time_t)((float)(uncalibrated_time - m_last_calibrate_time) * m_calibrate_factor + 0.5f);
m_tm_return_time = *localtime(&calibrated_time);
return &m_tm_return_time;
}
else return nrf_cal_get_time();
}

char *nrf_cal_get_time_string(bool calibrated)
{
static char cal_string[80];
strftime(cal_string, 80, "%x - %H:%M:%S", (calibrated ? nrf_cal_get_time_calibrated() : nrf_cal_get_time()));
return cal_string;
}


欢迎转载,本文地址: https://blog.prodrich.com/detail/42/

带着使命来到世上的你,给他人提供价值,才有价值