Getting Started with MSP430 Timers

Understanding MSP430 Timers

I found timers confusing! I spent my time reading datasheets, watching YouTube clips, reading technical tutorials, re-reading datasheets and still felt like I was fumbling in the dark. Read on as I explore the basics of Timers, at first in general and then specifically for TI’s MSP430 microcontrollers – a great range of low-power MCUs.

What the heck is a Timer?

If you’re reading this, you probably have an idea of what a timer is, and want to know more. A timer is essentially a counter – every time the CPU clock “ticks”, the counter increases (or decreases) its count. Like all things microcontroller-ish, the count is stored in a register (the Timer Register).

That’s great – we can count clock ticks – so what? Well, a classic timer allows you to trigger an interrupt when a certain value is reached – think of it like an alarm clock. If I set the timer’s “alarm” to a count of 10,000 then an interrupt will fire when the timer counts to 10,000. This is very useful as it lets us make things happen at (pretty) accurate, regular intervals.

We’ll look at how we achieve this in just a moment.

Tick-Tock, the Timer and the Clock

We said that the timer counter increases on each clock cycle. Which clock, though? As you know, many microcontrollers (the MSP430 included) have more than one clock – each driven by different sources, and running at different speeds. You can choose which of these the timer gets its input from. The challenge is choosing the best clock for your timer to run off – you need to choose one:

  • with the speed most suited to what you’re doing
  • with an appropriate degree of accuracy
  • which does/doesn’t operate in any low power modes you’ll want to use

In most cases it’ll work to choose a slow clock that still runs when the microcontroller is in low power mode.

For example, when flashing an LED you want to be able to flash it relatively slowly (i.e. in the millisecond to second timescales), without creating any overload on the CPU (allowing the CPU go to sleep), and probably don’t need a super-accurate crystal-based clock (an extra nanosecond here or there won’t matter).

By the way, many microcontrollers can also use an external source to drive the timer’s counter, by counting the toggle cycles on a GPIO input pin. We won’t cover the details of that for now.

Timer Intervals

If your timer interacts with humans, “time” is probably a more useful measure than clock cycles. You would probably want an LED to flash once a second, rather than once every 12,000 clock cycles. The same would hold true if you want to communicate with another system at a set speed (baud rate). You need to do a very simple calculation to convert the microcontroller’s clock cycles into time.

Frequency, measured in Hertz (Hz), is the reciprocal of time – i.e. 10Hz means 10 times per second, and has a period (i.e. a duration) of 1/10 of a second. 1kHz is therefore 1/1000 second. If your microcontroller runs on a 12kHz clock, it will generate 12,000 clock cycles per second.

If you want to generate an interrupt every second, you would set the microcontroller to generate an interrupt every 12,000 clock cycles; for every half-second you would trigger an interrupt every 6,000 cycles (12,000 divided by 2).

Getting stuck into the MSP430 Timers

OK, we’ve looked timers in general – let’s get into the MSP430 timers. The various MSP430 microcontrollers have different pin configurations, with different numbers and types of timer. For this discussion, let’s keep things simple and focus our examples on the MSP430G2553. Why choose this one from all the MSP430 variants? Simply because the MSP430G2553 is the microcontroller that comes with the Launchpad MSP-EXP430G2 – the Launchpad is a great learning and prototyping tool, is exceptional value ($9.99), and TI ships it for free.

Continue to Part 2…

2 thoughts on “Getting Started with MSP430 Timers

  1. Thanks for the post. I do not understand how to use Capture Mode. You can post about Capture Mode. Thank you.

    • Pleased you found the post useful. A post on capture mode will come in the next few months – follow us on twitter and I’ll be sure to let everyone know.

Leave a comment

Your email address will not be published. Required fields are marked *