As an Amazon Associate we earn from qualifying purchases.
Timer A is the main 16-bit timer on the MSP430G2x31 family. It lets you measure time, create delays and blink LEDs without wasting CPU cycles in software loops. This article shows three assembly programs that use Timer A with an interrupt: continuous mode, a one second delay built on continuous mode, and up mode. Each one comes with the timing calculation behind it.
If you are new to the series, start with the digital I/O basics and the article on interrupts and internal pull-up resistors, because the examples below rely on both.
How Timer A works
Timer A has a 16-bit counter register, TAR, that increments on every tick of the selected clock. The control register, TACTL, decides how it behaves. The examples in this article use these fields:
- TASSEL_2 selects SMCLK as the clock source. By default SMCLK comes from the internal DCO.
- ID_0 divides the clock by 1, so the timer counts at the full clock speed.
- MC_2 is continuous mode: the counter runs from 0 to 0xFFFF, then wraps to 0.
- MC_1 is up mode: the counter runs from 0 up to the value in TACCR0, then restarts.
- TAIE enables the timer interrupt, which fires when the TAIFG flag is set.
All the calculations here assume the DCO runs at about 1.1 MHz, which is the typical default after reset. The exact frequency differs between chips and with temperature, so real timings will be close to the numbers below but not identical.
Timer A in continuous mode
In continuous mode the counter takes 0x10000 (65536) ticks to overflow. At 1.1 MHz this gives:
Time = (0xFFFF + 1) / 1.1 MHz = 0.059578 seconds
That is roughly 17 overflows per second, which is fast enough that the LEDs look like they are blinking at high speed. The program sets P1.0 and P1.6 as outputs (mask 0x41), starts the timer with its interrupt enabled, and then idles in a loop. Each overflow runs the interrupt service routine, which toggles both pins.
.include "msp430g2x31.inc"
org 0xf800
mov.w #(WDTPW|WDTHOLD), &WDTCTL
mov.w #0x280, SP
mov.b #0x41,&P1DIR
clr.b P1OUT
mov.w #(TASSEL_2|MC_2|ID_0|TAIE), &TACTL
eint
jimmy: jmp jimmy
TAISR:
xor.b #0x41,&P1OUT
bic.w #1,&TACTL
reti
ORG 0FFF0h
DW TAISR
ORG 0FFFEh
DW 0xf800
What the code does
- The watchdog is stopped and the stack pointer is set to 0x280, the top of RAM on this device.
- P1DIR is loaded with 0x41, which makes P1.0 and P1.6 outputs. P1OUT is cleared so both start low.
- TACTL starts the timer from SMCLK in continuous mode with the overflow interrupt enabled.
- eint turns on global interrupts. Without it the timer would count but the ISR would never run.
- The main program loops forever. All the work happens in the ISR.
- In the ISR, xor.b toggles the LEDs and bic.w #1,&TACTL clears TAIFG (bit 0) so the interrupt does not fire again immediately.
- The vector table entry at 0FFF0h points to the ISR, and the reset vector at 0FFFEh points to the start of the program.
1 second timer in continuous mode
A single overflow is too short for a visible one second delay. The simple fix is to count overflows in a register and act only on every Nth one. Here 17 overflows are counted:
Time = ((0xFFFF + 1) / 1.1 MHz) × 17 = 1.012829 seconds
The result is close to one second but not exact, because 17 whole overflows is the nearest multiple available. The LEDs toggle once per interval, so one full on-and-off blink takes about two seconds.
.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_2|ID_0|TAIE), &TACTL
eint
jimmy: jmp jimmy
TAISR:
inc R5
cmp #17,R5
jne exit
xor.b #0x41,&P1OUT
clr R5
exit:
bic #1, &TACTL
reti
ORG 0FFF0h
DW TAISR
ORG 0FFFEh
DW 0xf800
How the counter works
Register R5 is cleared at start-up and used as an overflow counter. On every interrupt the ISR increments R5 and compares it with 17. If the values differ, the jne instruction jumps to exit, where the flag is cleared and the routine returns. When R5 reaches 17, the LEDs are toggled and R5 is reset to zero. Note that the flag is cleared on both paths, which is important.
Timer A using up mode
Up mode gives finer control. The counter runs from 0 to the value in TACCR0 and then returns to 0, so you choose the period directly instead of being stuck with 65536 ticks. The period is TACCR0 + 1 ticks, because the count includes zero. With TACCR0 set to 51000:
Time = 51001 / 1.1 MHz = 0.046365 seconds
This version blinks even faster than the continuous mode example, as the shorter period suggests.
.include "msp430g2x31.inc"
org 0xf800
mov #(WDTPW|WDTHOLD), &WDTCTL
mov #0x280, SP
mov.b #0x41,&P1DIR
clr.b P1OUT
mov #(TASSEL_2|MC_1|ID_0|TAIE), &TACTL
mov #51000, &TACCR0
eint
jimmy: jmp jimmy
TAISR:
xor.b #0x41,&P1OUT
bic #1, &TACTL
reti
org 0FFF0h
dw TAISR
org 0FFFEh
dw 0xf800
The only differences from the first program are MC_1 instead of MC_2 and the extra write to TACCR0. In up mode the TAIFG flag is set when the counter rolls back from TACCR0 to 0.
Choosing a mode
- Use continuous mode when you want a free-running counter, or when a fixed period of 65536 ticks is acceptable.
- Use up mode when you need a specific period. Calculate TACCR0 as (desired time × clock frequency) − 1.
- Use the ID bits to divide the clock when you need longer periods without a software counter.
Improving timing accuracy
The DCO is an internal oscillator, so it is not precise. If your project needs accurate time, load the factory calibration values for a known DCO frequency, or clock the timer from an external watch crystal if your board has one. The same code structure works either way, and only the calculation changes.
For more Timer A examples and variations, see the related posts. When you are ready to continue, the next timer tutorial builds on these programs, and the series then moves on to arithmetic operations in assembly.
FAQ
Why do I need to clear TAIFG in the interrupt routine?
TAIFG stays set after an overflow until it is cleared. If you leave it set, the CPU re-enters the ISR as soon as it returns. The examples clear it by writing to bit 0 of TACTL.
Why is the 17-overflow delay not exactly one second?
One overflow lasts about 0.0596 seconds at 1.1 MHz, and 17 of them add up to 1.0128 seconds. The DCO frequency is also only approximate. Up mode with a tuned TACCR0 value can get closer.
What is the difference between continuous and up mode?
Continuous mode counts from 0 to 0xFFFF before wrapping. Up mode counts from 0 to the value in TACCR0, so you set the period yourself.
Why is the interrupt vector at 0FFF0h?
On this device the Timer A overflow flag shares the interrupt vector at 0FFF0h. The DW directive stores the address of the ISR there so the CPU knows where to jump.