Interrupts.

Hi. I'm wonder if I can attach function on Serial external interrupts. I mean, can I receive data from serial on external interrupt? i don't want to use loops to check data.

The serial funcs are already interrupt-driven.

i don't want to use loops to check data.

Maybe you should tell us why, because otherwise you'll have to write a custom buffering function which is almost certainly more trouble than it's worth.


Rob

Ok, i'll try to explain. In my opinion loops is that thing which loads our chip and it always works. In this case, when I push a lot of code in VOID LOOP(), it will decrease perfomance. And I thought, that interrupts is the best way to receive data from COM, because it will be easily to watch and catch needed data and no need to check data cyclically. I just listen to interrupts and perform needed functions.

It intereseted me, because the syntax of code in Arduino is different, for example, from CodeVision AVR. And this language writting hides from us more importnat things.

In this case, when I push a lot of code in VOID LOOP(), it will decrease perfomance.

But what else will the processor be doing if not looping? If it's not sleeping or crashed it's looping somewhere.

And I thought, that interrupts is the best way to receive data from COM,

In the Arduino environment serial characters are already received using interrupts. The fact that you can write

if (Serial.available() > 3)

means that characters have been received in the background. And while it is possible to write code specific to an application that is more efficient that would require a reasonable amount of programming skill.

Interrupts can make code easier but for novice programmer they are also a source of much trouble, also technically interrupts are LESS efficient than polling as the overhead for an interrupt is quite high.

So in general I would say that interrupts are not worth it, but if you have a specific example maybe.


Rob

also technically interrupts are LESS efficient than polling as the overhead for an interrupt is quite high.

Don't forget that interrupt handlers must be very fast, since the interrupt handler might itself by interrupted. It's best to be done before that can happen.

technically interrupts are LESS efficient than polling as the overhead for an interrupt is quite high.

That's an...interesting point of view.

I suppose it depends on your personal definition of "efficient".

I suppose it depends on your personal definition of "efficient".

That's true.

I should qualify by saying I'm referring to response time and "wasted" time in general. An interrupt will consume maybe up to 100-odd clock cycles to save and restore the processor context, a poll can respond in a couple of cycles.

So maybe "efficient" isn't the correct term, but depending on the program flow 2-3 cycles used every loop may be less wasteful than 100 used every interrupt, for example when receiving characters at high speed you can spend most of you time in the ISR pre and post ambles.

Having said that I use interrupts of one kind or another for just about everything, they can certainly clean up the program logic and solve many spaghetti code issues.


Rob