MSP430 LaunchPad Interrupt vs Polling

From Texas Instruments Wiki
Jump to: navigation, search

by Aldo Briano


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.


                                            Launchpad datasheets InterruptTable.png

                                                                                        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.

Polling Code

//******************************************************************************
// MSP430G2xxx Demo - Software Poll P1.3, Set P1.0 if P1.3 = 1
//
// Description: Poll P1.3 in a loop (assumes active low switch), if low P1.0 
// is set, if high, P1.0 reset. ACLK = n/a, MCLK = SMCLK = default DCO
//
// MSP430G2xxx
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// /|\ | |
// --o--|P1.3 P1.0|-->LED
// \|/
//
// M.Buccini / L. Westlund
// Texas Instruments, Inc
// October 2005
// Built with CCE Version: 4
//******************************************************************************
#include <msp430g2231.h>

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 to output direction

while (1) // Infinite Loop
{
if ((BIT3 & P1IN)) // active low switch
{
P1OUT &= ~0x01; // if P1.3 is 1(not pressed),reset P1.0
}else
{
P1OUT |= 0x01; // else set P1.0
}
}
}


Interrupt Code

//******************************************************************************
// MSP430G2xxx Demo - Software Port Interrupt Service on P1.3 from LPM4
//
// Description: A hi/low transition on P1.3 will trigger P1_ISR which,
// toggles P1.0. Normal mode is LPM3 ~ 0.1uA. LPM4 current can be measured
// with the LED removed, all unused P1.x/P2.x configured as output or inputs
// pulled high or low, and ensure the P1.3 interrupt input does not float.
// ACLK = n/a, MCLK = SMCLK = default DCO
//
// MSP430F20xx
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// /|\ | |
// --o--|P1.3 P1.0|-->LED
// \|/
//
// M.Buccini / L. Westlund
// Texas Instruments, Inc
// October 2005
// Built with CCE Version: 4
//******************************************************************************
#include <msp430g2231.h>

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 to output direction
P1IE |= BIT3; // P1.3 interrupt enabled
P1IES |= BIT3; // P1.3 Hi/lo edge
P1IFG &= ~BIT3; // P1.3 IFG cleared

_BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/interrupt
}

// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
P1OUT ^= 0x01; // P1.0 = toggle
P1IES ^= BIT3;
P1IFG &= ~BIT3; // P1.3 IFG cleared
}


References

  1. MSP430x2xx Family User Guide
  2. MSP430G2x31 Datasheet