Categories
Keil uVision TIVA Launch Pad

Tiva C TM4C123G Systick Timer

We will create delay using the Systick Timer in TM4C123GH6PM microcontroller in Keil IDE using CMSIS Core libraries. We will use systick timer in Interrupt mode and toggle the RED on board LED in the systick interrupt service routine(ISR).

The systick timer is a dedicated hardware-based timer in your Tiva C microcontroller. This is built inside the ARM Cortex M4 CPUs. Because our TM4C123GH6PM is also ARM Cortex M series microcontroller it also has it. It helps you set a regular timer that interrupts your tasks at specific intervals. This is handy for things like making LEDs blink on accurate time intervals. If you remember from our previous LED blinking example with Tiva C TM4c123g, you remember that the blinking rate is not accurate as we controlled the blink rate with some for loop which is not accurate.

To use the systick timer on your Tiva C TM4C123G launch pad, just follow these simple steps:

  • Enable the clock for the GPIO port that contains the LED pin. You can do this by setting the corresponding bit in the RCGCGPIO register.
  • Enable the digital function of the LED pin by setting the corresponding bit in the DEN register.
  • Set the direction of the LED pin as output by setting the corresponding bit in the DIR register.
  • Configure the systick timer control registers.
  • Create a Systick_handler Interrupt Service Routine
  • Toggle LED in the systick handler ISR
  • Enable the Global Interrupt

TM4C123GH6PM Systick Timer Configuration

To configure the Systick Timer you need to do the following

  • SysTick->LOAD : This register holds the reload value for the systick timer. You can calculate the value based on the system clock frequency and the desired delay time. In our case in the Tiva C TM4C123G launchpad evaluation board the system clock frequency is 16 MHz. Therefore the delay time is 1 second, the reload value is 16000000 - 1.
  • SysTick->VAL : This register holds the current value of the systick timer. You can write any value to this register to clear the current value and the COUNT flag.
  • SysTick->CTRL : This register controls the operation of the systick timer. You can set the following bits in this register:
  • SysTick_CTRL_CLKSOURCE_Msk : This bit selects the clock source for the systick timer. You can set this bit to 1 to use the system clock or clear this bit to 0 to use an external reference clock.
  • SysTick_CTRL_TICKINT_Msk : This bit enables or disables the systick timer interrupt. You can set this bit to 1 to enable the interrupt or clear this bit to 0 to disable the interrupt.
  • SysTick_CTRL_ENABLE_Msk : This bit enables or disables the systick timer. You can set this bit to 1 to enable the timer or clear this bit to 0 to disable the timer.
  • Define the systick timer interrupt service routine. You can do this by using the same name as the weak definition in the startup file, which is SysTick_Handler1. In this function, you can toggle the LED pin by using the XOR operation on the DATA register of the GPIO port2.
  • Enable the global interrupts by using the __enable_irq() function.

The reload value calculation formula is: Reload = (required delay / Clock Frequency) – 1

Here is the complete code for blinking Red on board LED in Tiva C TM4C123G launch pad evaluation board.

#include "TM4C123.h"                    // Device header

#define LED_RED   (1U << 1)

void SysTick_Handler(void);

int main()
{
    SYSCTL->RCGCGPIO |= (1U << 5);  // enable clock for GPIOF
    GPIOF->DEN |= LED_RED; // enable digital function on pin 1
    GPIOF->DIR |= LED_RED; // set pin 1 as output

    SysTick->LOAD = 16000000 - 1; // set reload value for 1 second delay
    SysTick->VAL = 0; // clear the current value and the COUNT flag
    SysTick->CTRL = (1U << 2) | (1U << 1) | 1U; // enable systick timer with system clock and interrupt

    __enable_irq(); // enable global interrupts

    while(1)
    {
        // do nothing
    }
}

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

Code language: Arduino (arduino)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.