STM32 SysTick tick timer principle and application

Publisher:清新家园Latest update time:2018-07-17 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

SysTick Timer

1. Function

The SysTick timer is a simple timer that is available in all CM3\CM4 core chips. The SysTick timer is often used for delay and is used as the system clock when a real-time system is used.

Whether used as a delay or as a system heartbeat clock, SysTick can do the job without too complicated functions.

2. Implementation Principle

The SysTick timer is a 24-bit countdown. When the countdown reaches 0, the value in the RELOAD register is used as the initial value of the timer. At the same time, an interrupt can be generated at this time (exception number: 15).

For example, if the value of RELOAD is 999, then when the countdown reaches 0, it will continue to count down from reset to 999.

        As long as the enable bit in the SysTick control and status register is not cleared, it will never stop and will continue to work even in sleep mode.

3. SysTick register (defined in core_cm3.h, all M3 core microcontrollers are the same)

#define SysTick ((SysTick_Type *) SysTick_BASE) 

#define SysTick_BASE (SCS_BASE + 0x0010)

#define SCS_BASE (0xE000E000) 

typedef struct

{

  __IO uint32_t CTRL; // Control and status register

  __IO uint32_t LOAD; // Reload value register

  __IO uint32_t VAL; // Current count value register

  __I uint32_t CALIB; // Calibration register

} SysTick_Type;

SysTick->CTRL: (can be set via SysTick_CLKSourceConfig() function)

COUNTFLAG(16)R: Count flag

When SysTick counts to 0, this bit is set to 1 by hardware, and when this bit is read, it is cleared to 0 by hardware.

CLKSOURCE(2)R/W: Clock source setting

1 = External clock source (STCLK) (1/8 of AHB bus clock (HCLK/8))

0 = Core clock (FCLK) (frequency of AHB bus clock (HCLK))

TICKINT(1)R/W: interrupt enable bit

1 = Generate a SysTick exception request when SysTick counts down to 0

0 = No action when count reaches 0

ENABLE(0)R/W: SysTick timer enable bit

(When the interrupt is enabled, you need to pay attention to the void SysTick_Handler(void) function)

SysTick_Type->LOAD: (SysTick_Config() function sets this register)

RELOAD(23:0)R/W: Reload value register

When SysTick reaches 0, the value will be reloaded

SysTick_Type->VAL: (SysTick_Config() function sets this register)

CURRENT(23:0)R/Wc: Current count value register

When read, it returns the current countdown value, and writing it clears it to zero, also clearing the COUNTFLAG flag in the SysTick Control and Status Register.

4. Library function analysis

misc.c

-------------------------------------------------- --------------------------------

#define SysTick_CLKSource_HCLK_Div8 ((uint32_t)0xFFFFFFFB)

#define SysTick_CLKSource_HCLK ((uint32_t)0x00000004)

#define IS_SYSTICK_CLK_SOURCE(SOURCE) (((SOURCE) == SysTick_CLKSource_HCLK) || \

((SOURCE) == SysTick_CLKSource_HCLK_Div8))

void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)

{

  /* Check the parameters */

  assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));

  if (SysTick_CLKSource == SysTick_CLKSource_HCLK)

  {

SysTick->CTRL |= SysTick_CLKSource_HCLK; // Set CLKSOURCE to 1

  }

  else

  {

SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8; // Set CLKSOURCE to 0

  }

}


core_cm3.c

-------------------------------------------------- --------------------------------

#define SysTick_LOAD_RELOAD_Pos 0                                           

#define SysTick_LOAD_RELOAD_Msk (0xFFFFFFul << SysTick_LOAD_RELOAD_Pos)

typedef enum IRQn

{

//...

SysTick_IRQn = -1, 

//...

}IRQn_Type;

#define __NVIC_PRIO_BITS 4

#define SysTick_CTRL_CLKSOURCE_Pos 2                                           

#define SysTick_CTRL_CLKSOURCE_Msk (1ul << SysTick_CTRL_CLKSOURCE_Pos)          


#define SysTick_CTRL_TICKINT_Pos 1                                           

#define SysTick_CTRL_TICKINT_Msk (1ul << SysTick_CTRL_TICKINT_Pos)            


#define SysTick_CTRL_ENABLE_Pos 0                                           

#define SysTick_CTRL_ENABLE_Msk (1ul << SysTick_CTRL_ENABLE_Pos) 

static __INLINE uint32_t SysTick_Config(uint32_t ticks)

  if (ticks > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */

  // Set the count value to ticks - 1 

  // Reason 1: The video says that it takes time to execute these codes, so reduce one beat

  // Reason 2: I think it is because SysTick counts down to 0. For example, if it is set to 1000, the range should be 999 ~ 0.

  SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;     

  // Set interrupt priority

  NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); 

  SysTick->VAL = 0;                                

  // Set the clock source to the external clock source, enable the interrupt, and enable the SysTick timer

  SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | 

   SysTick_CTRL_TICKINT_Msk | 

   SysTick_CTRL_ENABLE_Msk;                   

  return (0);                                                 

}

5. Delay Application

1. Interrupt mode

static __IO uint32_t TimingDelay;

void Delay(__IO uint32_t nTime)

   TimingDelay = nTime;

   while(TimingDelay != 0);

}

/* Interrupt service function */

void SysTick_Handler(void)

{

if (TimingDelay != 0x00) 

TimingDelay--;

}

}

int main(void)

{  

// ...

if (SysTick_Config(SystemCoreClock / 1000)) // Note that the systick clock here is HCLK and the interrupt interval is 1ms 

{

while (1);

}

while(1)

{

Delay(200); //2ms

// ...

}

}

