STM32 learning notes - using SysTick timer for delay

Publisher:缘到泉Latest update time:2015-08-14 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
   School has started, and I can no longer play with the board all day like I did during the holidays! Fortunately, the number of classes this semester is not too few, but not too many either, with more than a dozen classes per week. In the remaining time, apart from student work and other miscellaneous things, I can still read tutorials and play with the board from time to time! I find that the "STM32 Novice Learning Manual - Long-winded Edition" is a really good introductory tutorial, simple and easy to understand. Other official documents such as the "Firmware Library Manual" are even more indispensable, but it's a pity that it's really troublesome to find manuals on the ST company's website!

   I have been looking at documents about SysTick these days, but since the 3.5 version of the firmware library has removed the driver functions related to SysTick compared to the 2.0 version, users must call the functions in CMSIS to use SysTick, and most of the examples on the Internet (including the "Rookie Learning Manual") use the 2.0 library, so I encountered many problems in learning, and there were always problems with program compilation. Generally, the error is "Cannot find the function related to SysTick, the function is undefined". Therefore, I searched a lot of literature to solve it.

   The SysTick timer is bundled in the NVIC and is used to generate the SysTick exception (exception number: 15). It is mainly used in the operating system as a "tick interrupt" to maintain the rhythm of the operating system's "heartbeat". Of course, in addition to serving the operating system, the SysTick timer can also be used for other purposes: such as as an alarm, for measuring time, etc. It should be noted that when the processor is halted during debugging, the SysTick timer will also be suspended.

1.SysTick register:

register

illustrate

address

CTRL

SysTick Control and Status Register

0xE000E010

LOAD

Systick reload value register

0xE000E014

VAL

Systick Current Value Register

0xE000E018

CALIB

SysTick Calibration Value Register

0xE000E01C

 

      SysTick Control and Status Register (SysTick_CTRL)

STM32 learning notes - using SysTick timer for delay

    illustrate:

CLKSOURCE bit: CM3 allows two clock sources to be selected for SysTick. The first is the core's "free running clock" FCLK. "Free" means that it does not come from the system clock HCLK, so FCLK continues to run when the system clock stops. The second is an external reference clock. However, when using an external clock, because it is sampled internally by FCLK, its period must be at least twice that of FCLK (sampling theorem). In many cases, chip manufacturers will ignore this external reference clock, so it is usually not available.

COUNTFLAG bit: When the SysTick timer counts from 1 to 0, it will set the COUNTFLAG bit; the following method can clear it:

Read the SysTick control and status register (STCSR);

Write any data to the SysTick Current Value Register (STCVR).

 

      SysTick reload value register (SysTick_LOAD)

STM32 learning notes - using SysTick timer for delay

   

    illustrate:

   When the counter counts down to 0, use the SysTick_LOAD register to specify the initial value loaded into the "Current Value Register". The initial value can be any value between 1 and 0x00FFFFFF. [page]

 

      Systick current value register (SysTick_VAL)

STM32 learning notes - using SysTick timer for delay

      SysTick calibration value register (SysTick_CALIB)

STM32 learning notes - using SysTick timer for delay

 

Schematic diagram of each register:

STM32 learning notes - using SysTick timer for delay

2. SysTick function

The STM32 firmware library after version 3.0 removes the SysTick driver in the standard peripheral library, so users must call the CMSIS-defined functions.

CMSIS only provides a SysTick setting function, which replaces all the functions of the original SysTick driver of STM32.

 

[cpp]  view plain copy
 
  1. SysTick_Config(uint32_t ticks);  

 

This function sets the value of the auto-reload counter (LOAD), the priority of the SysTick IRQ, resets the value of the counter (VAL), starts counting and enables the SysTick IRQ interrupt. The SysTick clock uses the system clock by default.

In addition, you can use the SysTick_CLKSourceConfig function to change the clock source and use NVIC_SetPriority to set the interrupt priority (more on this later).

SysTick_CLKSourceConfig function

Function name

