Periodic interrupts with Energia and the MSP432 Launchpad

Correction on 25th July 2016: Base clock for timer was wrong in original – now fixed.

In a previous post I showed how a periodic interrupt could be generated with the MSP430 Launchpad.  This post shows how it can be done using Energia and the MSP432 (ARM Cortex M4F) Launchpad.  The example uses Timer A3 to generate a periodic (1 kHz) interrupt.  There appears to be some interplay between this and the built-in delay routine however the delayMicroseconds function seems to work fine.  A global counter variable is updated at each interrupt so software can use this to perform additional time delays etc.

Some use is made of lower level library functions that are included with the Energia environment.  This example was compiled with Energia 0101E0017.

/* Periodic interrupt example for the MSP432 development board and the energia development environment */
/* The function OnTimer is called every millisecond and flashes the green LED every second
   The main loop flashes the blue LED every second
   Timer A3 is used to generate the interrupt
*/
 
#include <msp432.h>;
#include <driverlib/timer_a.h>
#include <driverlib/interrupt.h>
 
/*
General notes: serial comms uses eusci_a
PWM uses Timer A0 (at least I think so)
The built-in delay function appears not to work if it expires at the same time as a timer interrupt
The delayMicroseconds appears not to be so affected (so far)
*/
volatile uint32_t millisecondCounter=0;
int count = 0;
volatile int state = HIGH;
volatile int flag = HIGH;
 
void setup()
{
  Serial.begin(9600);
 
  pinMode(BLUE_LED,OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  setupTimer(1000); // set timer period to 1000 micro seconds
}
 
void OnTimer()
{
 
  static int Count = 0;
  static int state = 0;
  Count++;
  if (Count > 1000)
  {
    Count = 0;
    digitalWrite(GREEN_LED,state);
    if (state)
      state = 0;
    else
      state = 1;
  }
}
void loop()
{
 
  digitalWrite(BLUE_LED,HIGH);
  delayMicroseconds(500000);
  digitalWrite(BLUE_LED,LOW);
  delayMicroseconds(500000);
  Serial.println(millisecondCounter);
}
 
void setupTimer(unsigned Period)
{
 
  // Configuration word
  // Bits 15-10: Unused
  // Bits 9-8: Clock source select: set to SMCLK (12MHz)
  // Bits 7-6: Input divider: set to 4
  // 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)
  TA3CTL=0b0000001010010110;
  TA3CCR0=Period*3; // Set TACCR0 = Period (3MHz clock)
  TA3CCTL0=BIT4; // Enable interrupts when TAR = TACCR0
// The following places the address of our interrupt service routine in the RAM based interrupt vector table
// The vector number is 14 + 16  = 30 which is represented by the symbol INT_TA3_0
  Interrupt_registerInterrupt(INT_TA3_0,timerA3ISR); 
 
  // according to the datasheet Table 6-12 timer A3 is on ISR 14
  NVIC_ISER0 = (1<<14); // enable this interrupt in the NVIC
 
}
 
void timerA3ISR(void)
{
  TA3CTL &= ~1;         // Acknowledge the interrupt
  TA3CCTL0 &= ~1;       // Acknowledge the interrupt
  NVIC_ICPR0 = (1<<14); // clear interrupt pending flag in NVIC
  millisecondCounter++;
  OnTimer();
}

 

5 thoughts on “Periodic interrupts with Energia and the MSP432 Launchpad

  1. Cleidir Salvato - BR February 2, 2017 / 10:55 am

    #include driverlib/MSP432P4xx/timer_a.h
    #include driverlib/MSP432P4xx/interrupt.h

    Like

    • fduignan February 2, 2017 / 4:16 pm

      Thanks for the comment Cleidir. It seems to have come in three parts and I’m not clear on how to combine them. Could your resent it as a single comment?
      Thanks again for the feedback.

      Like

  2. Cleidir Salvato - BR February 2, 2017 / 6:23 pm

    Sorry, I’m adapting to the tool. Can you delete my comments? Which I’ll post correctly

    Like

  3. Cleidir Salvato - BR February 2, 2017 / 6:27 pm

    Hello fduignan

    Thanks for making this article available, I could not compile it from the start. I am using enegia 1.6.10e18, to run I execute the following changes:

    before
    #include <driverlib/timer_a.h>
    #include <driverlib/interrupt.h>

    after
    #include <driverlib/MSP432P4xx/timer_a.h>
    #include <driverlib/MSP432P4xx/interrupt.h>

    And I commented the lines

    // NVIC_ISER0 = (1 << 14); // enable this interrupt in the NVIC

    // NVIC_ICPR0 = (1 << 14); // clear interrupt pending flag in NVIC

    Like

  4. fduignan February 2, 2017 / 7:30 pm

    Thanks again Cleidir, its great to have good feedback like this. I’m guessing that some of the directories change place as the tools evolves.

    Like

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