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 ??

Parents
  • 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

Reply
  • 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

Children
Related