Writing Systick tick timer and delay function for STM32

Publisher:asdfrewqppLatest update time:2019-01-15 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. What is the Systick tick timer?

    

The Systick timer is a system tick timer, a 24-bit countdown timer that automatically reloads the timing initial value from the RELOAD register when it counts to 0. As long as the enable bit in the SysTick control and status register is not cleared, it will never stop and can work even in sleep mode.


A timer is used to execute an action after a specified time.


 Systick timer is often used for delay or heartbeat clock of real-time system. This can save MCU resources and avoid wasting a timer. For example, in UCOS, time-division multiplexing requires a minimum timestamp. Generally, in STM32+UCOS system, Systick is used as UCOS heartbeat clock.


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


2. Systick is generally used for delay and precise delay.


There are 4 Systick registers in total


    CTRL SysTick Control and Status Register 


    LOAD SysTick automatically reloads the initial value register 


    VAL SysTick current value register 


    CALIB SysTick Calibration Value Register


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


3. Systick timer principle


This timer sets an initial value, and then decrements the initial value to 0, which means the timing is completed. After completion, an interrupt can be generated or not.



Bit 0 is the enable bit, which can enable or disable the timer


The first bit is the interrupt enable bit, which determines whether an interrupt is generated after the subtraction is completed.


The second bit is the clock selection bit, which can select external or internal clock as the clock source.


The 16th bit is the flag bit. After the number is subtracted to 0, this bit is set to 1 and is automatically cleared after being read.


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


4. uSysTick reload value register-LOAD



When the current value register is decremented to 0, the value of RELOAD will be automatically assigned to the current value register.


if(VAL == 0)


    VAL = RELOAD


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


5. SysTick Current Value Register - VAL



VAL is counted down from the initial value (RELOAD) by one, and when it reaches 0, the initial value (RELOAD) is reassigned to VAL.


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


6. Implementation of tick timer


For STM32, the external clock source is 1/8 of HCLK (AHB bus clock) and the core clock is HCLK clock


1. Select the clock source


    SysTick_CLKSourceConfig(); //Select the clock source in the misc.c file



    SysTick_Config(uint32_t ticks) //Initialize systick, set the clock to HCLK, and enable interrupt in core_cm4.h file



2. Write a delay function


3. Set interrupt priority grouping


    NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2);


    Generally, when using interrupts, we need to group our priorities at the beginning, and then set the priorities after the grouping.


    Try not to change groups frequently in the same program. Frequent switching of groups may cause unexpected errors in the program.


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


7. Use interrupts to implement delay

Systick interrupt service function: void SysTick_Handler(void);



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


Notice:


  In uCortex-M system, Systick code can be used universally.


  If you find that the delay is inconsistent during use, the problem is usually caused by different core clocks. You can modify the ticks value.


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


The reference code is as follows:


void init_delay(void)

{

    /*Our external crystal oscillator is 8MHz, and then multiplied to 168M, then the Systick clock is 21M, which is the Systick counter

    Every time VAL decreases by 1, it means 1/21us has passed*/

 

    //1. Select the clock source (select external clock)

    //External clock needs to be /8, internal clock is never used

    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); 

}

 

//2. Write a delay function

//The maximum delay does not exceed 798.915ms, which is (2^24)/21/1000

void delay_us(u32 nus)

{

    u32 temp = 0;

 

    //1. Realize a delay of 1us*nus

    //21 is the external clock frequency, if you choose the internal clock it is 168M/8

    SysTick->LOAD = 21*nus; //Set the automatic load value to 21 (1us)

    SysTick->VAL = 0x00; //Set the current initial value to 0

    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // Turn on (enable) counting: enable SysTick timer

 

    do

    {

        //Read the control register

        temp = SysTick->CTRL;

    }while(!(temp & (1<<16)));//Wait for the count time to arrive (bit 16)

 

    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // Disable count

    SysTick->VAL = 0x00; //Set the current initial value to 0 and reset VAL 

}

 

void delay_ms(u32 nms)

{

    u32 temp = 0;

 

    //1. Realize a delay of 1us*nus

    SysTick->LOAD = 21000*nms; //Set the automatic load value to 21 (1us)

    SysTick->VAL = 0x00; //Set the current initial value to 0

    SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // Turn on (enable) counting

 

    do

    {

        //Read the control register

        temp = SysTick->CTRL;

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

 

    SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // Disable count

    SysTick->VAL = 0x00; //set the current initial value to 0 

}

 

void delay_s(u32 s)

{

    while(s--)

    {

        delay_ms(500);

        delay_ms(500);

    }

}

 

Keywords:STM32 Reference address:Writing Systick tick timer and delay function for STM32

Previous article:STM32 serial port (UART) and serial communication principle
Next article:STM32 clock system architecture

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

Design of FSMC interface driving TFT color screen based on STM32
  0 Introduction   With the continuous updating of electronic products, more and more display interfaces are being developed. Due to the high cost-effectiveness of TFT color screens, they are widely used as display screens in various electronic devices. At present, there are many solutions to drive TFT color screens
[Microcontroller]
Design of FSMC interface driving TFT color screen based on STM32
stm32 enters standby mode
When using the library function PWR_EnterSTANDBYMode() to enter the standby mode, it always displays "Error : no definition for "__WFI" ..." After more than two hours, I finally found out that the cortexm3_macro.s file was missing. After downloading the file, put the assembly code directly in C, asm("WFI"); One
[Microcontroller]
STM32 JTAG debug interface PB3, PB4 multiplexing problem
JTAG interface: mainly used for internal chip testing. Most advanced devices now support the JTAG protocol, and the standard wiring is 4 wires. TMS: JTAG interface mode selection TCK: JTAG interface clock TDI: JTAG interface data input TDO: JTAG interface data output TRST: JTAG optional pin, function as test re
[Microcontroller]
STM32 study notes USART
USART Functional Overview The interface is connected to other devices via three pins (see Figure 248). Any USART bidirectional communication requires at least two pins: receive data input (RX) and transmit data output (TX). RX: Receive data serial input. Use oversampling technology to distinguish data from noise and
[Microcontroller]
stm32 RS485 SP3485
RS485 is half-duplex communication (2-wire)   The DE and RE of the SP3485 chip are short-circuited together and connected to the PG3 of the STM32F1 chip. The PG3 pin can be used to control the transmission and reception of the SP3485. When PG3=0, it is in the receiving mode, and when PG3=1, it is in the sending mod
[Microcontroller]
stm32 RS485 SP3485
STM32 learning----clock
In STM32, there are five clock sources: HSI, HSE, LSI, LSE, and PLL. ①. HSI is a high-speed internal clock, an RC oscillator, with a frequency of 8MHz. ②. HSE is a high-speed external clock, which can be connected to a quartz/ceramic resonator or an external clock source, with a frequency range of 4MHz~16MHz. ③. LSI
[Microcontroller]
Read the raw data of MPU6050 module with STM32
    In many application fields, acceleration sensors and gyroscope sensors are indispensable. MPU6050 is simple, economical and compact, and is very suitable for development with low requirements.         1. MPU6050 is a 6-axis motion processing component, including 3-axis acceleration and 3-axis gyroscope     2. MPU6
[Microcontroller]
Use keil to burn stm32 and pay attention to changing the address
  Pay attention to modifying the RAM and ROM addresses along the way, especially when setting up IAP.
[Microcontroller]
Use keil to burn stm32 and pay attention to changing the address
Latest Microcontroller Articles
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号