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:
Register Description
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=====
Previous article:stm32 assert_param function
Next article:STM32 PWM output experiment
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Commonly used algorithms for drones - Kalman filter (VI)
- Serial port experiment
- IoT products have more and more functions. How can we solve the problem of insufficient MCU memory?
- 【GD32E503 Review】 Summary of Simple Oscilloscope Experiment Project
- Weekend small topic
- JD.com also started selling components
- [Qinheng RISC-V core CH582] Use WCH_Link online simulation to debug LED light cycle
- Quickly learn how to choose the right resistor!
- With the highest increase of 358% on the first day of listing, how long will the A-share chip "newcomer" Cambrian continue to suffer huge losses?
- Linux driver programming - ch340x driver transplantation