# Timing a section of Code using MS\_TIMER variable

**URL:** <https://forums.digi.com/t/timing-a-section-of-code-using-ms-timer-variable/10539>\
**Category:** Rabbit\
**Created:** [November 2, 2010, 5:53pm UTC](https://forums.digi.com/t/timing-a-section-of-code-using-ms-timer-variable/10539 "2010-11-02T17:53:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eastpoint](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@eastpoint](https://forums.digi.com/u/eastpoint)\
**Post date:** [November 2, 2010, 5:53pm UTC](https://forums.digi.com/t/timing-a-section-of-code-using-ms-timer-variable/10539/1 "2010-11-02T17:53:31Z")

</div>

Hello,  
I would like to keep a running total of how long my ISR takes to execute. However, my runningTotal variable below always prints zero even after I have call the ISR several times. Anybody see what I am doing wrong?

Thanks

//global  
unsigned long runningTotal;

main()  
{  
printf(“Running total time is %d”, runningTotal);

}

debug root interrupt void buttonPress\_isr\_S2()  
{  
unsigned long startTime;  
unsigned long endTime;

```
startTime = MS_TIMER;
.....
//do something
....

endTime = MS_TIMER;

runningTotal = runningTotal + (endTime - startTime);

```

} //ISR

---

<div class="post-metadata">

**Author:** ![petermcs](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@petermcs](https://forums.digi.com/u/petermcs)\
**Post date:** [November 2, 2010, 8:26pm UTC](https://forums.digi.com/t/timing-a-section-of-code-using-ms-timer-variable/10539/2 "2010-11-02T20:26:51Z")

</div>

A couple of points here:

First, the millisecond timer may not be working whilst your interrupt service routine is executing if you do not enable interrupts (you need to look at the interrupt priorities to know for certain).

Second your isr will need to execute for at least one millisecond on average to chalk up any time this way - even if it was less than 1mS I would expect the timer to roll over occasionally whil the isr executes but only if you sort out point 1.

Regards,  
peter

---

<div class="post-metadata">

**Author:** ![eastpoint](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@eastpoint](https://forums.digi.com/u/eastpoint)\
**Post date:** [November 3, 2010, 12:55am UTC](https://forums.digi.com/t/timing-a-section-of-code-using-ms-timer-variable/10539/3 "2010-11-03T00:55:46Z")

</div>

Thank you for the reply. You are correct. It looks like the MS\_TIMER variable does not update while inside of an interrupt. This leaves me a little lost as the best way to time the execution of an interrupt.

---

<div class="post-metadata">

**Author:** ![petermcs](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@petermcs](https://forums.digi.com/u/petermcs)\
**Post date:** [November 3, 2010, 7:23am UTC](https://forums.digi.com/t/timing-a-section-of-code-using-ms-timer-variable/10539/4 "2010-11-03T07:23:15Z")

</div>

You could re enable interrupts within your ISR but this cre es other potential problems. In particular, the millisecond timer ISR (which actually occurrs every 500 microseconds but only updates the time every second call) will perform other house keeping tasks which will impact on your ISRs execution time.

You could look at using a spare port pin and an oscilloscope to measure the time taken by toggling the pin at the start and end of the ISR.

You could use one of the timers in the Rabbit processor to time things.

On general principal you should aim to do as little as possible within an ISR so as to reduce the impact on other ISRs. The normal way to do this is to set some sort of flag within ISR which the main application checks and then process tha data the ISR has gathered.

Is there something time consuming that absolutly has to be done within your ISR?

Regards,  
Peter

---

<div class="post-metadata">

**Author:** ![eastpoint](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@eastpoint](https://forums.digi.com/u/eastpoint)\
**Post date:** [November 4, 2010, 1:45am UTC](https://forums.digi.com/t/timing-a-section-of-code-using-ms-timer-variable/10539/5 "2010-11-04T01:45:46Z")

</div>

Thanks for the reply.

Using a spare port pin and a scope is probably the best method.

Unfortunately, I do not have access to one right now. I tried using the command “VdInit();” inside the ISR to start the MS\_TIMER but the variable does not increment. Is there something else I need to do to enable it?

Your statement “You could use one of the timers in the Rabbit processor to time things.” What other timers can I used?

Thanks for the info. I really appreciate it.

---

<div class="post-metadata">

**Author:** ![petermcs](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@petermcs](https://forums.digi.com/u/petermcs)\
**Post date:** [November 4, 2010, 7:50am UTC](https://forums.digi.com/t/timing-a-section-of-code-using-ms-timer-variable/10539/6 "2010-11-04T07:50:43Z")

</div>

You need to look at the section in the processor manual on setting and restoring interrupt priorities to see how to enable interrupts within youor ISR so that the MS\_TIMER can run - but remember it will increase the time taken to execute your ISR if it has to carry out other tasks.

Depending on which processor you are using, you could set up TIMER B or TIMER C with a slow clock input and read the value at the start and end of your ISR. As long as your ISR did not take longer than the time for one complete cycle of the timer you can calculate the execution time by comparing the values. Have a look at the manual for your processor.

Regards,  
Peter
