This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Measuring the time between events using a one shot timer?

I have two program events. I want to measure the time between these events in clock cycles.

I'm able to measure one event, as the timer will start counting up. However, I don't seem to be able to reset it. The values I get out of the timer represent continuous, non stop counting.

Is it possible to reset a one shot timer to zero, so it begins counting again?

I've checked the API documentation and there's no explicit reset command.  I presume I will have to do this another way. But it does seem like a strange omission. On some other processors you can load the timer value directly, so I would just load it to zero. But I can't see a way of writing the value register...

  • Is your use of a, "One Shot" Timer proper/well considered for your measurement application?

    Might it be that the "one shot" is intended to run once (for your programmed period) and then extinguish?

    Suggest instead a timer (perhaps a "wide" one) which can be read (but not stopped) upon both, "Event 1 & Event 2."  The difference between the timer counts logged (event 2 vs. event 1) provides your desired, time delta.  Timer data acquired is stored w/in MCU SRAM for, "after the fact" processing/analysis...

    Note - superior/broader IDEs (IAR is one) provide this feature w/out any need for program code/effort...

  • I did consider using a 64-bit timer, which would give me about 7,311 years of counter time at 80MHz. However I'd still like to know if resetting a one-shot timer manually is possible.

  • Perhaps our use of a Timer config'ed as, "one shot" aids. 

    We pre-configure and load the one-shot - then disable it - as part of our general code initialization.  Then - when the "monitored" program execution space is entered - we enable that one shot.  Code (just below) illustrates:

    ROM_TimerEnable(TIMER1_BASE, TIMER_B);

    Here's our interrupt service:

          ROM_TimerIntClear(TIMER1_BASE, TIMER_TIMB_TIMEOUT);
          GPIO_PORTD_DATA_R &= ~0X10;   // PD4 Lo        
          ROM_TimerDisable(TIMER1_BASE, TIMER_B);

    And here's our set-up/config:


        ROM_TimerDisable(TIMER1_BASE, TIMER_B);   
       
        ROM_TimerConfigure(TIMER1_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_ONE_SHOT);
       
        ROM_TimerLoadSet(TIMER1_BASE, TIMER_B, 25000); // 500uS
       
        ROM_TimerIntEnable(TIMER1_BASE, TIMER_TIMB_TIMEOUT);
       
        ROM_IntEnable(INT_TIMER1B);

    Now this code generates a 500uS pulse output - upon each/every "visit" to this code program segment.

    One "sure way" to achieve "one shot reset" is via the TimerLoadSet() function.  In our case - the one-shot runs to completion - we've no need to reset...  It may prove useful for you to visit this function w/in Periph. Drv. Guide - and see if "TimerDisable()" impacts the timer count. 

    Note that - in the usage above - the one-shot automatically reloads with our config value - w/no effort on our part...

  • Hello All,

    That is a General Purpose Timer in action. If the idea is for it to be counting between two events and capacity for reloading, then a Watch Dog Timer may also server the purpose. DO ensure that the RESETEN bit is clear, so that when it elapses the timer does not reset the device.

    Stopping it midway can be done by using the RCGCWD register in System Control Space and reloading it all the time can be done by writting to the LOAD register of the WatchDog peripheral.

    Regards

    Amit

  • Hi,

    If need to "reset" the timer, you can do it by loading with the prescribed reset value - no special API, just TimerLoadSet.The user manual states also this (One-shot/Periodic Timer paragraph):

    If software updates the GPTMTnILR or the GPTMTnPR register while the counter is counting down, the counter loads the new value on the next clock cycle and continues counting from the new value if the TnILD bit in the GPTMTnMR register is clear. If the TnILD bit is set, the counter loads the new value after the next timeout.  

    Adjust and use as you need - no watchdog to be used, we do no need to reset the device..

    Petrei

  • cb1_mobile said:
    One "sure way" to achieve "one shot reset" is via the TimerLoadSet() function.

    Thanks for the restatement.  Note that one-shot is unlikely to be "best practice" for poster's application.  (that too was noted, w/in original, answering post...)

  • Sorry guys for not updating. I used a 64 bit timer as suggested. I did try to use LoadSet, but the timer did not properly reset - it did allow it to continue counting... but it would always count until the maximum loaded value. I'm not sure if I was missing something or doing something wrong.

    The only disadvantage with the 64-bit method is the maths takes a few more cycles (I presume about 3 cycles for an add or subtract, but possibly only 2?) This has some affect in my very fast ISRs but I was able to work around it.

    I'm rushing to finish a prototype before I go away for summer, so that I can get PCBs made while I'm away.