Skip to content

Hardware Timers and Timing Control ​

Laboratory Manual

The lab manual contains the full theory, circuit diagrams, and requirements for this experiment - keep it open while you work. Download Experiment 7 PDF

Target device: TM4C123GH6PM

Examples ​

The following examples demonstrate SysTick and GPTM configuration and usage.

Example 1: Millisecond Counter with SysTick Timer ​

This example configures the SysTick timer to generate interrupts every 100 ms and toggles the green LED (PF3) in the interrupt handler.

c
#include "TM4C123.h"
#define GREEN_LED 0x08					// PF3 - Green LED

volatile uint32_t systick_counter = 0;	// Global counter for SysTick interrupts

int main(void)
{
	SYSCTL->RCGCGPIO |= (1<<5);			// Enable clock to GPIOF
	
	__asm__("NOP");
	__asm__("NOP");
	__asm__("NOP");
	
	GPIOF->DIR |= GREEN_LED;			// Set green LED as output pin
	GPIOF->DEN |= GREEN_LED;			// Enable digital function for green LED
	

	SysTick->LOAD = 5000000 - 1;			// Set reload value for 100ms
	SysTick->VAL = 0;						// Clear current value
	SysTick->CTRL = 0x07;					// Enable SysTick with processor clock and interrupt
	
	while(1)								// Main loop
	{
	}
}

void SysTick_Handler(void)
{
	systick_counter+=100;				// Increment counter by 100ms
	GPIOF->DATA ^= GREEN_LED;			// Toggle green LED
}

Explanation:

  • SysTick->LOAD = 5000000 - 1; sets the reload value for 100 ms at 50 MHz.
  • SysTick->VAL = 0; clears the current counter value.
  • SysTick->CTRL = 0x07; enables the timer with processor clock and interrupt.
  • SysTick_Handler() is called every 100 ms and toggles the LED.
  • systick_counter accumulates elapsed time in milliseconds (+100 per interrupt), which can be used for longer timing.

Example 2: Maximum 16-bit Delay with GPTM ​

This example configures TIMER1 in 16-bit periodic mode with maximum prescaler to achieve the longest possible delay and toggles the blue LED (PF2).

c
#include "TM4C123.h"
#define BLUE_LED 0x04


int main(void)
{
	SYSCTL->RCGCGPIO |= (1<<5);			// Enable clock to GPIOF
	SYSCTL->RCGCTIMER |= (1<<1);		// Enable clock to Timer1

	__asm__("NOP");						// Wait for clock stabilization
	__asm__("NOP");						// Wait for clock stabilization
	__asm__("NOP");						// Wait for clock stabilization

	
	GPIOF->DIR |= BLUE_LED;				// Set blue LED as output pin
	GPIOF->DEN |= BLUE_LED;				// Enable digital function for blue LED

	TIMER1->CTL = 0;        			// Disable the timer
	TIMER1->CFG = 0x4;       			// Choose 16-bit mode
	TIMER1->TAMR = 0x02;       			// Periodic mode
	TIMER1->TAPR = (1 << 8)  - 1;			// Set prescaler value
	TIMER1->TAILR = (1 << 16) - 1;  		// Set initial reload value
	TIMER1->ICR = (1 << 0);           		// Clear any prior interrupts
	TIMER1->IMR |=(1<<0);				// Enable timeout interrupt
	NVIC->ISER[0] |= (1<<21);			// Enable Timer1A interrupt in NVIC
	TIMER1->CTL |= (1 << 0);          		// Enable the timer
	
    while(1)							// Main loop
    {
    }
}


void TIMER1A_Handler()
{
	if(TIMER1->MIS & 0x1) {				// Check if timer timeout interrupt occurred
		GPIOF->DATA  ^= BLUE_LED;		// Toggle blue LED
 		TIMER1->ICR = 0x1;				// Clear timer interrupt flag
	}
}

Explanation:

  • SYSCTL->RCGCTIMER |= (1<<1); enables clock to TIMER1.
  • TIMER1->CFG = 0x4; selects 16-bit mode.
  • TIMER1->TAMR = 0x02; configures periodic mode (count down).
  • TIMER1->TAPR = (1 << 8) - 1; sets the prescaler to its maximum (255, divisor = 256).
  • TIMER1->TAILR = (1 << 16) - 1; writes 65535 (0xFFFF, the 16-bit maximum).
  • Period: T=65536×25650,000,000≈0.335 seconds.
  • NVIC->ISER[0] |= (1<<21); enables TIMER1A interrupt (IRQ 21).
  • TIMER1A_Handler() toggles the blue LED and clears the interrupt flag.

Tasks ​

Task 1: Debouncing a Push Button with SysTick ​

Modify the SysTick example so that the RED LED (PF1) toggles only when SW1 (PF4) is pressed and properly debounced using the SysTick timer.

Requirements:

  • Use SysTick_Config(SystemCoreClock/1000) to generate a 1 ms tick.
  • Implement interrupt-driven GPIO input for SW1 (PF4) as in Experiment 6.
  • In the GPIO ISR, use a global millisecond counter to implement 150 ms debouncing.
  • Only toggle the RED LED if at least 150 ms have elapsed since the last press.

Hint: Declare global variables:

c
volatile uint32_t global_ms = 0;
volatile uint32_t last_press_ms = 0;

In SysTick_Handler(), increment global_ms. In GPIOF_Handler(), check:

c
if ((global_ms - last_press_ms) >= 150) {
    GPIOF->DATA ^= (1 << 1);  // Toggle RED LED
    last_press_ms = global_ms;
}

Task 2: Multiple Blinking LEDs with GPTM ​

Use three GPTM timers (TIMER0A, TIMER1A, TIMER2A) to blink the three LEDs at different rates:

  • RED LED (PF1): Blink every 250 ms (use TIMER0A)
  • BLUE LED (PF2): Blink every 500 ms (use TIMER1A)
  • GREEN LED (PF3): Blink every 1000 ms (use TIMER2A)

Requirements:

  • Configure each timer in 32-bit periodic mode.
  • Calculate the TAILR values for each period at 50 MHz.
  • Enable interrupts for each timer in the NVIC.
  • Implement separate ISRs (TIMER0A_Handler(), TIMER1A_Handler(), TIMER2A_Handler()).
  • Each ISR should toggle its corresponding LED and clear the interrupt flag.