Detach interrupt

Does the detachInterrupt() function detach both hardware and software interrupts?

detachInterrupt() take an interrupt number (or pin number for some boards) as its argument so you can only detach interrupts that you have attached with attachInterrupt()

Thanks

And the way we know this is we looked at the code - its open source!

I actually had a lot of pain in my rear recently with attachInterrupt() and detachInterrupt(). If you attach an interrupt to some function, call it interruptAlpha(), and then at some point you want to detach the interrupt, you have to be aware that the hardware interrupt still triggers (don't worry, the function is not called). What this means is that if you ever decide to re-attach that interrupt to interruptAlpha(), if at any time during the time between when you detached and re-attached the interrupt that pin was triggered, then the software interrupt will immediately be called on re-attachment (probably not what you want). You have to put a special line of code in to clear the hardware interrupt flags in order to prevent it from automatically triggering the software attached function upon re-attachment.

I apologize if that made absolutely no sense.

cypherrage:
I actually had a lot of pain in my rear recently with attachInterrupt() and detachInterrupt(). If you attach an interrupt to some function, call it interruptAlpha(), and then at some point you want to detach the interrupt, you have to be aware that the hardware interrupt still triggers (don't worry, the function is not called). What this means is that if you ever decide to re-attach that interrupt to interruptAlpha(), if at any time during the time between when you detached and re-attached the interrupt that pin was triggered, then the software interrupt will immediately be called on re-attachment (probably not what you want). You have to put a special line of code in to clear the hardware interrupt flags in order to prevent it from automatically triggering the software attached function upon re-attachment.

I apologize if that made absolutely no sense.

Makes a lot of sense. Detaching a user interrupt can be complex in the context of the whole application. Might be better to just leave the interrupt enabled all the time and have a global variable flag set or reset by the main process, so the ISR function can check if it should do something or not instead of the whole attach/detach process.

Lefty

Arduinos don't have any software interrupts. There is no INT instruction.

However, there are ways to trigger a hardware interrupt by using software. Here's what it says in the data sheet:

The External Interrupts are triggered by the INT0 and INT1 pins or any of the PCINT23...0 pins. Observe that, if
enabled, the interrupts will trigger even if the INT0 and INT1 or PCINT23...0 pins are configured as outputs. This
feature provides a way of generating a software interrupt.

If I read the code right, interrupts that can be captured by attachInterrupt() and detachInterrupt() are limited to external interrupts INTx that aren't official pin change interrupts. You can do pin change interrupts, but only using INTx instead of PCINTx. You can't use attachInterrupt to do any timer interrupts, or ADC interrupts, or watchdog interrupts... only INTx. If you want to do any other interrupts, you have to use ISR() or use another system.

TanHadron:
Arduinos don't have any software interrupts. There is no INT instruction.

However, there are ways to trigger a hardware interrupt by using software. Here's what it says in the data sheet:

The External Interrupts are triggered by the INT0 and INT1 pins or any of the PCINT23...0 pins. Observe that, if
enabled, the interrupts will trigger even if the INT0 and INT1 or PCINT23...0 pins are configured as outputs. This
feature provides a way of generating a software interrupt.

If I read the code right, interrupts that can be captured by attachInterrupt() and detachInterrupt() are limited to external interrupts INTx that aren't official pin change interrupts. You can do pin change interrupts, but only using INTx instead of PCINTx. You can't use attachInterrupt to do any timer interrupts, or ADC interrupts, or watchdog interrupts... only INTx. If you want to do any other interrupts, you have to use ISR() or use another system.

You can imitate one by writing to an interrupt pin :\ . A little bootleg and probably not too terribly useful, but I'm sure it gets the job done.

Interrupts are usually for when you don't know when something will happen, exactly. Like, when a button will be pressed.

If you know you want a software interrupt right now, so you can call function X, why not just call function X yourself and skip the intermediate step of the interrupt?

Software interrupts are usually used in an operating system where you may or may not know the address of the function you are calling. In the old DOS days, for example, you did an INT21 to do a DOS request and you didn't have to link anything to find out where that address mapped to. That allows you to load programs with an underlying operating system already running, without linking to addresses. Linux does INT 80 requests for system calls. It's quite useful in those situations.

Even on the Arduino, it would be a nice way to use boot loader functions. If the boot loader had a function to write to flash, for example, and had a published interface so I could call that function easily, I would use it.