MSP430 Interrupts

Using Interrupt

All practical embedded software will have interrupts. It is time we learn. It is indispensible to almost commercial embedded. You will thank me that you were told to learn interrupt.

Concept of Interrupt

Consider the LED blinking example in the previous example. Also assume that the delay is kept substantially high, say, 5 seconds. In that case, the program is stuck into the delay loop. Now assume that we want our software to capture some real time event in those 5 seconds, for example, it wants to capture change in the level of an Input button. With this program it will miss it. With the use of Interrupts we can solve the problem.

We can generate an Interrupt based upon the change in the level of the IO pin. The program will execute normally. But as soon as there is a change in the input Pin level, there is an interrupt. The program jumps to the memory location pointed to by ther interrupt. It does tasks as programmed in the interrupt location and comes back and continues the normal execution.

Similar to the IO pin interrupt we have timer interrupt. The idea is to create an interrupt at defined amount of time. The program keeps executing normally. At interval defined by the timer interrupt parameter, it jumps to the interrupt service routine, completes the task and comes back.

In this tutorial we will learn timer interrupt. Go ahead and copy paste the following code in the IAR embedded system. As before Replace the msp430x14x.h and the IO pin corresponding to your hardware. In this particular example there is an LED connected to Port P5.0 and a push button is connected at Pin P1.0. Pin P5.0 is used as an output pin and Pin P1.0 is used as an input pin. If you have an LED connected to, for example P2.3, then replace statement P5DIR |= BIT0 with P2DIR |= BIT3 and similarly the P5OUT lines.

#include "msp430x14x.h"

void main(void)
P5DIR |= 0x01; // P5.0 output
CCTL0 = CCIE; // CCR0 interrupt enabled
CCR0 = 60000;
TACTL = TASSEL_2 + MC_2; // SMCLK, contmode

_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt

// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
P5OUT ^= 0x01; // Toggle P5.0
CCR0 += 60000; // Add Offset to CCR0

If we run the program freely, it will make the LED blink. The same result as our previous LED blinking program. But the approach is different. If you want to learn the details of what CCTL0, CCIE, CCR0,TACTL, TASSEL_2 , MC_2, their assignments and values mean, please refer to Chapter 11 of slau049.pdf file from Texas Instruments. It has details of timer and interrupt.