External interrupts can be used to trigger routines to run in response to changes in voltage on a pin. Each GPIO pin on the Maple can be used to detect transitions such as when the voltage goes from low to high, or from high to low. This technique can be used to avoid unnecessary polling of the state of a pin.
External interrupts are often used to detect when events happen outside of the microcontroller. These can be used to tell the Maple when events happen, such as when a sensor has data ready to be read, or when a button has been pushed. When such an event happens, an interrupt is raised and the Maple can react to it with a preset interrupt handler.
Every GPIO pin on the Maple can be used as an external interrupt, subject to certain constraints; there can be a maximum of 16 different external interrupts set up at a time on the Maple. This is because the external interrupt lines on the STM32 are multiplexed between GPIO ports. In effect, this means that every pin on the Maple maps to a certain EXTI line, and within that EXTI line, only one of the pins that maps to it can be used as an external interrupt at a time.
The following table shows which pins can be used on which lines.
EXTI Line | Maple pins |
---|---|
EXTI0 | 2, 15, 27 |
EXTI1 | 3, 16, 28 |
EXTI2 | 1, 17, 25 |
EXTI3 | 0, 18 |
EXTI4 | 10, 19 |
EXTI5 | 4, 13, 20 |
EXTI6 | 5, 12, 35 |
EXTI7 | 9, 11, 36 |
EXTI8 | 6, 14, 37 |
EXTI9 | 7, 25, 28 |
EXTI10 | 8, 26, 29 |
EXTI11 | 30 |
EXTI12 | 31 |
EXTI13 | 21, 32 |
EXTI14 | 22, 33 |
EXTI15 | 23, 34 |
Note
You should set the pin mode of your desired pin to an input mode (e.g INPUT or INPUT_FLOATING, INPUT_PULLUP, INPUT_PULLDOWN).
Attach an interrupt handler to the given channel.
This interrupt handler will be called when the timer’s counter reaches the given channel compare value.
The argument should be a function which takes no arguments and has no return value; i.e. it should have signature
void (*handler)(void);
Note: The function (often called an interrupt service routine, or ISR) should attempt to return as quickly as possible. Blinking the LED, some logic, PWM updates, and Serial writes are fine; writing to SerialUSB or waiting for user input can take a long time and other compare interrupts won’t fire. Tip: if you have a delay() in your interrupt routine, you’re probably doing it wrong.
Parameters
the channel to attach the ISR to, from 1 to 4.
The ISR to attach to the given channel.
Remove the interrupt handler attached to the given channel, if any.
The handler will no longer be called by this timer.
Parameters
the channel whose interrupt to detach, from 1 to 4.
The kind of transition on an external pin which should trigger an interrupt.
Values:
To trigger an interrupt when the pin transitions LOW to HIGH.
To trigger an interrupt when the pin transitions HIGH to LOW.
To trigger an interrupt when the pin transitions from LOW to HIGH or HIGH to LOW (i.e., when the pin changes).
Blink the LED on every transition:
int pin = 13;
volatile int state = LOW;
void setup() {
pinMode(pin, OUTPUT);
pinMode(0, INPUT_FLOATING);
attachInterrupt(0, blink, CHANGE);
}
void loop() {
digitalWrite(pin, state);
}
void blink() {
state = !state;
}
STMicro documentation for STM32F103RB microcontroller:
- Datasheet (pdf)
- Reference Manual (pdf)