UPDATE!
3rd of March 2018
The code in this example has been updated. See here:
https://wordpress.com/post/ioprog.com/1628
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 ; #include #include /* 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(); }