Can I break out of an interrupt?

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a ATMega88PA i'm using to control a gearbox, I have two sensors at the moment both of which are digital and use PCINT1. But I want to have the program run as fast as possible and I do not want it to waste time running through code it does not need to, is there a way in which I can break out of the interrupt if a certain condition is met without having it go into the next if statements?

The reason being is if the primary sensor condition is met, the second no longer matters until the next primary condition.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In assembler? sure, just execute an reti at the point you want to break.

If c, then, no. Just jump t the end if the ISR. In c, there is a whole bunch of stuff that is saved on call and restored on exit. If you just jump out, the stuff that should be restored won't, and it will be a mess.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

of course, simply place a "return" statement wherever you want to exit the ISR. This works the same as in any other function. You could also use nested if/else clauses to generate the same effect. Check the generated assembly for the most efficient result with your compiler/version/settings. [change any of the 3, and you'll have to repeat the process]

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
If c, then, no.

Jim, in C you can place a return statement in your code at any point. The compiler will ensure that the proper clean-up is done.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can do conditional jumps to different parts in program as long as you end both pieces of code with RETI or its synonym in your programming language.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But I want to have the program run as fast as possible and I do not want it to waste time running through code it does not need to, is there a way in which I can break out of the interrupt if a certain condition is met without having it go into the next if statements?

OK so what would be the difference between:

ISR(vector_name) {
 if (primary_condition) {
   //primary stuff
   return;
 }
 if (secondary_condition) {
    // secondary stuff
 }
}

and

ISR(vector_name) {
 if (primary_condition) {
   //primary stuff
 }
 else if (secondary_condition) {
    // secondary stuff
 }
}

Remember that there's probably only one epilogue in an ISR so the "return;" is not simply going to equate to a "RETI", it'll like be an RJMP into the epilogue where the pre-ISR machine state is first restored then followed by RETI. So I'm not sure using an early "return" here actually generates more efficient code?