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

Not able to read correct timer value for nrf9160dk

Hello,

I have been trying to setup timer counter for nrf9160dk using the sample I have attached but for some reason the timer value is always read as 0. Not sure if the timer is not initialised correctly or I am reading the wrong value. I couldn't find any timer sample in the documentation to compare it to.

6712.timer.rar

Thank you.

Regards,

Haris Ahmed

Parents
  • Hi Haris,

    There are two issues with your code:

    • You configure the timer in counter mode instead of timer mode. As you do not have any code for counting anything, I assume you want to use timer mode (count clock cycles). So you must replace NRF_TIMER_MODE_COUNTER with NRF_TIMER_MODE_TIMER.
    • You call nrfx_timer_capture_get(), but this only returns the value of the capture register without actually performing a capture first. You should replace this with a call to nrfx_timer_capture(), which first captures the timer value, then returns it.
  • Dear Einar,

    Thank you for your response.

    Okay, nrf_timer_capture_get() does seem to return clock cycles when I changed NRF_TIMER_MODE_COUNTER with NRF_TIMER_MODE_TIMER.

    But I actually want to use this timer for measuring time in microseconds between two events. What would be the best way to go about this?

    Thank you.

  • Hi,

    harisahmed said:
    Okay, nrf_timer_capture_get() does seem to return clock cycles when I changed NRF_TIMER_MODE_COUNTER with NRF_TIMER_MODE_TIMER.

    Yes and no. nrf_timer_capture_get() just returns the value of the capture register. So you must capture first (using for instance PPI), or use nrf_timer_capture() which both triggers capture and returnes the capture register value. If you only use nrf_timer_capture_get() you will just return whatever is in the capture register, which would be the reset value 0 unless it has been used before.

    harisahmed said:
    But I actually want to use this timer for measuring time in microseconds between two events. What would be the best way to go about this?

    This depends on what you want to time. If you want to time HW events, activity on GPIOs or similar, then it makes sense to use PPI. Then you can for instance start the clock using PPI, and capture using PPI. Or you can leve the clock running and use two capture registers, one for the start time and one for the end time. 

Reply
  • Hi,

    harisahmed said:
    Okay, nrf_timer_capture_get() does seem to return clock cycles when I changed NRF_TIMER_MODE_COUNTER with NRF_TIMER_MODE_TIMER.

    Yes and no. nrf_timer_capture_get() just returns the value of the capture register. So you must capture first (using for instance PPI), or use nrf_timer_capture() which both triggers capture and returnes the capture register value. If you only use nrf_timer_capture_get() you will just return whatever is in the capture register, which would be the reset value 0 unless it has been used before.

    harisahmed said:
    But I actually want to use this timer for measuring time in microseconds between two events. What would be the best way to go about this?

    This depends on what you want to time. If you want to time HW events, activity on GPIOs or similar, then it makes sense to use PPI. Then you can for instance start the clock using PPI, and capture using PPI. Or you can leve the clock running and use two capture registers, one for the start time and one for the end time. 

Children
Related