Lab 8
Gettings Started with the MSP430 - Using Energia (Arduino) 8 - 31
Lab 8e – Using TIMER_A
9. Create a new sketch and call it Interrupt_TimerA
File New
File Save As…
C:\msp430_workshop\<target>\energia\Interrupt_TimerA
10. Add the following code to your new sketch.
#include <inttypes.h>
uint8_t timerCount = 0;
void setup()
{
pinMode(RED_LED, OUTPUT);
TA0CCTL0 = CCIE;
TA0CTL = TASSEL_2 + MC_2;
}
void loop()
{
// Nothing to do.
}
__attribute__((interrupt(TIMER0_A0_VECTOR)))
void myTimer_A(void)
{
timerCount = (timerCount + 1) % 80;
if(timerCount ==0)
P1OUT ^= 1;
}
In this case, we are not using the attachInterrupt() function to setup the interrupt. If you
double-check the Energia reference, it states the function is used for ‘external’ interrupts. In
this case, the MSP430’s Timer_A is an internal interrupt.
In essense, though, the same three steps are required:
a) The interrupt source must be setup. In our example, this means seting up TimerA0’s
CCTL0 (capture/compare control) and TA0CTL (TimerA0 control) registers.
b) An ISR function – which, in this case, is named “myTimer_A”.
c) A means to hook the interrupt source (trigger from TimerA0) to our function. In this case,
we need to plug the Interrupt Vector Table ourselves. The GCC compiler uses the
__attribute__((interrupt(TIMER_A0_VECTOR))) line to plug the Timer_A0 vector.
Note: You might remember that we introduced Interrupts in Chapter 5 and Timers in
Chapter 6. In those labs, the syntax for the interrupt vector was slightly different from
what we are using here. This is because the other chapters use the TI compiler.
Energia uses the open-source GCC compiler, which uses a slightly different syntax.