System Timer (SysTick)

 

The System Timer (SysTick) is an integrated 24-bit counter available in all ARM Cortex-M
microcontrollers that counts down to zero, useful for time delay and system interrupt application.



System Timer (SysTick) Registers

It consist of the following three registers: SysTick Control and Status (SysTick_CTRL), SysTick Reload Value (SysTick_LOAD), & SysTick Current Value (SysTick_VAL):

SysTick Control and Status Register (SysTick_CTRL), offset 0x010: 



SysTick Reload Value Register (SysTick_LOAD), offset 0x014:




SysTick Current Value Register (SysTick_VAL), offset 0x018:





As a summary:


SysTick_CTRL register:


  • Bit 0: use to enable the counter with a clock signal. 

    • Value 1 -> Enable

    • Value 0 -> Disable

  • Bit 1: use to enable the interrupt if needed. 

    • Value 1 -> Enable

    • Value 0 -> Disable

  • Bit 2: allows you to select between two clock sources: 

    • Value 1 -> System Clock (16 MHz)

    • Value 0 -> PIOSC (divided by 4)

  • Bit 16: use to read only (RO) the status of the counter. If:

    • Value 1 -> SysTick has reached zero. The COUNT flag is high. 

    • Value 0 -> SysTick hasn’t reached zero. The COUNT flag is low.

    SysTick_LOAD register:


  • Bit 23:0 -  for you to set up the initial value that will eventually be stored in the SysTick_VAL register. 

    • Value Range: 0x0 -> 0x00FF.FFFF (16,777,215)


Note: Because the SysTick_LOAD register is a 24-bit register you should be careful when assigning a value to this register. Remember, 0x00FF.FFFF (16,777,215) is the maximum value. 


SysTick_VAL register:


  • Bit 23:0 -  allow you to read the current value of the counter at the moment that it was accessed.  Value Range: 0x0 -> 0x00FF.FFFF (16,777,215)



Correct Initialization Sequence 


The correct initialization sequence for the SysTick counter is as follows:

1. Program the value in the STRELOAD register.
SysTick->LOAD = 0x00FFFFFE; //Max. to be used. 

2. Clear the STCURRENT register by writing to it with any value.
SysTick->VAL = 0x0; //Reset the SysTick counter value.

3. Configure the STCTRL register for the required operation.
SysTick->CTRL = 0x5; //Enable SysTick, no interrupt, use system clock.



Example


The following program will show you how to blink the red LED with a delay time of 1 second by  using the Tiva C Series TM4C123G LaunchPad.


Delay Time Calculation


Clock Frequency: 16MHz
Clock Time Period = (1/16MHz) = 62.5ns 


Let’s say that you load value N = 6 into the SysTick_LOAD register. 

SysTick->LOAD = 0x6;

Later you enable the SysTick counter (SysTick_VAL register) using the system clock.

SysTick->CTRL = 0x5; //No interrupt

It would take the SysTick_VAL register 62.5ns to change from 6 to 5. That is the clock period giving a clock frequency of 16MHz. 



To reach from 6 to 0 it would takes to the SysTick_VAL register:

N * clock period = 6 * 6.25ns =37.5ns

And to complete the whole System Interrupt cycle, i.e. reaches 0, raises the COUNT flag, reload the initial value and starts all over, it would take another clock period to complete:

SysTick delay time = (N + 1) * clock period = 7 * 6.25ns = 43.75ns 

Therefore, the System Timer (SysTick) would have a delay time of 43.75ns giving an initial value of N = 6 in the SysTick_LOAD register



But you want a 1 second delay time. By using the above equation the  (N + 1) value to be stored in the SysTick_LOAD register for a delay time of 1 second should be:

N = (SysTick delay time / System Clock Period ) - 1

=(SysTick delay time * System Clock Frequency) - 1

= (1s * 16MHz) -  1 = 16M - 1 = 15,999,999

Therefore, the value to be stored in the SysTick_LOAD register for a 1 second delay time would be 0x00F423FF (15,999,999). 

Note: SysTick_LOAD register is a 24-bit register allowing a maximum value of 0x00FF.FFFF (16,777,215). 0x00F423FF (15,999,999) is less than 0x00FF.FFFF (16,777,215), so you are ok. But remember to always take this into consideration.

CODE
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
The following program show how to set up the Sistem Timer Tick (SysTick) 
to blink the red LED with a delay time of 1 second using the 
Tiva C Series TM4C123G LaunchPad.

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 delay();

int main()
{
  /*GPIOF Initialization*/
  SYSCTL->RCGCGPIO |= 0x20;     
  GPIOF->DIR = 0x02;            
  GPIOF->DEN = 0x02;            
  
  while (1) 
  {
    GPIOF->DATA |= 0x02;
    delay();
    GPIOF->DATA &= ~(0x02);
    delay();                              
  }
}

void delay() {
  
    //SysTick Initialization sequence:
    /*1. Program the value in the STRELOAD register.*/
    SysTick->LOAD = 0x00F423FF;        //Value for 1sec delay giving a system clock of 16 MHz.
    /*2. Clear the STCURRENT register by writing to it with any value.*/
    SysTick->VAL = 0x0;         //Reset the SysTick counter value.
    /*3. Configure the STCTRL register for the required operation.*/
    SysTick->CTRL = 0x5;         //Enable SysTick, no interrupt, use system clock
  
    while((SysTick->CTRL & 0x10000) == 0){} //Here is the delay: wait until the Count flag is set
    SysTick->CTRL = 0x0;         //Stop the Counter
  
}




Comments

Anonymous said…
Very informative! Thanks
Anonymous said…
Among my finest runs have been enjoying in} on 우리카지노 50 and 100-hand Video Poker machines normally enjoying in} Jacks or Better, which is my normal game of selection. With that lovely galactic theme and a big ol’ Royal Flush prize to dream about. Every win comes with the possibility of a fast double-up or quadruple-up. Pick a go well with|swimsuit} if you want like|if you'd like} a 4x, or a colour for a 2x, and the card flips to reveal the result. Video poker could make for a nice change of pace from on-line and stay money poker games and tournaments. Video poker has such a low minimum bet you could play for subsequent to nothing on-line.
fjakzdt5yo said…
They are on a “must-watch” record of each science-fiction lover. NetEnt invested plenty of time, vitality, and funds to deliver an amazingly generous slot. The animations embody original photographs from the films which make this slot much more appealing. People at NetEnt paid homage to two films from 파라오카지노 an array of sequels — one screen is devoted to the Rise, and the opposite belongs to the Dawn. Playtech decided to take a experience on the hype practice and deliver a slot sport primarily based on the 2016 blockbuster, Batman vs. Superman Dawn of Justice, with an identical plot. In February, Mr Vegas introduced its entry to the Swedish market after receiving a licence from the nationwide regulator, Spelinspektionen.