RSS

Interrupting msp430!!!

12 Sep

The key feature of msp is the lower power mode which requires us to know how to put it to sleep and wake it. This is one main reason why we should and must know interrupts in msp.The interrupts can be classified into three types as:

  • System Reset
  • Maskable NMI
  • Non maskable NMI

The system Reset is of types

  • Power up clear (PUC)
  • Power on Reset (POR)

POR is associated with powering up of the device and a reset signal on RST/NMI pin. PUC is triggered by a POR and also watchdog timer and security violation (unauthorised memory access by programs).

Maskable interrupts are caused by peripherals and are enabled or disabled by the GIE(global interrupt enable) bit in Status register.The different maskable interrupts are

  • External interrupts
  • Timer Interrupts
  • Serial
  • ADC …etc.

The Non maskable interrupt refers to an interrupt which when enabled is not disabled by the GIE bit (global interrupt enable) in Status register.There are basically three different NMI’s.

  1. an edge on RST/NMI pin
  2. oscillator fault
  3. memory access violation

Each interrupt is associated with a corresponding Interrupt service routine which is located in a different location based on the interrupt.In C programming we define the ISR by using the directive #pragma and using keyword vector.The executed interrupt depends on the value of the vector.For example if we need to use the interrupts for the PORT1 pins then the vector should be declared as

#pragma vector=PORT1_VECTOR
__interrupt void isr(void)
{}

The different vector s can be found in the header file.They are

************************************************************
 * Interrupt Vectors (offset from 0xFFE0)
 ************************************************************/
 #define PORT1_VECTOR                (2 * 2u)  /* 0xFFE4 Port 1 */
 #define PORT2_VECTOR               (3 * 2u)  /* 0xFFE6 Port 2 */
 #define USI_VECTOR                     (4 * 2u)  /* 0xFFE8 USI */
 #define ADC10_VECTOR               (5 * 2u)  /* 0xFFEA ADC10 */
 #define TIMERA1_VECTOR          (8 * 2u)  /* 0xFFF0 Timer A CC1, TA */
 #define TIMERA0_VECTOR         (9 * 2u)  /* 0xFFF2 Timer A CC0 */
 #define WDT_VECTOR                  (10 * 2u) /* 0xFFF4 Watchdog Timer */
 #define NMI_VECTOR                   (14 * 2u) /* 0xFFFC Non-maskable */
 #define RESET_VECTOR              (15 * 2u) /* 0xFFFE Reset [Highest Priority] */
***************************************************************

By replacing the value of the vector the function will serve as the ISR for that particular interrupt.If PORT1_VECTOR is replaced with NMI_VECTOR then the ISR will run on the ocurence of NMI and not PIN interrupt.

All the maskable interrupts have to be enabled first before they can be used.To use the interrupts first we have to set the global interrupt enable bit in SR.This can be done by using the intrinsic function

__EINT();     if this function is not used then even if we enable individual interrupts they wont function.

External Interrupts On GPIO

Each port is associated with PxIE , PxIES , PxIFG registers which are used to control the interrupts.The interrupt on a particular pin is enabled by setting the particular bit in PxIE.ex:

P1IE=BIT3;             //enebles interrupt on pin 3

The PxIES is used to select if we need rising edge or the falling edge of the signal to trigger the interrupt.setting a bit makes the interrupt on that pin to be triggered on a rising edge and resetting makes it a falling edge triggered.

0= rising edge

1= falling edge

When an interrupt is triggered on a pin, the bit corresponding to that pin is set.These bits have to be manually reset once the ISR has been served.

Given below is  a code which uses a port pin as an interrupt to toggle the led.

#include"msp430x20x2.h"
main()
{
 WDTCTL=WDTPW+WDTHOLD;        //STOP WATCHDOG TIMER

P1DIR=BIT0+BIT6;              // MAKE PIN 0 AND PIN 3 AS OUTPUTS.
P1OUT=BIT0;                   //MAKE PIN 0 HIGH
P1IE=BIT3;                    //ENABLE THE INTERRUPT FOR THE PIN 3
P1IES&=~BIT3;                 // TRIGGER INTERRUPT FOR RISING EDGE
_EINT();                      //ENABLE GLOBAL INTERRUPT ; SET GIE BIT IN SR

while(1)
{};
}

#pragma vector=PORT1_VECTOR     //ISR FOR PORT1
__interrupt void P1(void)
{
P1OUT^= (BIT0+BIT6);            //TOGGLE
P1IFG&=~BIT3;                   //CLEAR THE INTERRUPT FLAG FOR PIN3

}

Timer Interrupt

I’ll explain the controlling of a timer later but at the moment we should know that by giving the vector value as TIMERA1_VECTOR the ISR will function on an interrupt when timer overflows.


NMI Interrupt on RST/NMI

To use RST/NMI as an interrupt pin we have to set two bits.One in WDTCTL register and one in IE1 register.The oscillator fault is triggered when there is an error in the oscillator.It is used to switch toDCO when the crystal oscillator is not present.

By modifying the WDTNMIES bit in WDTCTL we can change the triggering edge of the NMI.

The following code initializes the pin and toggles the led on interrupt.

#include"msp430x20x2.h"
main()
{
  IE1=NMIIE;                    //enable nmi
  WDTCTL=WDTPW+WDTHOLD+WDTNMI;  //select nmi function on RST/NMI
  P1DIR=BIT0+BIT6;
  P1OUT=BIT0;
  while(1)
  {};
}

#pragma vector=NMI_VECTOR
__interrupt void nmi(void)
{
 P1OUT^= (BIT0+BIT6);

 IFG1&=~NMIIFG;                      //clear nmi interrupt flag
 IE1=NMIIE;                          // enable nmi
 WDTCTL = WDTPW + WDTHOLD+WDTNMI;    // select nmi function on RST/NMI

}

Its weird that in the above code interchanging the last 2 lines in ISR will not set the RST/NMI pin nmi mode.
Advertisements
 
1 Comment

Posted by on September 12, 2011 in Flip, MSP430 Tutorials

 

Tags: , ,

One response to “Interrupting msp430!!!

  1. Saurabh

    February 5, 2014 at 8:16 pm

    Hey Suraj,

    I understood everything! Thanks for that. But I could not get “#pragma vector”. Why it is use? How to write an ISRs?

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: