Skip to content

Hardware Timers and Timing Control ​

Complete Lab Manual

For the complete experiment including learning objectives, theoretical background, and detailed explanations, download the PDF manual: Download Experiment 6 PDF

Examples ​

Example 1: Milliseconds Counter with SysTick Timer ​

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
}

Example 2: Maximum 16-bit Delay with GPTM ​

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 = 256  - 1;			// Set prescaler value
	TIMER1->TAILR = 65536 - 1;  		// Set initial reload value
	TIMER1->ICR = 0x1;           		// Clear any prior interrupts
	TIMER1->IMR |=(1<<0);				// Enable timeout interrupt
	NVIC->ISER[0] |= (1<<21);			// Enable Timer1A interrupt in NVIC
	TIMER1->CTL |= 0x01;          		// 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
	}
}

Tasks ​

Task 1: Debouncing a Push Button with SysTick ​

Update your SysTick program so that the RED LED (PF1) toggles only when SW1 (PF4) is pressed and properly debounced.

Use SysTick_Config(SystemCoreClock/1000) to generate a 1 ms tick. Accumulate these ticks in a counter to measure longer intervals (e.g., 2000 ticks for 2 seconds).

  • Debouncing requirement: Use the 1 ms SysTick counter to debounce SW1 (PF4) with a 150 ms interval. When a valid press is detected (after debouncing), toggle the RED LED (PF1).

Hint

Declare global variables:

c
volatile uint32_t global_ms;
volatile uint32_t last_ms;
  • Increment global_ms in the SysTick handler.
  • In the GPIOF ISR, check if (global_ms - last_ms) >= 150 before toggling the LED and set last_ms = global_ms;.

Task 2: Multiple Blinking LEDs with GPTM ​

Use three external LEDs alongside 3 GPTM timers to blink each LED at different intervals:

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