Saturday, October 27, 2012

Using Interrupts With the Arduino

Good explanation of Interrupts with the Arduino from Dave Auld

The volatile keyword is added to the state variable, this causes the compiler to use RAM instead of a storage register. This is done because the storage register can be temporarily inaccurate if they are being modified by areas other than the main thread. In the Arduino, this relates to code being triggered by interrupts.

The attachInterrupt(param1, param2, param3) requires 3 parameters, these are;
param1 = Which interrupt to listen for. This is the Interrupt Number not the Digital In number
param2 = Which code function to call, this must be a method that takes no parameters and returns no value.
param3 = Which condition to watch for.

The Arduino can listen for 4 types of condition changes. These are;
LOW = The input is at a LOW state
RISING = The input state changes from LOW to HIGH
FALLING = the input state changes from HIGH to LOW
CHANGE = The input state changed from HIGH to LOW or LOW to HIGH, i.e. has changed its state

ReAssigning Interrupts
Interrupts can be changed at any point by using the attachInterrupt() method. As soon as this is done, any previously assigned interrupt on the associated pin is removed.

Starting / Stopping Interrupts
The Arduino also has the ability to temporarily ignore all the interrupts. You may want to do this if you have some sensitive code that must be executed without interruption. In this case you would issue a noInterrupts() call. Once your sensitive code block has completed, interrupts can be restarted by calling interrupts().

Removing Interrupts
Interrupts can also be removed by using the detachInterrupt(interrupt_number) method.

No comments:

Post a Comment