As an Amazon Associate we earn from qualifying purchases.
Timer A is the general-purpose 16-bit timer on many MSP430 chips. It can count clock pulses, raise an interrupt at a set interval and keep your program free to do other work. This article shows four short assembly programs for the MSP430G2x31 family that use Timer A to blink two LEDs, first at a one-second pace and then much faster. If you want to start from the fundamentals, read our MSP430 assembly timer basics first.
How Timer A counts
Every example below sets up Timer A through a few registers. Knowing what each one does makes the code much easier to read.
- TACTL is the main control register. It selects the clock source, the divider, the counting mode and the overflow interrupt.
- TACCR0 holds the value the timer counts up to in up and up/down modes.
- TACCTL0 controls capture/compare channel 0. Setting its CCIE bit enables an interrupt when the timer reaches TACCR0.
- TASSEL_2 chooses SMCLK as the clock source. Here SMCLK comes from the internal DCO.
- ID_0 divides the clock by 1, so the timer runs at the full clock rate.
- MC_1 is up mode. The timer counts from 0 to TACCR0, then returns to 0.
- MC_3 is up/down mode. The timer counts from 0 up to TACCR0, then back down to 0.
- TAIE enables the Timer A overflow interrupt (the TAIFG flag).
Timing formulas
In up mode the timer counts TACCR0 + 1 clock ticks per period, because it includes the step back to zero. In up/down mode one full period is 2 × TACCR0 ticks. Divide the tick count by the clock frequency to get seconds.
All the calculations here assume the DCO runs at about 1.1 MHz. That is a nominal figure. The uncalibrated DCO varies from chip to chip and with temperature, so real timings will differ a little. For accurate time keeping you would calibrate the DCO or use a crystal.
What the shared setup code does
All four programs begin the same way:
mov #(WDTPW|WDTHOLD), &WDTCTLstops the watchdog timer so it does not reset the chip.mov #0x280, SPplaces the stack pointer at the end of RAM on these small devices.mov.b #0x41, &P1DIRmakes P1.0 and P1.6 outputs. On LaunchPad-style boards these pins usually drive the two LEDs.eintturns on interrupts globally, and the main loop simply jumps to itself while the interrupt does the work.
The last lines of each program place the interrupt service routine address in the vector table and set the reset vector at 0FFFEh so the chip starts at 0xF800.
1 second timer in up mode
This program uses the internal DCO as the clock. TACCR0 is set to 1100, so each timer period is 1101 ticks, or roughly 1 millisecond. The interrupt routine counts 999 of those periods in register R5 and toggles the LEDs once the count is reached.
The calculation, assuming DCO = 1.1 MHz:
Time = ((1100 + 1) / 1.1 MHz) × 999 = 0.999908182 seconds
The overflow interrupt (TAIFG) shares the vector at 0FFF0h, so the routine clears the flag itself with bic #1, &TACTL before returning.
.include "msp430g2x31.inc"
org 0xf800
mov #(WDTPW|WDTHOLD), &WDTCTL
mov #0x280, SP
mov.b #0x41,&P1DIR
clr.b P1OUT
clr R5
mov #(TASSEL_2|MC_1|ID_0|TAIE), &TACTL
mov #1100, &TACCR0
eint
jimmy: jmp jimmy
TAISR:
inc R5
cmp #999, R5
jne exit
xor.b #0x41,&P1OUT
clr R5
exit:
bic #1, &TACTL
reti
org 0FFF0h
dw TAISR
org 0FFFEh
dw 0xf800
The LEDs start off and change state about once per second. If you want a different delay, change the 999 comparison value or TACCR0.
High speed blinking using up/down mode
Here the interrupt fires on every timer overflow without a software counter. With TACCR0 = 65000 in up/down mode, one period is 65000 × 2 = 130000 ticks:
Time = (65000 × 2) / 1.1 MHz = 0.1182 seconds
The original version of this article added one extra tick to the result (0.118182727). In up/down mode the period is exactly twice TACCR0, so the difference is tiny and does not matter in practice.
.include "msp430g2x31.inc"
org 0xf800
mov #(WDTPW|WDTHOLD), &WDTCTL
mov #0x280, SP
mov.b #0x41,&P1DIR
mov.b #0x41,&P1OUT
mov #(TASSEL_2|MC_3|TAIE), &TACTL
mov #65000, &TACCR0
eint
jimmyloop:
jmp jimmyloop
jimmyisr:
bic #1,&TACTL
xor.b #41h, &P1OUT
reti
ORG 0FFF0h
DW jimmyisr
ORG 0FFFEh
DW 0xf800
The LEDs toggle about every 0.12 seconds, so one full on-off cycle takes roughly a quarter of a second. Note that the pin register is written as &P1OUT here. The original listing left out the ampersand in one instruction. Absolute addressing is clearer and matches the other lines.
High speed blinking using up mode and CCIFG
This version drops the overflow interrupt and uses the capture/compare interrupt on channel 0 instead. Setting CCIE in TACCTL0 makes the timer interrupt when it reaches TACCR0. This interrupt has its own vector at 0FFF2h, and the CCIFG flag clears automatically when the routine is serviced. The service routine therefore does not need to clear anything.
In up mode the period is 65000 + 1 = 65001 ticks, which is about 0.059 seconds at 1.1 MHz.
.include "msp430g2x31.inc"
org f800h
mov #(WDTPW|WDTHOLD), &WDTCTL
mov #0x280, SP
mov.b #0x41,&P1DIR
mov.b #0x41,&P1OUT
mov #(TASSEL_2|MC_1), &TACTL
mov #CCIE, &TACCTL0
mov #65000, &TACCR0
eint
jimmyloop:
jmp jimmyloop
jimmyisr:
xor.b #41h, &P1OUT
reti
ORG 0FFF2h
DW jimmyisr
ORG 0FFFEh
DW 0xf800
High speed blinking using up/down mode and CCIFG
The last example is the same as the previous one, except the timer runs in up/down mode (MC_3). The channel 0 interrupt now fires once per full up-and-down period of 130000 ticks, which is again about 0.118 seconds.
.include "msp430g2x31.inc"
org 0xf800
mov #(WDTPW|WDTHOLD), &WDTCTL
mov #0x280, SP
mov.b #0x41,&P1DIR
mov.b #0x41,&P1OUT
mov #(TASSEL_2|MC_3), &TACTL
mov #CCIE, &TACCTL0
mov #65000, &TACCR0
eint
jimmyloop:
jmp jimmyloop
jimmyisr:
xor.b #41h, &P1OUT
reti
ORG 0FFF2h
DW jimmyisr
ORG 0FFFEh
DW 0xf800
Comparing the four programs
- Up mode with TAIFG and a counter: about 1 second between toggles.
- Up/down mode with TAIFG: about 0.118 seconds between toggles.
- Up mode with CCIFG: about 0.059 seconds between toggles.
- Up/down mode with CCIFG: about 0.118 seconds between toggles.
The practical difference between the two interrupt types is the flag handling. The TAIFG vector is shared, so you must clear the flag in software or read the TAIV register. The channel 0 CCIFG interrupt has a dedicated vector and clears itself, which makes the routine shorter and is usually the simpler choice for a periodic tick.
Tips for using these examples
- Change TACCR0 to adjust the interval. A larger value gives a slower blink.
- Use the ID bits (for example ID_3) to divide the clock if you want longer delays without a software counter.
- Check that your assembler accepts the
.include,organddwdirectives used here. Other toolchains use different syntax for the same ideas. - For more ways to think about delays, see our related MSP430 timer walkthrough and our notes on timer-based LED blinking.
- If you also work with other microcontrollers, compare this approach with PIC16F877A timers 0, 1 and 2.
FAQ
What is the difference between up mode and up/down mode?
In up mode the timer counts from 0 to TACCR0 and restarts, giving a period of TACCR0 + 1 ticks. In up/down mode it counts up to TACCR0 and back down to 0, giving a period of 2 × TACCR0 ticks.
Why is the one-second timer not exactly one second?
The program counts whole timer periods, so the maths gives 0.9999 seconds, not exactly 1. The DCO is also not a precise clock, so the real interval can drift further depending on the chip and temperature.
Why do I need to clear TAIFG in the interrupt routine?
The overflow interrupt shares its vector with other Timer A sources, so the flag is not cleared automatically in this code. If it stays set, the interrupt would trigger again as soon as the routine returns. The CCIFG flag for channel 0 clears automatically when serviced.
Can I use a different clock source?
Yes. TASSEL selects the source, so you can use ACLK or SMCLK. A watch crystal on ACLK is a common choice when you need accurate long delays.