This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I want to keep running RTC even when nrf51 is in sleep mode,is there any way ?

I am running RTC in nrf51, after coming out of sleep mode RTC is getting reset.I want this RTC to keep running so that we can use it as wake up signal.

  • The RTC peripheral will continue to run as normal if you use System On sleep.

    The following code works for me as a 30 second wakeup timer.

    Initialize Timer:

    NRF_RTC0->TASKS_STOP = 1;							// Stop if running for whatever reason
    nrf_delay_us(75);									// We have to wait at least 1 LFCLK tick for stop to take effect.
    
    NRF_RTC0->PRESCALER = 32;							// ~1ms
    NRF_RTC0->CC[0] = 29789;							// 30.0 seconds
    NRF_RTC0->INTENSET = RTC_INTENSET_COMPARE0_Msk;
    NRF_RTC0->TASKS_CLEAR = 1;
    NRF_RTC0->TASKS_START = 1;							// Start timer immediately.
    
    NVIC_EnableIRQ(RTC0_IRQn);							// Enable IRQ
    

    Sleep (if SD Disabled):

    // Wait for interrupt (System ON sleep)
    __WFI();   
    

    Sleep (if SD Enabled)

    sd_app_evt_wait();
    

    And you need the interrupt handler for RTC0

    void RTC0_IRQHandler(void)
    {
    	if ((NRF_RTC0->EVENTS_COMPARE[0] != 0) &&
            ((NRF_RTC0->INTENSET & RTC_INTENSET_COMPARE0_Msk) != 0))
        {
    		// Do stuff here
    
    		
    		// Clear CC flag
    		NRF_RTC0->EVENTS_COMPARE[0] = 0;
        }
    }
    
  • You don't have to configure anything. Just use one of the two sleep functions from my post.

Related