Home > Uncategorized > MSP430 Interrupt Tutorial

MSP430 Interrupt Tutorial

In this post we have tried to explain the Concept of Interrupt using real world example. Let us assume that you want to monitor the port Pin P1.1 using interrupt. Whenever, the P1.1 changes from low to high and interrupt occurs. When that happens, the P3.4 is toggled.

We have tried to keep the code to minimal possible. Here is goes.

#include <msp430x14x.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop Watch Dog Timer
P3DIR |= 0x10; // P3.4 set to output
P1IE |= 0x02 ; // Interrupt on Input Pin P1.1
_BIS_SR(GIE);
}

#pragma vector = PORT1_VECTOR
__interrupt void InterruptVectorPort1()
{
P3OUT ^= 0x10; // Toggle P3.4
P1IFG &= ~0x02; // Clear Interrupt Flag
}

Now connect a source of input to P1.1. Here is a screen shot when P1.1 and P3.4 were captured ( P1.1 in blue and P3.4 is in Red).

Notice that P3.4 changes only on the rising edge of the clock.  There is no interrupt on the falling edge of the clock. What if you want to generate an interrupt of the falling edge of the clock ? What if you want to generate interrupt on the falling as well as rising edge ? We will cover it in the next post. Before that we want to mention and important thing - Clearing the interrupt flag. Notice the statement

P1IFG &= ~0x02; // Clear Interrupt Flag

If you do not give this statement , you will get scopeshots something like

Notice how P3.4 keeps toggling not only at the rising edge, but also at regular intervals. Why ? Because, we have not cleared the interrupt flag. So it keeps entering the interrupt since interrupt Flag is not cleared. That brings us to an important rule.

"Clear the interrupt once you have entered into the Interrupt Service Routine".

 

 

Uncategorized

  1. No comments yet.
  1. No trackbacks yet.