Coding Interrupts
Enabling Interrupts
Earlier in the chapter we learned that for the CPU to recognize an interrupt two enable bits must
be set:
• Individual Enable – one IE bit for each interrupt source
• Global Interrupt Enable – GIE is a common “master” enable bit for all interrupts (except
those defined as non-maskable)
In the example below we show the code required to setup a GPIO pin as an interrupt. We chose
to enable the interrupt, as well as configuring the other GPIO pins, in a function called initGPIO();
implementing your code in this way is not required, but it’s how we decided to organize our code.
The key DriverLib function which enables the external interrupt is:
GPIO_enableInterrupt()
You will find that most of the MSP430ware DriverLib interrupt enable functions take a similar
form: <module>_enableInterrupt().
Enabling Interrupts – GPIO Example
#include <driverlib.h>
void main(void) {
// Setup/Hold Watchdog Timer (WDT+ or WDT_A)
initWatchdog();
// Configure Power Manager and Supervisors (PMM)
initPowerMgmt();
// Configure GPIO ports/pins
initGPIO();
// Setup Clocking: ACLK, SMCLK, MCLK (BCS+, UCS, or CS)
initClocks();
//----------------------------------------------------------
// Then, configure any other required peripherals and GPIO
...
__bis_SR_register( GIE );
while(1) {
...
}
void initGPIO() {
// Set P1.0 as output & turn LED on
GPIO_setAsOutputPin (
GPIO_PORT_P1, GPIO_PIN0 );
GPIO_setOutputLowOnPin (
GPIO_PORT_P1, GPIO_PIN0 );
// Set input & enable P1.1 as INT
GPIO_setAsInputPinWithPullUpresistor (
GPIO_PORT_P1, GPIO_PIN1 );
GPIO_clearInterruptFlag (
GPIO_PORT_P1, GPIO_PIN1 );
GPIO_enableInterrupt (
GPIO_PORT_P1, GPIO_PIN1 );
}
__bis_SR_register( GIE );
initGPIO();
Within initGPIO() we highlighted two other related functions in Red:
• GPIO_setAsInputPinWithPullUpresistor() is required to configure the pin as an input. On
the Launchpad, the hardware requires a pull-up resistor to complete the circuit properly.
Effectively, this function configures our interrupt “source”.
• GPIO_clearInterruptFlag() clears the IFG bit associated with our pin (e.g. P1.1). This is not
required but is commonly used right before a call to “enable” an interrupt. You would clear the
IFG before setting IE when you want to ignore any prior interrupt event; in other words, clear
the flag first if you only care about interrupts that will occur now – or in the future.
5 - 26 MSP430 Workshop - Interrupts