SysTick_Config(SystemCoreClock / 1000): (The original code assumes that the clock source is HCLK)

The setting here is 72000000Hz / 1000 = 72000 ticks, which means SysTick starts counting down from (72000-1).

An interrupt is triggered every time 72,000 beats are counted down.

The duration of one beat is: 72000000 / 72000 = 1000us == 1ms

SysTick_Config((SystemCoreClock / 8000000) * 1000 * 1): 

SysTick_Config() will set the clock source to HCLK/8, so the parameters in the above code cannot be used in actual applications.

SystemCoreClock / 8000000: 1us beat number

1us beats * 1000: then 1ms beats

1ms beat number * 1: Set a SysTick interrupt of 1ms, that is, count down from ((SystemCoreClock / 8000000) * 1000 * 1) - 1.

2. Polling method

static u8 fac_us=0; //us delay multiplier    

static u16 fac_ms=0; //ms delay multiplier

void delay_init()

{

SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //Select external clock HCLK/8

fac_us = SystemCoreClock/8000000; // 1/8 of the system clock 1us = 72000000 / 8000000 = 9 beats

fac_ms = (u16)fac_us*1000; // 1ms requires 9 * 1000 = 9000 beats

}

//Delay nus microseconds         

void delay_us(u32 nus)

{

u32 temp;

SysTick->LOAD=nus*fac_us; //Time loading     

SysTick->VAL=0x00; //Clear the counter

SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; //Start countdown

do

{

temp=SysTick->CTRL;

}while((temp&0x01)&&!(temp&(1<<16))); //Wait for time to arrive  

SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //Close the counter

SysTick->VAL =0X00; //Clear the counter  

}

//Delay nms

//Note the range of nms

//SysTick->LOAD is a 24-bit register, so the maximum delay is:

//nms<=0xffffff*8*1000/SYSCLK

//SYSCLK is in Hz, nms is in ms

//Under 72M conditions, nms<=1864 

void delay_ms(u16 nms)

{        

u32 temp;    

SysTick->LOAD=(u32)nms*fac_ms; //Time loading (SysTick->LOAD is 24bit)

SysTick->VAL =0x00; //Clear the counter

SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; //Start countdown 

do

{

temp=SysTick->CTRL;

                //Wait for the time to arrive. Here is a little trick. Check the SysTick enable bit through (temp&0x01) to avoid the Systick timer being turned off and causing an infinite loop. 

}while((temp&0x01)&&!(temp&(1<<16)));  

SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //Turn off the counter

SysTick->VAL =0X00; //Clear the counter        


Keywords:STM32 Reference address:STM32 SysTick tick timer principle and application

Previous article:Setting of sysTick in STM32
Next article:Problems and solutions of STM32 Systick timer in realizing 1us delay

Recommended ReadingLatest update time:2024-11-15 15:49

stm32 nested vector interrupt controller NVIC
The Nested Vectored Interrupt Controller (NVIC) is closely connected to the processor core interface, which enables low-latency interrupt processing and efficient processing of late interrupts. The Nested Vectored Interrupt Controller manages interrupts including kernel exceptions. NVIC related functions are included
[Microcontroller]
stm32 nested vector interrupt controller NVIC
Problems encountered in stm32 debugging and solutions
1. PB4 is always at a high level, causing the ultrasonic sensor to fail to work. I searched on Baidu and found that some pins of stm32 are not general GPIO by default when powered on, such as JTAG and SWD debugging pins. So if you want to use these pins as general IO ports, you must turn off the JTAG and SWD functions
[Microcontroller]
STM32 introductory study notes: capacitive touch experiment (Part 1)
8.1 Introduction to capacitive touch 8.1.1 Overview of capacitive touch With the development of science and technology, traditional mechanical buttons are gradually disappearing from devices. The main reason for this is that mechanical buttons use mechanical contact and have a relatively short lifespan. From the user
[Microcontroller]
STM32 introductory study notes: capacitive touch experiment (Part 1)
STM32 Hex file format analysis
        If you use a special program to view the Hex file (usually Notepad can do this), you will find that the entire file is in lines, each line starts with a colon, and the content is all hexadecimal code. The Hex file can be split as follows to analyze its content:   For example:   :020000040000FA , I see it as 0x
[Microcontroller]
Solution to setting I2C_FLAG_BUSY in STM32
The touch screen IC on the stm32f429-disco is STMPE811, which uses I2C communication. The use of this IC is not difficult. It includes 8-channel 12-bit AD, 8 GPIO ports, plus 128set FIFO and several registers. The problem lies with the I2C IP core of STM32. Many people have complained about it online. The main probl
[Microcontroller]
Solution to setting I2C_FLAG_BUSY in STM32
How to define variables in flash in stm32
Purpose: Define variables in flash Actually, writing this is just a record. Suddenly, I was working on the font display problem. I thought that if the font data is put in the memory, it will inevitably cause problems. It is better to put it in the flash so that it does not need to be changed. int a; a in memory cons
[Microcontroller]
【stm32f407】SysTick to achieve delay
1. SysTick Introduction: The processing of the CM4 core is the same as that of the CM3. It contains a SysTick timer. SysTick is a 24-bit countdown timer. When it reaches 0, it will automatically reload the initial value of the timer from the RELOAD register. As long as the enable bit in the SysTick control and statu
[Microcontroller]
STM32 USART serial port simple use
The STM32 library is really powerful! The functions look like sentences... Ok, let’s start: Using USART in polling mode: Set the clock:       RCC_APB2Periph_AFIO function multiplexing IO clock          RCC_APB2Periph_GPIOA GPIOA clock          RCC_APB2Periph_USART1 USART1 clock You can use   //Enable seria
[Microcontroller]
STM32 USART serial port simple use
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号