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

Timer for time measurement

Hallo devzone,

i have a real time task, where i have a trigger, different calculations and then the corresponding action.

Now i need to calculate the time from the trigger as close as possible to the corresponding action.

I started with the app timer which was quite easy.

...

app_timer_init();

app_timer_create();

app_timer_start();

trigger();

var1.start = app_timer_cnt_get();

calc();

var1.stop = app_timer_cnt_get();

var1.diff  = app_timer_cnt_diff_compute(stop,start)

GPIO_set();

But 32kHz / 30µs is quite large, idealy we could count with sysclk and maybe a low prescaler.

So im looking for something like:

nrf_drv_timer_init();

nrf_drv_timer_create();

...

triger();

"nrf_drv_timer_start()"; // from 0

calc():

"nrf_drv_timer_stop();"

var2.diff = "nrf_drv_timer_get()"

"nrf_drv_timer_set(0)"

I managed to add timer, interrupts, Blink LED, count interrupts, but i cant find the right Start Stop and Read functions i need.

With the count interrupts i could build something. But the interrupt is unnecessary and to complex.

I simply want to start a timer from zero, stop the timer, read the time, set him to zero again. 

Parents
  • I found a solution that works with the registers directly.

        // Register Approach
        // Init
        NRF_TIMER3->MODE = TIMER_MODE_MODE_Timer;           // Count up
        NRF_TIMER3->BITMODE = TIMER_BITMODE_BITMODE_32Bit;  // no restiction needed
        NRF_TIMER3->PRESCALER = 0;    // Set CLK Speed to 16 MHz
        NRF_TIMER3->INTENCLR;         // NO interrupts
        // RUN
        NRF_TIMER3->TASKS_CLEAR = 1;
        NRF_TIMER3->TASKS_START = 1;
        nrf_delay_ms(3);
        NRF_TIMER3->TASKS_CAPTURE[1] = 1;
        captured_value = NRF_TIMER3->CC[1];
        NRF_TIMER3->TASKS_STOP = 1;
    
        NRF_LOG_RAW_INFO("Cycles = %u", captured_value);
        NRF_LOG_RAW_INFO("\r\n");
        // RUN again
        NRF_TIMER3->TASKS_CLEAR = 1;
        NRF_TIMER3->TASKS_START = 1;
        nrf_delay_ms(1);
        NRF_TIMER3->TASKS_CAPTURE[1] = 1;
        captured_value = NRF_TIMER3->CC[1];
        NRF_TIMER3->TASKS_STOP = 1;
    
        NRF_LOG_RAW_INFO("Cycles = %u", captured_value);
        NRF_LOG_RAW_INFO("\r\n");

Reply
  • I found a solution that works with the registers directly.

        // Register Approach
        // Init
        NRF_TIMER3->MODE = TIMER_MODE_MODE_Timer;           // Count up
        NRF_TIMER3->BITMODE = TIMER_BITMODE_BITMODE_32Bit;  // no restiction needed
        NRF_TIMER3->PRESCALER = 0;    // Set CLK Speed to 16 MHz
        NRF_TIMER3->INTENCLR;         // NO interrupts
        // RUN
        NRF_TIMER3->TASKS_CLEAR = 1;
        NRF_TIMER3->TASKS_START = 1;
        nrf_delay_ms(3);
        NRF_TIMER3->TASKS_CAPTURE[1] = 1;
        captured_value = NRF_TIMER3->CC[1];
        NRF_TIMER3->TASKS_STOP = 1;
    
        NRF_LOG_RAW_INFO("Cycles = %u", captured_value);
        NRF_LOG_RAW_INFO("\r\n");
        // RUN again
        NRF_TIMER3->TASKS_CLEAR = 1;
        NRF_TIMER3->TASKS_START = 1;
        nrf_delay_ms(1);
        NRF_TIMER3->TASKS_CAPTURE[1] = 1;
        captured_value = NRF_TIMER3->CC[1];
        NRF_TIMER3->TASKS_STOP = 1;
    
        NRF_LOG_RAW_INFO("Cycles = %u", captured_value);
        NRF_LOG_RAW_INFO("\r\n");

Children
No Data
Related