Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

First, to understand how timers work, it would be a good idea to look at the timer section in the datasheet (Documentation for Timer A starts on page 462). Here, we can see all the documentation about the registers that interact with Timer A on the microcontroller. From this, we find out that in order to use the timer, we need to set some information in the Timer A control register (TACTLTA0CTL) and handle the interrupts.

...

Code Block
languagecpp
themeConfluence
linenumberstrue
#include <msp430.h> 

/*
 * main.c

*/
int main(void) {
 
  WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer



  P1OUT &= ~BIT0;

   P1DIR |= BIT0;
    TACTLTA0CTL |= TASSEL_2 | MC_2 | ID_3;
    TACCTL0TA0CCTL0 |= CCIE;
    TACCR0TA0CCR0 = 65535;
 
  __enable_interrupt();

 	 for (;;) { }


  return 0;
}

#pragma vector = TIMERA0TIMER0_A0_VECTOR
__interrupt void TIMERA0TIMER0_A0_ISR(void) {
 
  P1OUT ^= BIT0;
}

Explanation

...

First, we setup the Timer A control register (TACTLTA0CTL) to use SMCLK  (TASSEL_2), up-down mode (MC_2), and a frequency divider of 8 (ID_3). Next, we set the Timer A capture/compare control register (TACCTL0TA0CCTL0) to enable interrupts locally for the timer. After that, we set the value we will be counting to by setting TACCR0 TA0CCR0 to 65535 (the maximum value it can store). This will cause the timer to start counting. Lastly, we globally enable interrupts by calling __enable_interrupt and loop forever.

Here at the bottom in the #pragma section, we can see the ISR we created for Port 1 Timer A interrupts. All this service routine does is toggle the state of the LED to make it blink.

...