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

How to call Time() function in Nordic nrf518222(Milliseconds) ??

Hello,

Please tell me, How to call Time() in milliseconds in Nordic nrf51822 ??

  • Hi Rakesh

    As mentioned by Endnode it is quite simple to have one of the TIMER modules return the time elapsed in milliseconds.

    I made a quick example for this based on the template example in the SDK:

    #define MY_TIMER            NRF_TIMER1
    #define MY_TIMER_IRQn       TIMER1_IRQn
    #define MY_TIMER_IRQHandler TIMER1_IRQHandler
    
    static uint32_t my_timer_seconds;
    
    static void my_timer_start(void)
    {
        // Reset the second variable
        my_timer_seconds = 0;
        
        // Ensure the timer uses 24-bit bitmode or higher
        MY_TIMER->BITMODE = TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos;
        
        // Set the prescaler to 4, for a timer interval of 1 us (16M / 2^4)
        MY_TIMER->PRESCALER = 4;
        
        // Set the CC[0] register to hit after 1 second
        MY_TIMER->CC[0] = 1000000;
        
        // Make sure the timer clears after reaching CC[0]
        MY_TIMER->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;
        
        // Trigger the interrupt when reaching CC[0]
        MY_TIMER->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
        
        // Set a low IRQ priority and enable interrupts for the timer module
        NVIC_SetPriority(MY_TIMER_IRQn, 7);
        NVIC_EnableIRQ(MY_TIMER_IRQn);
        
        // Clear and start the timer
        MY_TIMER->TASKS_CLEAR = 1;
        MY_TIMER->TASKS_START = 1;
    }
    
    static uint32_t my_timer_get_ms(void)
    {
        // Store the current value of the timer in the CC[1] register, by triggering the capture task
        MY_TIMER->TASKS_CAPTURE[1] = 1;
        
        // Combine the state of the second variable with the current timer state, and return the result
        return (my_timer_seconds * 1000) + (MY_TIMER->CC[1] / 1000);
    }
    
    static uint64_t my_timer_get_us(void)
    {
        // Store the current value of the timer in the CC[1] register, by triggering the capture task
        MY_TIMER->TASKS_CAPTURE[1] = 1;
        
        // Combine the state of the second variable with the current timer state, and return the result
        return (uint64_t)my_timer_seconds * 1000000 + MY_TIMER->CC[1];
    }
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
        // Enable LED_1
        nrf_gpio_cfg_output(LED_1);
        
        // Enable and start the timer
        my_timer_start();
        
        while (true)
        {
            // Toggle LED_1 every second based on the time value returned by my_timer_get_ms()
            nrf_gpio_pin_write(LED_1, (my_timer_get_ms() / 1000) % 2);
        }
    }
    /** @} */
    
    // Timer interrupt handler
    void MY_TIMER_IRQHandler(void)
    {
        if(MY_TIMER->EVENTS_COMPARE[0])
        {
            MY_TIMER->EVENTS_COMPARE[0] = 0;
    
            // Increment the second variable
            my_timer_seconds++;
        }
    }
    

    Just copy/paste the above code into the template project if you want to try it out.

    Best regards
    Torbjørn

  • Hi

    Yes, this code should run nicely on the nRF52832.

    Looking at the code I think I wrote it for the nRF52832 in the first place, since I set the interrupt priority like this:

    NVIC_SetPriority(MY_TIMER_IRQn, 7);

    On the nRF51822 the lowest IRQ priority (highest number) is 3. 

    The code should work fine on both the nRF51 and the nRF52 series, since the IRQ priority is masked internally before it is set (so 7 will become 3 on the nRF51). 

    Best regards 
    Torbjørn

  • Could you also help me in understanding how do I print the time(with millisecond accuracy) on uart terminal.

Related