STM32 Systick Timer

Publisher:BlissfulDreamsLatest update time:2016-12-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Q: What is the SysTick timer? 
SysTick is a 24-bit countdown timer. When it counts to 0, it will automatically reload the initial value of the timer from the RELOAD register. As long as its enable bit in the SysTick control and status register is not cleared, it will never stop. 
Q: Why set the SysTick timer? 
(1) Generate the clock beat of the operating system. 
The SysTick timer is tied to the NVIC and is used to generate the SYSTICK exception (exception number: 15). In the past, most operating systems required a hardware timer to generate the tick interrupt required by the operating system as the time base of the entire system. Therefore, a timer is needed to generate periodic interrupts, and it is best not to allow user programs to access its registers at will to maintain the rhythm of the operating system's "heartbeat". 
(2) Facilitate program porting between different processors. 
The Cortex-M3 processor contains a simple timer. Because all CM3 chips have this timer, the porting of software between different CM3 devices is simplified. The clock source of this timer can be the internal clock (FCLK, the free-running clock on the CM3) or the external clock (STCLK signal on the CM3 processor). 
However, the specific source of STCLK is determined by the chip designer, so the clock frequency between different products may be very different. You need to check the device manual of the chip to decide what to choose as the clock source. The SysTick timer can generate an interrupt. CM3 has a special exception type for it and it has a place in the vector table. It makes it much easier to port the operating system and other system software between CM3 devices because it is handled the same in all CM3 products. 
(3) As an alarm to measure time. 
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 stop operating. 
Q: How does Systick work? 
First set the counter clock source, CTRL->CLKSOURCE (control register). Set the reload value (RELOAD register) and clear the count register VAL (CURRENT in the figure below). Set CTRL->ENABLE to start timing. 
If it is an interrupt, allow Systick interrupts and handle them in the interrupt routine. If the query mode is used, read the COUNTFLAG flag of the control register continuously to determine whether the timing is zero. Or take one of the following methods. 
When the SysTick timer counts from 1 to 0, it will set the COUNTFLAG bit; and the following method can clear it: 
1. Read the SysTick Control and Status Register (STCSR) 
2. Write any data to the SysTick Current Value Register (STCVR). 
Only when the VAL value is 0, the counter automatically reloads RELOAD. 
Q: How to use SysTicks as a system clock? 
The biggest mission of SysTick is to generate exception requests regularly as the system's time base. The OS needs this "tick" to promote task and time management. To enable the SysTick exception, set STCSR.TICKINT. In addition, if the vector table is relocated to SRAM, it is also necessary to create a vector for the SysTick exception and provide the entry address of its service routine. 
Q: How to use SysTick to complete a delay? 
Query method reference: http://blog.ednchina.com/atom6037/188271/message.aspx 
Interrupt method reference: 
Put the initialization function SysTick_Configuration(void) outside the while() loop and execute it once: 
view plaincopy to clipboardprint? 
void SysTick_Configuration(void) 

/* Select AHB clock(HCLK) as SysTick clock source Set AHB clock to SysTick clock*/ 
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); 
/* Set SysTick Priority to 3 Set SysTicks interrupt preemption priority 3, from priority 0*/ 
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0); 
/* SysTick interrupt each 1ms with HCLK equal to 72MHz SysTick interrupt occurs every 1ms*/ 
SysTick_SetReload(72000); 
/* Enable the SysTick Interrupt */ 
SysTick_ITConfig(ENABLE); 

void SysTick_Configuration(void) 

/* Select AHB clock(HCLK) as SysTick clock source Set AHB clock as SysTick clock */ 
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); 
/* Set SysTick Priority to 3 Set SysTicks interrupt preemption priority 3, from priority 0 */ 
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0); 
/* SysTick interrupt each 1ms with HCLK equal to 72MHz 
SysTick_SetReload(72000); 
/* Enable the SysTick Interrupt */ 
SysTick_ITConfig(ENABLE); 

Delay function, call when delay is needed: 
view plaincopy to clipboardprint? 
void Delay(u32 nTime) 

