Systick Timer Interrupt Programming TM4C123 ARM Cortex M4

In this tutorial, we will learn how to generate a delay with a systick timer interrupt of TM4C123 microcontroller. We will toggle an LED with a delay of 1 second using the interrupt service routine of the systick timer handler function. In the last tutorial, we have discussed the in-depth introduction of the systick timer module of TM4C123 ARM Cortex M4 microcontroller. In that tutorial, we have learned to use a systick timer and how to configure control and status registers of the system timer. We have seen an example to generate 1 second delay, but we used a polling method. In this tutorial, we will see an example of an interrupt method. 

 You can read that post on this link: 

SysTick Timer (System Timer) TM4C123G ARM Cortex M4 Microcontroller

Systick Interrupt Introduction

Systick timer has three configuration and control registers. But STCTRL (systick control and status register is the main register which is used to configure the timer. It is a 32-bit register, but only four bits are used such as ENABLE, INTEN, CLK_SCR and COUNT bits. Rest of the bits are reserved as shown in figure below:

Systick timer configuration and control register TM4C123

INTEN bit enables or disables the interrupt functionality of systick timer. If we set the INTEN bit ( INTEN=1), it enables the systick interrupt. That means, whenever the counter reaches zero from a reload value, the systick timer requests the interrupt signal to the nested interrupt vector controller (NVIC) of TM4C123 microcontroller.

For example, if we load the number 100 to reload the register and the clock frequency of TM4C123 microcontroller is 16MHz. That means counter value will decrement by 1 after every 1/16MHz or 62.5 nanoseconds. Therefore,  the counter value will reach to zero after 101 x 62.5ns or : 

101 x 62.5 = 6.3125ms

Hence, after 6.3125ms, the systick timer set the count flag bit of STCTRL register which will send the interrupt signal to nested interrupt vector controller. 

Systick Interrupt Applications

Followings are the important applications with interrupt method:

  • It can be used to create periodic tasks that will execute after pre-defined interval of time
  • It can be used to synchrozie tasks execution in periodic manner

For example, we have one task in our application that reads current sensor value and we want to measure current value after every 100ms second. By using systick interrupt, we can enable the current sensor task execution. That means, the task will remain in the blocking state untill interrupt occurs. When systick timer interrupt occurs, the current sensor task will take one measurement and go back to blocked state untill next interrupt. These concepts are used in real time operating sytems.

You can read our complete FreeRTOS tutorials list here:

FreeRTOS Tutorials

Using Systick Interrupt Module

As we have discussed in last section, when counter value reaches zero, it will set counter flag bit (bit 16 of STCTRL register) which will generate an systick interrupt request to nested interrupt vector controller. The nested interrupt vector controllers reads this interrupt and tranfers the CPU from thread handler mode to exception hanlder mode. In this mode, CPU reads the respective interrupt singal and executes its respective handler routine.

Systick Interrupt Block Diagram

If you see the interrupt vector table of TM4C123, the systick interrupt has number 15 or INT15.

systick timer interrupt tm4c123 arm cortex m4

As we have seen in TM4C123G microcontroller startup file tutorial that the startup file contains weak definition of all interrupts and exception routines. These ISR routines are defined with weak attributes that means we can redefine them inside our main code according to our requirement. But the name of a specific ISR routine should remain the same. 

Systick Handler function

For example, the name of the systick interrupt service routine in the startup file of TM4C123G microcontroller in Keil compiler is SysTick_Handler.

Void SysTick_Handler (void)
{ //instructions  }

Generate Delay with Systick Interrupt

This program will generate a delay of 1 second. In other words, the systick timer interrupt will occur after one second. To generate interrupt after every second, the value of reload register will be: 

Reload =  (require delay / clock tim_period) - 1
Reload = ( 1 / 1/16MHz) - 1 = 16000000 - 1 = 1599999

Where 16MHz is a clock frequency of TM4C123 microcontroller. Hence, we will load system timer relaod register with 1599999. The count flag bit will become one and generate interrupt everytime the counter down counts from 1599999 to 0. After interrupt, the count register will be automatically loaded with 1599999.

Program

This example code generates a delay of one second. using an interrupt of systick timer and to demonstrate this, we will turn on and turn off onboard Green LED which is connected with PF3 pin of the Tiva launchpad.

This code uses an interrupt method instead of a polling method to toggle LED after one second. As you can see inside the while loop, we are doing nothing instead we have written a logic to turn on the LED inside the systick handler function.

#include "TM4C123GH6PM.h"
int main()
{
  SYSCTL->RCGCGPIO |= 0x20; // turn on bus clock for GPIOF
  GPIOF->DIR       |= 0x08; //set GREEN pin as a digital output pin
  GPIOF->DEN       |= 0x08;  // Enable PF3 pin as a digital pin
	
	SysTick->LOAD = 15999999; // one second delay relaod value
	SysTick->CTRL = 7 ; // enable counter, interrupt and select system bus clock 
	SysTick->VAL  = 0; //initialize current value register 
   
	while (1)
	{
		//do nothing here
	}
}

// this routine will execute after every one second

void SysTick_Handler(void)
{
   GPIOF->DATA  ^= 8;  //toggle PF3 pin
  
}

Video Demo

Other TM4C123G Tiv C Tutorials:

Leave a Comment

three × four =