Using timer interrupts with the Energia/MSP430 IDE.

I was curious whether you could run interrupts within Energia code on the TIMSP430 – guess what? You can. Here’s some code that toggles the Green LED during an interrupt service routine. The RED LED is toggled in the main program loop. The code runs on the MSP430 Launchpad with the MSP430G2553 microcontroller.

#include <msp430.h>
void setup()
{
  // put your setup code here, to run once:
  pinMode(P1_6,OUTPUT); // make the LED pins outputs
  pinMode(P1_0,OUTPUT);
  // Configuration word
  // Bits 15-10: Unused
  // Bits 9-8: Clock source select: set to SMCLK (16MHz)
  // Bits 7-6: Input divider: set to 8
  // Bits 5-4: Mode control: Count up to TACCRO and reset
  // Bit 3: Unused
  // Bits 2: TACLR : set to initially clear timer system
  // Bit 1: Enable interrupts from TA0
  // Bit 0: Interrupt (pending) flag : set to zero (initially)
  TA0CTL=0b0000001011010010; 
  TACCR0=2000; // Set TACCR0 = 2000 to generate a 1ms timebase @ 16MHz with a divisor of 8
  TACCTL0=BIT4; // Enable interrupts when TAR = TACCR0

  
}
void loop()
{
  // put your main code here, to run repeatedly:
  digitalWrite(P1_0,HIGH);
  delay(100);
  digitalWrite(P1_0,LOW);
  delay(100);
}
// The address function that follows this vector statement is placed in the specified location Interrupt Vector table 
#pragma vector=TIMER0_A0_VECTOR
__interrupt  void timerA0ISR(void)
{
// Timer A0 Interrupt service routine
  static int msCount=0;// Count milliseconds to allow a 1 second pulse
  static int state=0;  // Remember LED state for toggling purposes
  msCount++;
  if (msCount >= 1000)
  {
    msCount = 0;
    digitalWrite(P1_6,state); // Write to LED
    state=~state;             // toggle state
  }
  TA0CTL &= ~BIT0;     // Updated Dec 2015. Acknowledge the interrupt
}

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