TI ARM Tiva TM4C123G - 1.2 General Purpose Timer Module (GPTM)
The following exercises will explain timers and how to configure them in the Texas Instrument Tiva TM4C123G LaunchPad for One-Shot or Periodic Mode. They will used to blink an LED at an specific delay time.
Each individual Timer Blocks contains two free-running count up or down counters. They are called TimerA and TimerB. And are these two that you will use to control and/or configured individually (16-bit) or concatenated (32-bit) to generate some periodic or one-shot signal for delays, interrupt or any other additional feature from the Tiva TM4C123G microcontroller.
From now on, let’s use TimerA from the first Block 16/32Bbit -> Timer 0 as our model for different examples. In a similar fashion, the same principles may apply to the other 24 timers.
Supported Timer Modes
To make use of any of the 24 timers the GPTM needs to be initialized and configured prior to use. This initialization process will be different according to their supported timer modes.
The available modes for each GPTM block are One-shot, Periodic, Real Time Clock (RTC), Edge Count, Edge Time and PWM, as described below:
For the purpose of this TUTORIA only the first three modes, One-Shot Mode, Periodic Mode and RTC will be discussed.
One-Shot Mode
In One-Shot mode, the timer stops counting after a one time-out event has been completed.
When the timer is counting down, the timer counter begins counting from GPTMTnILR and goes down until it reaches zero. GPTMTnILR is the register used to load the starting count value into the timer. With one more clock cycle the TATORIS flag of the GPTMRIS register raises to notify that TimerA has reached zero. This indicates the completion of one time-out event. In the same clock cycle the TnEN bit in the GPTMCTL register is clear, disabling the counter and stopping counting.
When the timer is counting up, it counts from 0 to GPTMTnILR. With an additional clock cycle TATORIS flag of the GPTMRIS register it raises and the TnEN bit of the GPTMCTL register is clear causing the timer to stop.
Periodic Mode
Initialization and Configuration sequence:
I will summarize the steps to follow to Initialization and Configuration the General Purpose Timer Module (GPTM) for One-Shot or Periodic Mode.
For us to be able to use TimerA we must first enable the clock signal to the Timer Block0 by setting the appropriate TIMERn bit in the RCGCTIMER register.
SYSCTL->RCGCTIMER |= 1; //enable clock to Timer Block0 (0:disable, 1:enable)
Determine the global operation of the GPIM module by writing the corresponding value to the GPTMCFG register field: 2:0
TIMER0->CFG = 0x00; //For a 16/32-bit timer, this value selects the 32-bit timer config.
TIMER0->CFG = 0x04; //For a 16/32-bit timer, this value selects the 16-bit timer config.
Disable TimerA by setting bit TAEN in the GPTMCTL register to low before making any changes. Modification to TimerA during running time may cause unpredictable results.
TIMER0->CTL = 0;//disable Timer before initialization
Configure TimerA as One-shot mode or Periodic Mode by setting field TAMR in the GPTM Timer A Mode Register (GPTMTnMR). Same register can be used to select count up or down.
TIMER0->TAMR = 0x01; //One-Shot mode and down-counter
TIMER0->TAMR = 0x02; //Periodic mode and down-counter
Load the start value into the GPTM Timer n Interval Load Register (GPTMTnILR).
TIMER0->TAILR = 16000 * ttime - 1; //Timer A interval load value register
Make sure timeout flag is clear by writing a 1 to the TATOCINT bit in the GPTMICR
register.
TIMER0->ICR = 0x1; //clear the TimerA timeout flag.
Set the TnEN bit in the GPTMCTL register to enable the timer and start counting.
TIMER0->CTL = 0x01;//enable Timer A after initialization
Poll the GPTMRIS register to monitor the TimerA timeout flag. That is the bit 0 (TATORIS) in the GPTMRIS register.
while ((TIMER0->RIS & 0x1) == 0) { //wait for TimerA timeout flag to set
Value Description
0 Timer A has not timed out.
1 Timer A has timed out. This interrupt is asserted when a one-shot or periodic
mode timer reaches it's count limit (0 or the value loaded into GPTMTAILR,
depending on the count direction).
Examples: Let's blink the red LED with a delay time of 4ms.
Let’s assume that the CPU frequency is the default 16MHz.
Clock Frequency = 16MHz
This gives us a clock period of 62.5ns.
Clock Period = (1/16MHz) = 62.5ns
Let's also assume we are using the 16-Bit counter A from Timer0, Timer0A.
So, our max. value to be stored in the GPTMTnILR register would be 65,535 (0xFFFF).
N = ( 2^16 - 1) = 65,535
With this value the counter would have a delay time of 4ms to complete.
Counter Delay Time = ( N + 1 ) * clock period = (65,535 + 1) * 62.5ns = 4ms.
CODE
All LEDs are high active (a "1" turns ON the LED).
PF1 - red LED 0x02
PF2 - blue LED 0x04
PF3 -green LED 0x08
*/
#include <stdint.h>
#include <TM4C123GH6PM.h> //Tiva C Series TM4C123G “Header File”.
void timer0A_delayMs(int ttime);
int main()
{
/*GPIOF Initialization*/
SYSCTL->RCGCGPIO |= 0x20; //enable clock to GPIOF
GPIOF->DIR = 0x02; //enable GPIO pin PF1 as output
GPIOF->DEN = 0x02; //enable GPIO pin PF1 for digital function
while (1)
{
GPIOF->DATA |= 0x02; //Turn On red LED
timer0A_delayMs(4); //Timer0A msec delay
GPIOF->DATA &= ~(0x02); //Turn OFF red LED
timer0A_delayMs(4); //Timer0A msec delay
}
}
/*milliseconds delay using one-shot mode*/
void timer0A_delayMs(int ttime) {
SYSCTL->RCGCTIMER |= 1; //enable clock to Timer Block0
TIMER0->CTL = 0; //disable Timer before initialization
TIMER0->CFG = 0X04; //16-bit option
TIMER0->TAMR = 0X01; //one-shot mode and down-counter
TIMER0->TAILR = 16000 * ttime - 1; //Timer A interval load value register
TIMER0->ICR = 0X1; //clear the TimerA timeout flag.
TIMER0->CTL = 0X01; //enable Timer A after initialization
while ((TIMER0->RIS & 0x1) == 0) { //wait for TimerA timeout flag to set
}
}
N=delay timeSystem Clock Period - 1 =(Clock Frequency * delay time ) - 1
= (16MHz * 1ms ) - 1= 16K - 1 = 15,999
Therefore, with some minor changes to the previous code we obtain a timer with a 1m second delay.
CODE
/*
The following program shows how to set up the General Purpose Timer Module (GPTM) for One-Shot Mode in the Texas Instrument Tiva TM4C123G LaunchPad to blink the red LED once with a delay time of 1ms.
All LEDs are high active (a "1" turns ON the LED).
PF1 - red LED 0x02
PF2 - blue LED 0x04
PF3 -green LED 0x08
*/
#include <stdint.h>
#include <TM4C123GH6PM.h> //Tiva C Series TM4C123G “Header File”.
void timer0A_delayMs(int ttime);
int main()
{
/*GPIOF Initialization*/
SYSCTL->RCGCGPIO |= 0x20; //enable clock to GPIOF
GPIOF->DIR = 0x02; //enable GPIO pin PF1 as output
GPIOF->DEN = 0x02; //enable GPIO pin PF1 for digital function
while (1)
{
GPIOF->DATA |= 0x02; //Turn On red LED
timer0A_delayMs(1); //Timer0A msec delay
GPIOF->DATA &= ~(0x02); //Turn OFF red LED
timer0A_delayMs(1); //Timer0A msec delay
}
}
/*milliseconds delay using one-shot mode*/
void timer0A_delayMs(int ttime) {
SYSCTL->RCGCTIMER |= 1; //enable clock to Timer Block0
TIMER0->CTL = 0; //disable Timer before initialization
TIMER0->CFG = 0X04; //16-bit option
TIMER0->TAMR = 0X01; //one-shot mode and down-counter
TIMER0->TAILR = (16000 * ttime) - 1; //Timer A interval load value register
TIMER0->ICR = 0X1; //clear the TimerA timeout flag.
TIMER0->CTL = 0X01; //enable Timer A after initialization
while ((TIMER0->RIS & 0x1) == 0) { //wait for TimerA timeout flag to set
}
}
For a Periodic Mode, replace :
TIMER0->TAMR = 0X01; //One-shot Mode and down-counter
with
TIMER0->TAMR = 0X02; //Periodic Mode and down-counter
Plug your Tiva TM4C123G LaunchPad to your PC.
(Make sure the Power Select switch is to the right).
Copy and Paste the code onto your development environment tool and run the program.
Comments