Understanding the G.P. Timers

In this tutorial i will explain what are the General Purpose Timers of the Tiva tm4c123gh6pm from the launchpad. This tutorial is based around the respective datasheet from TI. It's a very basic explanation, and for more detail consult the datasheet

What are the General Purpose Timers?

From the name you are probably think they are something to do with keeping time and yes they can be used for that but not only. They aren't a simple clock that only gives you the time. But that's not the only mode of operation they have.

The Timers can be used for things like a RTC to keep time in seconds, a periodic timer, one shot timer, edge counter or a basic PWM generator.

Why General Purpose? Well the Tiva has more timers than this ones, but they are specific for other functions, like the RTC is made to specifically count timer very precisely or the system tick. The General Purpose are all-rounders but won't do some tasks better than a timer made for that specific task.

Let's see the specs from the datasheet:

  • 16/32-bit operating modes:

    • 16- or 32-bit programmable one-shot timer

    • 16- or 32-bit programmable periodic timer

    • 16-bit general-purpose timer with an 8-bit prescaler

    • 32-bit Real-Time Clock (RTC) when using an external 32.768-KHz clock as the input

    • 16-bit input-edge count- or time-capture modes with an 8-bit prescaler

    • 16-bit PWM mode with an 8-bit prescaler and software-programmable output inversion of the PWM signal

  • 32/64-bit operating modes:

    • 32- or 64-bit programmable one-shot timer

    • 32- or 64-bit programmable periodic timer

    • 32-bit general-purpose timer with a 16-bit prescaler

    • 64-bit Real-Time Clock (RTC) when using an external 32.768-KHz clock as the input

    • 32-bit input-edge count- or time-capture modes with a16-bit prescaler

    • 32-bit PWM mode with a 16-bit prescaler and software-programmable output inversion of the PWM signal

  • Count up or down

  • Twelve 16/32-bit Capture Compare PWM pins (CCP)

  • Twelve 32/64-bit Capture Compare PWM pins (CCP)

  • Daisy chaining of timer modules to allow a single timer to initiate multiple timing events

  • Timer synchronization allows selected timers to start counting on the same clock cycle

  • ADC event trigger

  • User-enabled stalling when the microcontroller asserts CPU Halt flag during debug (excluding RTC mode)

  • Ability to determine the elapsed time between the assertion of the timer interrupt and entry into the interrupt service routine

  • Efficient transfers using Micro Direct Memory Access Controller (μDMA)

    • Dedicated channel for each timer

    • Burst request generated on timer interrupt

As you can see the Tiva has alot of timers. Six 32bit timers + six 64bit timers (Wide timers) to a total of 12 timers, but you can divide them, bringing the total to twelve 16bit timers +twelve 32bit timers, 24 timers!

Each timer is composed by 2 free-running count up or down counters, named TimerA and TimerB, two prescaler registers, two match registers, two prescaler match registers, two shadow registers and two load/initialization registers.

TimerA and TimerB can be used individually if you divide a timer into 2, making a 32bit timer into 16bit one and a 64bit into a 32bit.

Note that the prescaler can only be used when you use the TimerA and TimerB individually. When counting down in one-shot or periodic modes, the prescaler acts as a true prescaler and contains the least-significant bits of the count. When counting up in one-shot or periodic modes, the prescaler acts as a timer extension and holds the most-significant bits of the count. In input edge count, input edge time and PWM mode, the prescaler always acts as a timer extension, regardless of the count direction.

Each Time has it's own match register, this can be used to generate a interrupt, DMA trigger or a timeout to stop the timer in case of one-shot mode.

The prescaler match register is like the normal match but it's to be used when the prescaler is in use.

Shadow register is simply to read the current counter value or write a new value into the counter that will be loaded in the next cycle

You can set different loads into TimerA and B. The load is the value the counter holds when you start it or, when in count down mode, the timer reaches 0 and reloads that value. You can simply change the counter value without changing the reload value

This table shows the timer capabilities in each mode.(taken from tm4c123gh6pm datasheet)

Operating modes

note: when some registers are refered to explain the operating modes, remember that that "n" depends on the timer half you use, A or B.

One-shot Timer mode:

