External and Pin Change Interrupts

1. Overview

External interrupts and Pin Change (PC) interrupts are features that dedicated peripherals of the CPU will monitor I/O pins and automatically generate interrupts when a level change is detected. There are two External interrupt pins on Ansteron Board (D2 and D3) which can be set to trigger an interrupt with different options including low level detect, any level change, high-to-low and low-to-high. There are also 3 Pin Change (PC) interrupts which will trigger interrupts when level of a pin changes. PC interrupt number 1 can be any pin of group B (pin B0 to pin B5). The other two are group C and group D respectively.

The two External interrupts and three groups of PC interrupts can work independently. However, all pins in each group of PC interrupts will only trigger one interrupt. For example, pin number 3 and 7 of group D (pin D3 & D7) can both be interrupt pins but only one handler for the group will be called when pin-change is detected on either pin.

Interrupt pins can be read just like digital inputs and Handler can check these pins to determine its level as well as what level change has triggered the interrupt.

The library will handle low level hardware setup as well as interrupt routines to provide a simple user interface. Handlers are functions created by users and they will be called by the library when interrupts occur. The library comes in a file name "ext_interrupts.lib". Both library file and source file are in "libs" folder, where Ansteron IDE is.

2. Function

enable_ext_interrupt(int_pin,mode,handler);

The function will enable external interrupt on pin D2 or D3. One of the two identifiers "INT_PIN_D2" or "INT_PIN_D3" can be given to int_pin input. The mode, in other words, the level or edges of signal can be one of the following options:

IdentifierMode
LOW_LEVELThe interrupt will continuously trigger whenever low level on interrupt pin is detected.
LOW_TO_HIGHA transition from low level to high level will trigger interrupt
HIGH_TO_LOWA transition from high level to low level will trigger interrupt
LEVEL_CHANGEAny level change will trigger interrupt.

Input handler is address of handler function. Use operator "&" in front of a function call to get function address ("&handler_function()"). Note that handler function cannot have any input or return value.

Example:

disable_ext_interrupt(int_pin);

This function will disable external interrupt when it is no longer needed. Input int_pin is the same as function above.

enable_PC_interrupt(PCI_pin, handler);

Enable Pin Change Interrupt (PCI) on a pin. Input PCI_pin can be one of the identifiers in table below.

IdentifierPIN
Group B
PCI_PIN_B0B0
PCI_PIN_B1B1
PCI_PIN_B2B2
PCI_PIN_B3B3
PCI_PIN_B4B4
PCI_PIN_B5B5
Group C
PCI_PIN_C0C0
PCI_PIN_C1C1
PCI_PIN_C2C2
PCI_PIN_C3C3
PCI_PIN_C4C4
PCI_PIN_C5C5
Group D
PCI_PIN_D0D0
PCI_PIN_D1D1
PCI_PIN_D2D2
PCI_PIN_D3D3
PCI_PIN_D4D4
PCI_PIN_D5D5
PCI_PIN_D6D6
PCI_PIN_D7D7

There are 3 groups B, C and D corresponding to pin Bx, Cx and Dx of Ansteron Board. There must be one handler for each group. If more than one pin in a group is used as interrupt pin, all of them must be combined using bitwise OR (|) operator before giving to PCI_pin input. See example:

disable_PC_interrupt(PCI_pin);

The function will disable PC interrupt pin when it is no longer needed. Note that when one interrupt pin in a group is disabled, all other pins in the group will be disabled as well.

3. Note

Handler for interrupts should be compact and avoid using delay functions. Global variables can be used to store status and signal bits to be checked each time handler is called instead of waiting for subsequence events.

If other interrupts are triggered when a handler is running, it will be called right after the handler returns.