This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
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

How to reset the tick value in application timer?

My code detects an external signal using GPIOTE. Whenever the signal falls, GPIOTE calls a handler, audio_stimulus_handler. The code is shown below. Essentially, after detecting the first fall, it starts the timer and gets the tick value when the subsequent falls occur. After reading 5 falls AUDIO_STIM_MAX_SAMPLES, it returns to the idle mode.

APP_TIMER_DEF(timer_stim_freq_id);

In the main function, 

err_code = app_timer_create(&timer_stim_freq_id,
                                APP_TIMER_MODE_REPEATED,
                                timer_stim_freq_handler);

    void audio_stimulus_handler(void) 
    {
    	uint32_t err_code;
    	switch (audio_stim_case)
    	{
    		case AUDIO_STIM_STATE_IDLE:
    		{
    			// Start the timer
    			err_code = app_timer_start(timer_stim_freq_id, APP_TIMER_TICKS(TIMEOUT_STIM_FREQ, APP_TIMER_PRESCALER), NULL);
          APP_ERROR_CHECK(err_code);
          err_code = app_timer_cnt_get(&prev_tick);
          APP_ERROR_CHECK(err_code);			
    			audio_stim_sample_count = 0;
    			// Move to the next state where the frequency starts being estimated
    			audio_stim_case = AUDIO_STIM_STATE_EVAL_FREQ;
    			break;
    		}
    		case AUDIO_STIM_STATE_EVAL_FREQ:
    		{
    			// Measure the time that has elapsed
    			err_code = app_timer_cnt_get(&curr_tick);
          APP_ERROR_CHECK(err_code);
    			printf("%d, %d\n", audio_stim_sample_count, curr_tick);
    			audio_stim_timer_tick_array[audio_stim_sample_count++] = curr_tick - prev_tick;
    			prev_tick = curr_tick;
    			if (audio_stim_sample_count >= AUDIO_STIM_MAX_SAMPLES)
    			{
    				printf("STOP\n");
    				audio_stim_measurement_done = true;
    				// Stop the timer
    			  err_code = app_timer_stop(timer_stim_freq_id);
            APP_ERROR_CHECK(err_code);
    				// Reset the state to IDLE
    				audio_stim_case = AUDIO_STIM_STATE_IDLE;
    		  }
    			break;
             }
    	}
    	
    }

It prints the tick values to the serial monitor, shown below. It looks like the tick value does not reset to 0 when the timer is stopped.

GPIOTE_INIT starts
GPIOTE_INIT ends
Scan started
0, 89019
1, 89183
2, 89347
3, 89511
4, 89675
STOP
0, 121788
1, 121952
2, 122116
3, 122279
4, 122443
STOP
0, 154556
1, 154720
2, 154884
3, 155048
4, 155212
STOP

My questions are:

  1. Is it possible to reset the tick value whenever I want to?
  2. If so, how do I reset the tick value? Ideally, the tick value is reset when the timer is stopped.
Parents Reply Children
No Data
Related