Timing a section of Code using MS_TIMER variable

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?


unsigned long runningTotal;

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

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.


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.

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?


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.

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.