/* Enable the SysTick Counter */ 
SysTick_CounterCmd(SysTick_Counter_Enable); 
TimingDelay = nTime; 
while(TimingDelay != 0) 
; //Wait for the count to reach 0 
/* Disable the SysTick Counter */ 
SysTick_CounterCmd(SysTick_Counter_Disable); 
/* Clear the SysTick Counter */ 
SysTick_CounterCmd(SysTick_Counter_Clear); 

void Delay(u32 nTime) 

/* Enable the SysTick Counter */ 
SysTick_CounterCmd(SysTick_Counter_Enable); 
TimingDelay = nTime; 
while(TimingDelay != 0) 
; //Wait for the count to reach 0 
/* Disable the SysTick Counter Disable SysTick counter */ 
SysTick_CounterCmd(SysTick_Counter_Disable); 
/* Clear the SysTick Counter Clear SysTick counter */ 
SysTick_CounterCmd(SysTick_Counter_Clear); 

Interrupt function, called when the timer is reduced to zero, placed in the stm32f10x_it.c file

Keywords:STM32 Reference address:STM32 Systick Timer

Previous article:STM32 learning assert_failed
Next article:STM32-External Interrupt Experiment

Recommended ReadingLatest update time:2024-11-15 13:52

Initializing ADXL345 chip in STM32
Initialize the ADXL345 chip void ADXL345init() {       Single_WriteI2C(0x31,0x0B); //Low level output, 13-bit full resolution, output data right-aligned, 16g range  Single_WriteI2C(0X2C,0x0A); //Data output rate is 100HZ, read data once every 10ms     Single_WriteI2C(0X2D,0x28); //Link enable, measurement mo
[Microcontroller]
STM32--How to generate 8-channel PWM wave
Experimental description: PA0, PA1, PA2, PA3, PA8, PA9, PA10, PA11 output PWM waves with different duty cycles Program Listing: #include"PWM.H" #include "stm32f10x.h" void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure;  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //启动GPIO RCC_APB2PeriphCloc
[Microcontroller]
[STM32 motor vector control] Record 13——EXTI external interrupt
The STM32 EXTI controller supports 19 external interrupt/event requests. Each interrupt has a status bit, and each interrupt/event has independent trigger and mask settings. The 19 external interrupts of STM32 correspond to 19 interrupt lines, namely EXTI_Line0-EXTI_Line18: Lines 0~15: correspond to the input interr
[Microcontroller]
[STM32 motor vector control] Record 13——EXTI external interrupt
Regular garbled characters encountered in STM32 serial communication
      I have been learning to debug serial communication these days. For several days, the serial port has been sending and receiving garbled characters. The previous garbled characters were irregular. I found that the serial port hardware was not configured properly. There are three configurations here. (1) Initializ
[Microcontroller]
Regular garbled characters encountered in STM32 serial communication
What does GPIOB->BSRR mean in stm32
GPIOB->BSRR = 0x01 is to raise GPIOB port 0 to a high level GPIOB- BRR = 0x01 is to reduce the GPIOB port 0 to a low level GPIOB- BSRR = 0x02 is to raise GPIOB port 1 to a high level GPIOB- BRR = 0x02 is to reduce the GPIOB port 1 to a low level GPIOB- BSRR = 0x04 is to raise GPIOB port 2 to a high level GPIOB- BRR =
[Microcontroller]
Communication between STM32 spi and FPGA
I've been studying the SPI bus recently, so I won't go into details about the protocol and hardware description. The four lines include clock, chip select, receive, and send initialization.   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //Full-duplex   SPI_InitStructure.SPI_Mode = SPI_Mode_Maste
[Microcontroller]
STM32 MCU (8) Program encryption
References http://www.cnblogs.com/pied/archive/2011/06/08/2075481.html http://v.pps.tv/play_38DQ4J.html#vfrm=8-8-0-1 related articles Serial port interrupt communication  http://blog.csdn.net/leytton/article/details/38393553 Serial communication printf redirection  http://blog.csdn.net/leytton/article/details/3839
[Microcontroller]
STM32 LED light based on 3.5 library 2
The 3.5 library is here: http://www.51hei.com/mcu/2765.html #include "stm32f10x.h" /*********************************************************************** ************************************************************************/ GPIO_InitTypeDef GPIO_InitStructure; /**********************************************
[Microcontroller]
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号