Getting Started with MSP430 Timers – Part 2

Getting Started with MSP430 Timers – Part 2
Timer Image

Specifics of MSP430 Timers

Important References

From here on, we’ll be referencing the datasheet a fair bit, so download the following documents from the MSP430G2553 product page:

  • MSP430x2xx Family User’s Guide
  • MSP430G2x53 datasheet

The family user’s guide is the document we’ll refer to mostly – it covers all the detail of how to use the microcontroller. The datasheet contains specifics of what functionality is implemented on the individual microcontrollers – useful for example to see pin mapping.
Take a look at the user guide contents, and you’ll note there are two timers: Timer_A and Timer_B. Our microcontroller only has a Timer_A, but it’s worth remembering that others may have a Timer_B. Timer_B is basically the same as Timer_A, but has a few extra features.

Timer Specifications

We need to quickly dive into some specs on the MSP430G2553 Timers. There are two instances of Timer_A that you can use (i.e. Timer_A is not actually a single timer, but two timers – 0 and 1). The naming is a little confusing: Timer0_A3 and Timer1_A3. Why the “3” on the end? That refers to the number of interrupts that each timer can trigger (these are called capture/compare registers, which we’ll get into soon).

The timers can operate off the ACLK (Auxiliary Clock), SMCLK (Sub-main Clock) or an external clock source (TACLK or INCLK). We’ll use the ACLK for our examples, as it still operates in low power modes and runs off the slower VLOCLK (or an external watch crystal).

The Timers, like the MSP430 controllers themselves, are 16-bit timers. What does this mean to us? It means that we can count up to 16 bits – i.e. 2^16, or 65,535 (in hex, this is 0xFFFF). Remember the frequency discussion? If we run using a clock frequency of 12kHz (the default for the ACLK), it means the counter can count up to 5.46 seconds (65,535 divided by 12,000). If we were to run the timer off the SMCLK at a 1MHz frequency, we can only count up to 0.05 seconds (or 50 ms) – not that useful.

Triggering Interrupts: The Capture/Compare Registers

These tricked me a bit when I started out with Timers. Each Timer on the MSP430G2553 has 3 CCR’s (Capture/Compare Registers). Remember the naming of the timers – Timer0_A3 and Timer1_A3? The “3” refers to the number of CCR’s per timer.

What exactly is a capture/compare register? It allows us to choose whether we want to operate in either capture or compare mode – for now we’ll start by looking at the “compare” functionality.

In Compare mode, the CCR allows us to set the “alarm” that we mentioned earlier, and trigger an interrupt – in other words it compares the current value of the counter to the “alarm” value that has been set. There are 3 CCRs (CCR0, CCR1, CCR2), so we can set 3 different alarms.

For example, say that we want to flash an LED every second. We’re running off the ACLK (Auxiliary Clock) at a frequency of 12kHz. This means that one second passes every 12,000 clock cycles. In order to blink the LED, we write a value of 12,000 into the CCR (as the “alarm”), and write code to handle the interrupt. Each time the counter reaches 12,000 it triggers the interrupt which then flashes the LED. It’s that simple. Well, there’s one extra bit of complexity – Timer Modes. Before we get to Timer Modes, we need to look at the types of timer interrupts.

Timer Interrupts

There are two types of interrupt that the Timers generate.

The first is an interrupt that fires whenever the timer counts to the value in CCR0 (the first Capture/Compare Register). CCR0 gets special treatment as we’ll see a little later. The interrupt flag that is set here is TACCR0-CCIFG

The second type is a general interrupt that fires when:

  • the timer counts to the values in either of the other 2 CCRs (i.e. CCR1 and CCR2). The interrupt flag that is set here is TACCR1-CCIFG and TACCR2-CCIFG
  • The timer overflows past 0xFFFF (65,535) and back to zero. The interrupt flag that is set here is TAIV-TAIFG

We need to do a little bit more work on the second option to work out which of the CCRs triggered the interrupt, but luckily it’s straightforward.
You’re probably comfortable with the concept of interrupt flags. Each of the CCRs has a flag to show whether an interrupt was triggered.

Additionally, there is a general interrupt register that tells us if CCR1 or CCR2 generated an interrupt and a flag to indicate an overflow from 0xFFFF to zero.

A Final bit of Theory: Timer Modes

I promise, after this last bit of theory we’ll get stuck into an example that does something! The MSP430 Timers can count in 3 different ways, or modes: Continuous mode, Up mode, or Up/Down mode.

Continuous Mode

The timer starts at zero, and counts (once per clock cycle) all the way up to 65,535 (or 0xFFFF). If CCR0 (the “alarm”) is set, the timer triggers an interrupt but keeps on counting up. When it reaches OxFFFF it overflows and returns to zero, just to start counting up again.

Continuous Mode Timer
Continuous Mode Timer
    Interrupt Flags:

  • If CCR0 is set, then an interrupt is generated and the TACCR0-CCIFG flag is set when the timer reaches CCR0. However the timer will continue to count past CCR0 up to 0xFFFF.
  • When the timer overflows, it generates an interrupt and sets the TAIV-TAIFG interrupt flag.

Up Mode

The timer starts at zero and counts up to the value that you specify in CCR0. On the next count it restarts again from zero.

Up Mode Timer
Up Mode Timer
    Interrupt Flags:

  • When the timer counts to CCR0, it generates an interrupt and sets the TACCR0-CCIFG flag.
  • When the timer resets to zero, it generates an interrupt and sets the TAIV-TAIFG flag

Up/Down Mode

The timer starts at zero and counts up to the value that you specify in CCR0. It then counts back down to zero, and starts the cycle again.

Up/Down Mode Timer
Up/Down Mode Timer

    Interrupt Flags:

  • When the timer counts to CCR0, it generates an interrupt and sets the TACCR0-CCIFG flag.
  • When the timer counts back to zero, it generates an interrupt and sets the TAIV-TAIFG flag

From this you can see that the period between interrupts is double that in Up Mode – as the timer counts all the way back down to zero, rather than resetting at zero.

Continue to Part 3…

About

A few years ago I discovered that my programming skills could be used to make real-world things move and flash and beep - I haven't looked back since! I love sharing what I've learned along this pretty bumpy (but very exciting) journey into the world of embedded systems - and that's why this website exists. In addition to the website, I write a series for Nuts & Volts magazine that helps readers to move beyond the Arduino and into the world of AVR. I'm a dad with a couple of energetic youngsters and a really cool wife!

5 Comments on “Getting Started with MSP430 Timers – Part 2

Leave a Reply

Your email address will not be published.

*