STM32 Cortex system timer (SysTick)

Publisher:独行侠客Latest update time:2017-09-17 Source: eefocusKeywords:stm32  Cortex  SysTick Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

SysTick clock, commonly known as "tick timer", can generate an interrupt at a set time. Functions such as delay_ms() can be seen everywhere in control engineering code. But its internal mechanism has never been clear. I spent some time to study it today.

First, let’s take a look at the configuration of the SysTick register in the data sheet, as shown in the figure: 
Write the picture description here

Register Description

Write the picture description here 
Write the picture description here 
When the clock source of stm32 
is selected as an external clock source, the Systick clock is HCLK /8. 
When the core clock source is selected, the Systick clock is HCLK

Delay Programming Principle 
Systick timer is a 24-bit down counter. After setting the initial value and enabling it, it will reduce the counter by 1 for each system clock cycle. 
When the count reaches 0, it will automatically reload the timer initial value from the RELOAD register. As long as the enable bit in the SysTick control and status register is not cleared, it will never stop.

Delay programming steps 
1. Calculate how many clock cycles fac_us are needed to generate 1us 
2. Calculate the value of the RELOAD register 
, which is the number of clock cycles required to generate the corresponding delay 
RELOAD = fac_us * nus 
3. Start counting 
4. Loop to detect the flag bit when the count reaches 0 
5. Clear the counter and turn off the timer

SysTick exception configuration steps 
1. Configure the three registers CTRL/LOAD/VAL 
2. Initialize the clock used by SysTick 
3. Clear the current value of the system and load the reload value 
4. Enable SysTick so that SysTick can respond to interrupts

====================== 
The library function version 
uses the ST function library to use systick in strict accordance with the following order:

1. Call SysTick_CounterCmd() – Enable SysTick counter 
2. Call SysTick_ITConfig () – Enable SysTick interrupt 
3. Call SysTick_CLKSourceConfig() – Set SysTick clock source. 
4. Call SysTick_SetReload() – Set SysTick reload value. 
5. Call SysTick_ITConfig () – Enable SysTick interrupt 
6. Call SysTick_CounterCmd() – Start SysTick counter

Systick interrupt service function 
void SysTick_Handler(void);

The register version 
uses an external 8M clock, the frequency from the phase-locked loop is 72M, and the AHB pre-divided frequency is 72M. 
Systick fixes 1/8 of the HCLK clock, that is, 9M, so the delay of 1us is 9 clocks.

void delay_init(u8 SYSCLK) //The system clock is 72MHz, SYSCLK=72{
    SysTick->CTRL &= 0xfffffffb ; //bit2 is cleared to 0, that is, the configuration selects the external clock
    fac_us=SYSCLK/8; //Hardware frequency division by 8, the value obtained by fac_us is used for the following clock function
    fac_ms =(u16)fac_us*1000;

}void delay_us(u32 nus) //nus if 10us{
    u32 temp;
    SysTick->LOAD = nus*fac_us; // If the delay is 10us, it is 10*9=90, which is loaded into the load register
    SysTick->VAL=0x00; //The counter is cleared to 0, because when the currrent field is manually cleared, load will automatically reload into VAL
    SysTick->CTRL = 0x01; //Configure the exception to take effect, that is, an exception notification will be issued when the counter counts down to 0
    do
    {
       temp = SysTick->CTRL; //After the time is up, this bit will be set to 1 by hardware, but will be automatically cleared to 0 after being queried
    }    while(temp & 0x01 && !(tmep &(1<<16))); //查询
    SysTick->CTRL = 0x00; // Turn off the counter
    SysTick->VAL = 0x00; //clear val}12345678910111213141516171819202122

The last while loop determines if Systick is still in the Enable state and the counter has not reached 0, 
then it will keep looping to write the current SysTick->CTRL register value into the variable temp and continue the next judgment. 
When Systick is Disabled or the counter reaches 0, the loop stops.

=====End=====


Keywords:stm32  Cortex  SysTick Reference address:STM32 Cortex system timer (SysTick)

Previous article:stm32 assert_param function
Next article:STM32 PWM output experiment

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号