This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using timer how to make 5 sec delay in pca10040 board?

Hi,

I am using pca10040 board. i need to toggle a led in 5sec once. I tried to change the prescale value. But im not able to get that much time delay.

Can any one help me on this?

  • Have you tried using app_timer? It uses the RTC which will give you overflow at 512 seconds with prescaler 0. The RTC is also much more power efficient than using TIMER. See here for a tutorial on how to use it. You can also use the RTC directly, but be aware that RTC0 is used by SoftDevice and RTC1 is used by app_timer.

  • BTW you can use TIMER to get 5 second delay. Using prescaler 9 and 32 bits the overflow value will be 2^32 / 16MHz * 2^9 = 137439 seconds.

  • Hi Ole Bauck,

    Thank you for your valuable answers. Now i can able to do time delay whatever i need.

  • Hi Ole Bauck,

    If i set prescale value 9 and bitmode is 32 means. The counting will be start in 0 to 4,294,967,296. For 5 second delay i set cc[0]=156250. For my requirement i need continuously toggle a led for 5 mins. At very first time its coming in 5 second [0 - 156250] but after that its taking more time [156270 - 4,294,967,296] I dont kw how to do that..

    can u guide me on thz.. below is my function:

    void start_timer(void)
    {
            NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer;
            NRF_TIMER2->TASKS_CLEAR = 1;
            NRF_TIMER2->PRESCALER = 9;
            NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_32Bit;
            NRF_TIMER2->CC[0] = 156250;
            NRF_TIMER2->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos) ;
            NVIC_EnableIRQ(TIMER2_IRQn);
            NRF_TIMER2->TASKS_START = 1;
    }
    
    void TIMER2_IRQHandler(void)
    {
            if ((NRF_TIMER2->EVENTS_COMPARE[0] != 0))
            {
                    NRF_TIMER2->EVENTS_COMPARE[0] = 0;                                      
                    NRF_TIMER2->CC[0] += 156250;
                    nrf_gpio_pin_toggle(GPIO_TOGGLE_PIN);
            }
    }
    
  • You can use shorts:

    NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);
    

    This will clear the timer counter value on CC[0] compare. If not doing this the timer will count to the maximum value before it overflows.

Related