In one-shot timer mode the timer can be configured to count up or down. In count down mode if the counter reaches 0 the timer stops. You can set a value from which it starts counting down. In count up the counter starts from 0 and stops if it reaches the timeout value. Note that only a one-shot or periodic time-out event can produce an ADC trigger assertion.

In this mode the timer can also trigger a DMA transfer.

The timer clock is the same as the system clock so each counter value is equal to 1/SysClk, being SysClk the system clock. Ex: With the timer in count down mode, load value of 100, and system clock 80Mhz. 1/80Mhz is 12,5nS. So the timer takes 1,25uS to reach 0 (12,5nS*100).

Periodic Timer mode:

The periodic timer mode is very similar to the one-shot mode. The diference is that it does not stop at reaching 0. When it reaches 0 it restarts with the value set in load/initialization register.

In this mode the timer can also trigger a DMA transfer.

Real-Time Clock Timer Mode

In RTC mode the timer is used to keep track of seconds and uses both TimersA and B join together to make a 32bit or 64bit timer (depending if it's a wide timer or not).

The input clock on a CCP0 input is required to be 32.768 KHz in RTC mode. The clock signal is then divided down to a 1-Hz rate and is passed along to the input of the counter. This is what allows the 1 second count. For example: In this MCU the CCP0 of Timer0 is PB6

When RTC mode is selected for the first time after reset, the counter is loaded with a value of 0x1. If a new value is loaded the counter begins counting at that value and rolls over at the fixed value of 0xFFFFFFFF.

In this mode the timer can also trigger a DMA transfer.

Input Edge-Count Mode

Note: For rising-edge detection, the input signal must be High for at least two system clock periods following the rising edge. Similarly, for falling-edge detection, the input signal must be Low for at least two system clock periods following the falling edge. Based on this criteria, the maximum input frequency for edge detection is 1/4 of the system frequency.

In this mode the timer, as the name imply, counts how many rising, falling, or any, edge happens on the pin that is attached to. It can work as a count up or count down counter. The timer is configured as a 24-bit or 48-bit up- or up- or down-counter including the optional prescaler with the upper count value stored. This is if you use the prescaler, other wise it's just a 16 or 32bit counter.

Input Edge-Time Mode

Note: For rising-edge detection, the input signal must be High for at least two system clock periods following the rising edge. Similarly, for falling edge detection, the input signal must be Low for at least two system clock periods following the falling edge. Based on this criteria, the maximum input frequency for edge detection is 1/4 of the system frequency.

The timer is configured as a 24-bit or 48-bit up- or down-counter including the optional prescaler with the upper timer value stored. This is if you use the prescaler, other wise it's just a 16 or 32bit counter.

Like edge-count mode, the timer can sense rising, falling or both edges. In this mode when the timer captures a event it stores the current counter value in GPTMTnR and GPTMTnPS registers and it stays there until other event changes the value. Don't worry if you don't like registers, you can still use TivaWare.

PWM Mode

The GPTM supports a simple PWM generation mode. In PWM mode, the timer is configured as a

24-bit or 48-bit down-counter with a start value (and thus period). 24 and 48 bit? That may sound weird but remember that there's a prescaler and that in PWM mode it works as timer extension. In a 32bit timer, divided into 2 of 16bits, the prescaler has a size of 8bits hence the 24bits. For a 64bit timer, 2 * 32bit, the prescaler is 16bits in size.

Let's check how the PWM works.

(taken from the datasheet)

This shows the timer value over time. You notice that the value starts and 0xC350, counts down to 0 and then resets back to 0xC350. Basically working like the periodic mode. This sets the period which is CLK*Load Value (GPTMTnILR), CLK is the clock source for the timer, usually the system clock like referred before.

Now how to set the duty of the PWM. The value 0x411A is loaded into the Match (GPTMTnMATCHR). The timer is counting down and when it reaches the Match value it inverts the output logic value, when it resets back to the Load value (0xC350) the output inverts again. Usually the TnPWML is 0, so when the timer starts counting down the output is 1, when it reaches the match the output goes 0 and goes back to 1 when the timer resets to the Load value. You can make the output work in reverse by setting TnPWML to 1.

And well this does it for this tutorial. It's a basic look into the timers. There's more to learn and you can check the datasheet for the rest if you want to know more. I hope you liked it.