SysTick_CLKSourceConfig

Function prototype

SysTick_CLKSourceConfig(u32 SysTick_CLKSource)

Behavior Description

Configure the SysTick clock source

Input parameters

SysTick_CLKSource: Clock source of SysTick

Output parameters

none

return value

none

Prerequisites

none

Call functions

none

 

SysTick_CLKSource allowed values

SysTick_CLKSource

describe

SysTick_CLKSource_HCLK_Div8

The SysTick clock source is 1/8 of the AHB clock

SysTick_CLKSource_HCLK

The SysTick clock source is the AHB clock

 [page]

example:

Select 1/8 of the AHB clock as the SysTick clock source

 

[cpp]  view plain copy
 
  1. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);  

 

 

3. Routines

3.1 Program Code

This example code is modified from the example code for lighting up the LED lamp, using SysTick timing delay. Except for the delay, other codes remain unchanged, and the statements related to SysTick are commented.

 

[cpp]  view plain copy
 
  1. #include "stm32f10x.h"  
  2. void Delay(u32 nTime); //Declare delay function  
  3. void GPIO_Configuration(void);  
  4. int main(void)  
  5. {  
  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);  
  7. GPIO_Configuration();  
  8. while(SysTick_Config(SystemCoreClock/1000)!=0); //Configure SysTick and load the initial value. The load value depends on the clock source frequency. A 72MHz clock source generates a 1ms interrupt and the load value needs to be (72000000/1000)  
  9.   while(1)  
  10.   {  
  11.    GPIO_ResetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9);  
  12.    GPIO_SetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8);  
  13.    Delay(1000);  
  14.    GPIO_ResetBits(GPIOC,GPIO_Pin_6|GPIO_Pin_8);  
  15.    GPIO_SetBits(GPIOC,GPIO_Pin_7|GPIO_Pin_9);  
  16.    Delay(1000);  
  17.    GPIO_Write(GPIOC,0x0140);  
  18.    Delay(1000);  
  19.    GPIO_Write(GPIOC,0x0280);  
  20.    Delay(1000);  
  21.    }  
  22. }  
  23.    
  24. void GPIO_Configuration(void)  
  25. {  
  26.   GPIO_InitTypeDefGPIO_InitStructure;  
  27.  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;  
  28.  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;  
  29.  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;  
  30.  GPIO_Init(GPIOC,&GPIO_InitStructure);  
  31. }  
  32.    
  33. volatile u32 TimingDelay; //define global variables, which are declared in stm32f10x_it.c  
  34. void Delay(u32 nTime) //define delay function  
  35. {  
  36. TimingDelay=nTime; //Assign the delay number to the global variable  
  37. while(TimingDelay!=0);  
  38. }  

 

Among them, in stm32f10x_it.c:

 

[cpp]  view plain copy
 
  1. extern volatile u32 TimingDelay; //declare global variables  
  2. void SysTick_Handler(void)  
  3. {  
  4.   TimingDelay--;  
  5. }  

 

3.2 Results

After compiling and burning into the development board, the LEDs will flash alternately with an accurate interval of 1 second.

 

references:

[1] Joseph Yiu, translated by Song Yan. "Cortex-M3 Authoritative Guide" [EB/OL]. http://ishare.iask.sina.com.cn/f/11378333.html?retcode=0, 2010-11-05/2012-09-09.

[2] ST. "Comparison of STM32 firmware library 2.0.3 and 3.0 versions (Chinese version)" [EB/OL]. http://ishare.iask.sina.com.cn/f/18297257.html?from=like, 2011-08-22/2012-09-09.

[3]Xxbing8.STM32_SysTick[EB/OL].http://hi.baidu.com/xxbing8/item/c99ea4f53f996ad042c36ab8 2012-06-14/2012-09-09.


Keywords:STM32 Reference address:STM32 learning notes - using SysTick timer for delay

Previous article:STM32 study notes - using the general timer TIM2 for precise delay
Next article:STM32 learning notes-preparation

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号