Energia MSP430G2553 Timer interrupts Version 2

Previously I had developed a simple demonstrator of timer interrupts on the MSP430 Launchpad with Energia. This sort of worked but I failed to acknowledge the interrupt in the timer control register so the foreground function (loop) never ran. This version fixes that and tidies up the code a little. In this program the foreground function toggles the red LED and the interrupt service routine toggle the green LED.

#include <msp430.h>
void setup()
{
  // put your setup code here, to run once:
  pinMode(P1_6,OUTPUT); // make the LED pin outpu  
  pinMode(P1_0,OUTPUT);
  setupTimer(1000); // set timer period to 1000 micro seconds  
}

void loop()
{
  digitalWrite(P1_0,HIGH);
  delay(100);
  digitalWrite(P1_0,LOW);
  delay(100);
  
}
void OnTimer()
{
  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
  }

}
void setupTimer(unsigned Period)
{
  // 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=Period*2; // Set TACCR0 = Period*2 (2MHz clock)
  TACCTL0=BIT4; // Enable interrupts when TAR = TACCR0  
}
// 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
  OnTimer();
  TA0CTL &= ~BIT0;     // 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