MSP430 - Timer Module

The MSP430G2553 has everything we need, including an internal clock, in order to make a decent timer. This is the first of two solutions I will present.

This chip also has a low frequency, low power crystal oscillator circuit. So if we really want to do this properly we might as well go all the way and install the 32768Hz clock crystal.

That very same crystal was included in the LaunchPad kit in a tiny plastic package. I hope you didn't toss that away but kept it in a safe place. We are going to need it.

Solution #1 - Using the built-in oscillator, timer module and interrupts

Here is where the fun really begins. We will program the timer module to create interrupts at fixed intervals.

Timer Module

I will present the first timer example code followed by explanations.

// Timer Example without interrupts
// 2013.04.25 - MrChips
#include "io430.h"

#define ON 1
#define OFF 0

#define LED1 P1OUT_bit.P0
#define LED2 P1OUT_bit.P6

void init(void)

  // Stop watchdog timer to prevent time out reset

  P1OUT = 0x00;
  P1DIR = 0xFF;

  // initialize Timer0_A
  TA0CCR0 = 62500;  // set up terminal count
  TA0CTL = TASSEL_2 + ID_3 + MC_1; // configure and start timer

void main( void )
  while (1)
    while (TA0CCTL0_bit.CCIFG == 0);  // wait for overflow flag
    TA0CCTL0_bit.CCIFG = 0;          // reset flag
    LED1 = ~LED1;                     // toggle LED1
The MSP430 family features a number of timer modules. You have to check what is available on each specific chip.

To confuse things, there are different styles of timers, Timer_A and Timer_B.

On the MSP430G2553 there are two timers Timer0_A and Timer1_A. And to add to the confusion, a default timer is given a generic name which defaults to Timer0_A. I have not used the generic names to avoid this confusion.

Timer0_A is a 16-bit timer. The actual timer register is named TA0R.

TA0CCR0 is the capture/compare register which is used as the terminal count when counting UP.

TA0CTL is the control register used to configure the timer. We set three fields (or functions) as follows:

TASSEL_2 selects the internal 1MHz clock (SMCLK).
ID_3 sets the clock division to 8. Hence one timer clock cycle is 8μs.
MC_1 sets the count mode to count up.
In this mode, the timer will count for 62500 x 8μs = 500000μs or 0.5s before restarting at zero.

In the main loop, we wait for the flag CCIFG to go high which happens when the terminal count is reached. We then reset the CCIFG bit to zero and then toggle LED1.

The operator ~ takes the complement of the value.

Hence LED1 will be on for 0.5s and off for 0.5s.
(On my oscilloscope the time measured is 0.49s and this can be fine tuned if a more accurate time is required.)

Note that the operator to test for equality is ==.

A common error in C programming is to mistake the = assignment operator for the == equality test.

I will show the timer example using interrupts in the next blog post.

Blog entry information

Last update

More entries in General

More entries from MrChips

Share this entry