0

If I'm programming a pin change interrupt I can call attachInterrupt at various points in my code and change what function is called when the interrupt occurs.

The overflow and compareA/B interrupt calls are defined by the ISR macro which is evaluated at compile time, which therefore can't be changed at run time. One solution would be to make that function consist of a single line, which invokes a function pointer. If I'm reading the code correctly, this is how attachInterrupt is implemented. Short of mucking with the interrupt vector table (which seems like a bad idea, or at least very hard to debug if you screw it up), is there another way to redefine the overflow or compare interrupt functions?

  • For a arduino uno with a microcontroller of the avr family? They have the vector table in code (in flash), you can not change that runtime. You could make each vector of the vector table point to ram which you could change runtime, but that would be the same as having a ISR that invokes a function pointer. That is not a bad idea, as long as it is clear and well defined, it is a normal way to change the function of an interrupt. For the pin-change-interrupt I suggest the EnableInterrupt: github.com/GreyGnome/EnableInterrupt – Jot Apr 8 at 0:34
  • call attachInterrupt at various points in my code ..... you could set a global variable at various points in your code ...... the ISR would execute a function dependent on value of the global variable – jsotola Apr 8 at 0:46
1

What about using a switch statement inside the ISR function and reroute the flow depending on the value of the global variable used in the switch?

uint8_t status = 0;

void myInterruptFn() {
  switch(status) {
    case 0:
      // do something
      status = 1;
      break;
    case 1:
       // do something else
       status = 0;
       break;
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.