MSP430 LaunchPad LED Timer

From Texas Instruments Wiki
Jump to: navigation, search

by Aldo Briano

Timer A is used to blink the LED. After every overflow, the timer A generates an interrupt which is used to blink the LED. Since the timer is clocked by SMCLOCK (running at approx. 1Mhz), 8 overflows are counted to approximate blinking every 0.5 seconds. 

Related Projects

Quick Start

  • Create new Project
  • Download Code
  • Enjoy!



// MSP430 Timer Blink LED Demo - Timer A Software Toggle P1.0 & P1.6
// Description; Toggle P1.0 and P1.6 by xor'ing them inside of a software loop. 
// Since the clock is running at 1Mhz, an overflow counter will count to 8 and then toggle
// the LED. This way the LED toggles every 0.5s. 
// ACLK = n/a, MCLK = SMCLK = default DCO 
// MSP430G2xx
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | P1.6|-->LED
// | P1.0|-->LED
// Aldo Briano
// Texas Instruments, Inc
// June 2010
// Built with Code Composer Studio v4
#include <msp430g2231.h>

#define LED_0 BIT0 
#define LED_1 BIT6
#define LED_OUT P1OUT
#define LED_DIR P1DIR

unsigned int timerCount = 0;
void main(void)
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
LED_DIR |= (LED_0 + LED_1); // Set P1.0 and P1.6 to output direction
LED_OUT &= ~(LED_0 + LED_1); // Set the LEDs off

TACTL = TASSEL_2 + MC_2; // Set the timer A to SMCLCK, Continuous
// Clear the timer and enable timer interrupt


__bis_SR_register(LPM0 + GIE); // LPM0 with interrupts enabled


// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
timerCount = (timerCount + 1) % 8;
if(timerCount ==0)
P1OUT ^= (LED_0 + LED_1);

Theoretical Background

Timers are an important feature of microcontrollers. The MSP430 included in the LaunchPad features a 16-bit timer/counter with two capture/compare registers. It supports capture/compare, pulse width modulation (PWM), and interval timing. To understand how timer works and a detailed explanation of MSP430 timer registers please read MSP430x2xx Family User Guide. There are four timer modes as depicted below in the table.

                                               Launchpad datasheets TimerModes.png

                                                                                  Read about the timer modes in page 12-5 of the user guide

Basically, a timer uses a clock to increment a counter. This counter can be configured to count to a max value, overflow, start at a specific value, and generate interrupts or output according to the application. These are configured through the timer modes of operation.

Timer A counts up to a max of = 65536 counts since it is 16-bit (216)

                                                                   Simple formula to remember:              Launchpad equation time.PNG


This example uses the timer A clocked from SMCLOCK. This clock is based on the MCU’s current frequency of 1Mhz. In this case we want the LED to toggle every 0.5s. Using the formula above, we can calculate how to program the timer. The number of counts results in 524,288, which is 8 times 65,536.

The timer will generate an overflow interrupt and reset once it reaches 65,536. Since we want to count up to 524,288, after every 8 overflow interrupts, the LED will be toggled.

Future Ideas

  • Use an external clock source
  • Count using the Up mode instead of Continuous
  • Program two LEDs to toggle at different speeds using the two compare registers


  1. MSP430x2xx Family User Guide
  2. MSP430G2x31 Datasheet