Max interrupts / second

Hi Arduino,

I would know the maximum number of interrupts I can read with an Arduino Mega 2560. I guess should be something less than 16 Millions / second, since the clock speed is 16 Mhz.

Do you have this information? I have a project and I am reading, sometimes, until 3kHz. I guess that I can read more than that, right?

Thanks,

A. :)

I guess should be something less than 16 Millions / second, since the clock speed is 16 Mhz.

Do you know what an interrupt is? How they work?

Hardware exception, caused by an external hardware. Commonly used to read pulses or similar. ISR are used to read and make something with that.

How many of them can I read per second?

Not 16 million a second, that's for sure, or anywhere close to that number. Think about it.

To answer your question, as always, it depends on what "reading an interrupt" involves.

All depends how much you need to do to get your data in that ISR.

The limit is 16 million clock cycles per second, and it takes a few to get into and out of the ISR. Plus you need time outside of the ISR to do something with all that data you've been gathering.

If you're just doing pulse lengths, look into using timer1 for Input Capture - it does some of the work for you.

Try 30kHz and see how much CPU is left for other things…

Entering an ISR involves delaying for the existing instruction to finish (1 to 4 cycles or so),
saving the stack pointer (2 RAM accesses), a jump, then all the code in the ISR to save
and restore registers, and its reti instruction (which has to restore values from the stack
and jump).

Expect more like ~50 cycles minimum for any realistic scenario. That’s several microseconds
not counting any time for executing your code in the body of the ISR!

For the reasons @MarkT so clearly outlined you can probably read an I/O pin with polling much more frequently (using PINx) than with an interrupt.

Of course the interrupt "reacts" to the external event whereas polling does not know it happened until after it has read the pin - and polling might miss a short-lived event.

...R

TLERDRDEN:
I would know the maximum number of interrupts I can read with an Arduino Mega 2560. I guess should be something less than 16 Millions / second, since the clock speed is 16 Mhz.

Do you have this information? I have a project and I am reading, sometimes, until 3kHz. I guess that I can read more than that, right?

Right. The overhead for saving the controller registers, calling and executing the interrupt handler function and restoring the controller registers is something like 60 machine cycles. So you can do a rough estimation.

The total duration of an interrupt handler is something like (60 cycles + duration of your code).

If you also consider an interrupt latency of a few cycles and if your ISR code is doing something really simple like setting a flag, or increment a byte variable, you can calculate with perhaps 80 machine instructions per interrupt, which will lead to an absolute max of:
16000000/80 = 200000 interrupts per second
(or much less if your ISR code is doing more than “isrCount++;” with a variable declared “volatile byte isrCount;”)

But this will slow down your normal code to zero execution: If all the controller cycles are used for handling interrupts, so no machine instructions are available for normal code, and the system is at stagnation then and normal code does not execute any longer.

I have a project and I am reading, sometimes, until 3kHz. I guess that I can read more than that, right?

Let's turn the question on its head. How many readings per second do you need to take and how long does the input stay in its changed state ?

Okey,

Thanks guys for your answers. I'm sorry , maybe I did not ask exactly what I wanted. So, what I was trying ask is if somebody has a calculation of how many interrupts can a Mega 2560 read per second with an optimized ISR.

@AWOL, I know that less than 16 Million per second, but let´s say that the first limitation is the clock speed.

@MarkT, @DrAzzy OK.

@Jurs, sounds good, this could be a good estimation, 200000 interrupts per second ( I can read from an Instrument until 200 kHz pulses).

@UKHeliBob, just 500 Hz, but could be until 3KhZ.

My ISR just reads from a pin, and counts the time of each pulse (using micros() function), so I think it will be able to do it with no problems, right?

Thanks guys!!

A. :)

My ISR just reads from a pin, and counts the time of each pulse (using micros() function), so I think it will be able to do it with no problems, right?

Presumably not all of the processing is within the ISR. It would be interesting to see how you are doing it.

TLERDRDEN: @Jurs, sounds good, this could be a good estimation, 200000 interrupts per second ( I can read from an Instrument until 200 kHz pulses).

Count 200K pulses per second with interrupt handling while slowing down normal code to zero speed?

I doubt that will be good programming practice for any kind of application. Perhaps you better set up a hardware counter to count pulses at such high speed instead of invoking an interrupt handler service routine on each pulse.

You are right, thanks!

TLERDRDEN: My ISR just reads from a pin, and counts the time of each pulse (using micros() function), so I think it will be able to do it with no problems, right?

I hope you mean, "records the time of each pulse".