Coding Interrupts
Finally, once you have enabled each individual interrupt, the global interrupt needs to be enabled.
This can be done in a variety of ways. The two most common methods utilize compiler intrinsic
functions:
• __bis_SR_register(GIE) instructs the compiler to set the GIE bit in the Status Register
− bis = bit set
− SR = Status Register
− GIE = which bit to set in the SR
• __enable_interrupts(void) tells the compiler to enable interrupts. The compiler uses the
EINT assembly instruction which pokes 1 into the GIE bit.
Sidebar – Where in your code should you enable GIE?
The short answer, “Whenever you need to turn on interrupts”.
A better answer, as seen in our code example, is “right before the while{} loop”.
Conceptually, the main() function for most embedded systems consists of two parts:
• Setup
• Loop
That is, the first part of the main() function is where we tend to setup our I/O, peripherals, and other
system hardware. In our example, we setup the watchdog timer, power management, GPIO, and
finally the system clocks.
The second part of main() usually involves an infinite loop – in our example, we coded this with an
endless while{} loop. An infinite loop is found in almost all embedded systems since we want to run
forever after the power is turned on.
The most common place to enable interrupts globally (i.e. setting GIE) is right between these two
parts of main(). Looking at the previous code example, this is right where we placed our function that
sets GIE.
As a product example, think of the A/C power adaptor you use to charge your computer; most of
these, today, utilize an inexpensive microcontroller to manage them. (In fact, the MSP430 is very
popular for this type of application.) When you plug in your power adapter, we’re guessing that you
would like it to run as long as it’s plugged in. In fact, this is what happens; once plugged in, the first
part of main() sets up the required hardware and then enters an endless loop which controls the
adaptor. What makes the MSP430 such a good fit for this application is: (1) it’s inexpensive; and (2)
when a load is not present and nothing needs to be charged, it can turn off the external charging
components and put itself to sleep – until a load is inserted and wakes the processor back up.
MSP430 Workshop - Interrupts 5 - 27