MSP430 LaunchPad Interrupt vs Polling

One of the most important microcontroller programming techniques to understand is interrupt service routines. In this article, I will try to give a brief overview of the differences between interrupts and polling, and the advantages.

Related Projects

Quick Start

Read about the differences between Interurpt and Polling

Download both code samples

Analyze the different programming schemes

Enjoy!

Theoretical Background

Polling is to constantly check or “poll” something. This can be applied in software to any user interface where the microcontroller can poll the buttons to verify if there are any user inputs.On the other hand, Interrupts use hardware to implement a more efficient way of letting the firmware know of any occurring events.

Interrupts are like a telephone. Consider these two situations. You are at your house and you are expecting a call. You could periodically grab the telephone every 5 seconds and check if there is anyone in the line calling you. Yet, that would mean that you would waste a lot of time polling the telephone line without any success. On the other hand, you can continue whatever you are doing and when the telephone rings you receive an interrupt. Once it rings, you leave whatever you’re doing and take care of the telephone (you service the telephone interrupt).

This is completely analogous to a microcontroller. Once a microcontroller receives an interrupt request it suspends its current execution and jumps to the interrupt service routine, and once finished, continues at its last executed instruction (in the main routine). The MSP430G2xxx provides various interrupt sources which are detailed in page 10 of the datasheet. The interrupts I use the most are the port, timer, and USI (serial communication) interrupts.

Table 1: MSP430G2xx1 Interrupt Table from page 10 of datasheet

It is a common technique to do as little processing as possible in interrupt service routines, mostly avoiding loops. Remember that they can occur at any time, you want to resume your original task as soon as possible. Most people limit interrupt routines to set flags for processing later in the main routine. Nevertheless, the style employed depends on the application.

The main advantage of interrupts is it allows for more efficient and elegant code. Through polling there are many clock cycles spent unnecessarily.

Programming

In the MSP430, you must remember to enable any interrupt you want to use. For example, all ports have a PxIE (interrupt enable) register that will control whether the interrupt is acknowledged. Similarly, there is a global interrupt flag (GIE) in the SR register which must be set to allow any non-maskable interrupts (NME: ports, timers, etc) to occur.

The interrupts are great for low power applications because if programmed correctly they can wake-up device in low power mode.

Development

These two examples were taken from the MSP430G2xxx zip code examples. Some minor modifications for port pins were made.