Hi,
I want to use nrf51 development board as a bluetooth alarm clock using internal RTC . How can I generate timestamps to determine current date and time. Can the timer library be used for this purpose?
Hi,
I want to use nrf51 development board as a bluetooth alarm clock using internal RTC . How can I generate timestamps to determine current date and time. Can the timer library be used for this purpose?
Digital clock and calendar along with nrf51422 and nrf51822 RTC's setting. BHAGYESH D BHAVASAR
// Internal 32kHz RC
NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos;
// Start the 32 kHz clock, and wait for the start up to complete
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
NRF_CLOCK->TASKS_LFCLKSTART = 1;
while(NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
// Configure the RTC to run at 1 second intervals, and make sure COMPARE0 generates an interrupt (this will be the wakeup source)
NRF_RTC0->PRESCALER = 0;
NRF_RTC0->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Msk;
// NORDIC: Count to 32767, and not 32768
NRF_RTC0->CC[0] = 1*32767;
NVIC_EnableIRQ(RTC0_IRQn);
// NORDIC: SET IRQ PRIORITY
NVIC_SetPriority(RTC0_IRQn, 0);
NRF_RTC0->TASKS_START = 1;
//--------------------------------------------------------------------------------------------------
void RTC0_IRQHandler(void)
{
// NORDIC: CLEAR TASK AS QUICKLY AS POSSIBLE
NRF_RTC0->TASKS_CLEAR = 1;
// This handler will be run after wakeup from system ON (RTC wakeup)
if(NRF_RTC0->EVENTS_COMPARE[0])
{
NRF_RTC0->EVENTS_COMPARE[0] = 0;
info.tm_sec++;
}
}
//--------------------------------------------------------------------------------------------------
You can use the RTC directly, or you can use the application timer library. You would probably want to make the timer timeout at a regular interval, such as once every second, and calculate the current time based on that.
Note that the accuracy your time depend on the 32 kHz clock source. If you use the internal RC and calibrate it every 4 seconds, the accuracy will be ±250 ppm. That means that the time can be off by as much as ±21.6 s per day, so it may not be accurate enough.