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

About the use of timer

I want to use the timer to measure the interval between two signals, which requires an accuracy of 1us. But in Timer mode, I don't find a register that can read the current counter value, just like the counter register of RTC.
So I want to ask, is there any way to achieve 1us precision time measurement.

Parents
  • Hi,

    The only peripheral that can give you the resolution you need, is the TIMER as you have found. There is no COUNT register available from software there, but there are several capture registers with tasks associated with them. To time something, you trigger capture and the internal counter value is copied to the capture register. There are several ways to do this. For instance,  you can start the timer on the first signal (either via PPI or SW), and then capture it on the second signal (either via PPI or SW). Alternatively, you can let the timer run and use two capture registers and capture the counter by both signals and calculate the difference (remember to handle the special case where the counter wraps around). This is an example of the second approach:

    void test_timer(void)
    {
        uint32_t sample_1;
        uint32_t sample_2;
        // Configure and start timer:
        NRF_TIMER3->BITMODE     = TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos;
        NRF_TIMER3->MODE        = TIMER_MODE_MODE_Timer << TIMER_MODE_MODE_Pos;
        NRF_TIMER3->PRESCALER   = 0;
        NRF_TIMER3->TASKS_START = 1;
    
        NRF_TIMER3->TASKS_CAPTURE[0] = 1;   // Take first timestamp
        test_function();                    // Measuring the time of this...
        NRF_TIMER3->TASKS_CAPTURE[1] = 1;   // Take second timestamp
    
        sample_1 = NRF_TIMER3->CC[0];       // Read out first timestamp
        sample_2 = NRF_TIMER3->CC[1];       // Read out second timestamp
    
        uint32_t diff_time = sample_2 - sample_1;   // Calculate diff
    
        LOG_INF("test_timer used %d ticks.", diff_time);    // Note that clock ticks at 16 000 000 Hz
    }

  • This is a great technical support!

Reply Children
No Data
Related