Home > Uncategorized > Understanding Timers in MSP430 #3

## Understanding Timers in MSP430 #3

May 17th, 2012

We now define an interrupt vector. When a timer interrupt occurs, it runs the code defined in the interrupt routine. Take a look at the Interrupt routine.

#include <msp430x14x.h>
``` void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT P6DIR |= 0x08; // P6.3 Set P6.3 to output CCTL0 = CCIE; // CCR0 interrupt enabled CCR0 = 60000; TACTL = TASSEL_2 + MC_2; // SMCLK, contmode _BIS_SR(GIE); // Enable interrupt }```

// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
P6OUT ^= 0x08; // Toggle P6.3
CCR0 += 60000; // Add Offset to CCR0
}

If you run this code you and observe the signal, you get a screen shot something similar to Some Experiments

Here are some experiments you can do.

1. Try changing

CCR0 += 60000;

to

CCR0 += 30000;

and you will observe that time period becomes half. Changing it to CCR0 += 6000; will reduce it to 1/10th.

2. If you want a slower timer, you may change the

TACTL = TASSEL_2 + MC_2;

to

TACTL = TASSEL_1 + MC_2;

Which will change the clock from SMCLK ( derived from 4 MHz) to ACLK ( derived from 32.768 KHz).
3. If you change

TACTL = TASSEL_2 + MC_2 ;

to

TACTL = TASSEL_2 + MC_2 + ID_1;

it doubles the time period ( by by halving the clock period used for timer interrupt). Bit 7-6 defines it.

IDx Bits

00 divide by 1
01 divide by 2
10 divide by 4
11 divide by 8