How can I find out how long my ISR is taking to execute?

I can't work out how to do this. I considered printing the value of OCF2A to serial at the end of my ISR to see if a new interrupt has been flagged, but the serial print command takes a long time to be processed.

You could count the instruction cycles from the disassembled compiled code.
Or toggle a pin at the start and end of the ISR.

But, whatever you do, don't print anything.

Never done it but you might try recording micros() going in and subtracting that from micros() just before exit. Probably negligible but, how to account for the time recording the micros()?

AWOL:
You could count the instruction cycles from the disassembled compiled code.
Or toggle a pin at the start and end of the ISR.

But, whatever you do, don't print anything.

I'd like to toggle a pin, but I don't have an oscilloscope. Maybe I should buy one ......

, but I don't have an oscilloscope

You could use a second Arduino.

dougp:
Never done it but you might try recording micros() going in and subtracting that from micros() just before exit. Probably negligible but, how to account for the time recording the micros()?

That might be tricky. I'll have to check how micros is implemented. At the moment I have my own implementation of millis on Timer2 (which also does some other tasks in the same ISR), and I use Timer1 for something else. I only have Timer0 spare at the moment. I'm running the internal RC clock at 2MHz for low voltage operation, which is why I need to check how long the ISR is taking.

AWOL:
You could use a second Arduino.

Genius!!!

I like that. I will try it and report back.

Just watch out for micros() granularity.

Time it inside the AtmelStudio simulator:

My instinct is that if you have to worry about the time taken in an ISR it needs to be shortened. :slight_smile:

...R

JimEli:
Time it inside the AtmelStudio simulator:

Nice idea, but I'm not going back to Windows for the foreseeable future, and I can't be bothered with VMs.

Robin2:
My instinct is that if you have to worry about the time taken in an ISR it needs to be shortened. :slight_smile:

...R

I agree. However, with a 1000Hz interrupt, and a relatively low clock speed, it's useful to get some definite measurements for how long the code was taking to run.