MSP430 Tutorial – Using Interrupt
Most of the practical embedded software will use interrupts at some point of time. Interrupts are indispensible to almost all the commercial emdedded software. Believe me, if you will thank this tutorial, that it taught you about interrupts – one of the most widely used concepts in embedded programming.
Concept of Interrupt
To understand the concept of interrupts, consider the LED blinking code in the previous example. Assume that the delay in that example is kept substantially high, let us say, 5 seconds. When the delay is kept as high as 5 seconds, the program is stuck into the delay loop while excuting. This creates some problem and real issue in practice. Assume that we want our code to capture some real time event in those 5 seconds, for example, it may wants to capture a push button action. With this code, it is very likely that it will miss capturing the input button press event. The program is stuck in the delay loop while you pressed the input button. The problem can be solved using the interrupt buttons.
We “generate” and interrupt when there is a change in the level of the IO pins. The program will continue to execute normally, but, as soon as the button is pressed, the interrupt is generated. As soon as the interrupt is generated, the normal execution of the program halts. We write a code – as to what needs to be done once the interrupt occurs. The program completes the interrupt and comes back to the normal execution fo the program.
We also have timer interrupts in a fashio similar to the IO pin interrupt. In timer interrupt, 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 particular tutorial, we will learn timer interrupt. 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.
WDTCTL = WDTPW + WDTHOLD; // Stop Watchdog
P5DIR |= 0x01; // Port P5.0 is 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
__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. For a detailed description of CCTL0, CCIE, CCR0,TACTL, TASSEL_2 , MC_2, their assignments and values, please refer to Chapter 11 of slau049.pdf file from TI. It has details of timer and interrupt.