1. SysTick Introduction:
The processing of the CM4 core is the same as that of the CM3. It contains a SysTick timer. SysTick is a 24-bit countdown timer. When it reaches 0, it will automatically reload the initial value of the timer from the RELOAD register. As long as the enable bit in the SysTick control and status register is not cleared, it will never stop. We use the internal SysTick of STM32 to achieve delay, which does not occupy interrupts or system timers.
Usually SysTick can be implemented by interruption, which will be added later, but currently it is only implemented by polling
2. Register Introduction
SysTick has 4 registers
The corresponding code is in core_cm4.h
typedefstruct
{
__IO uint32_t CTRL; /*!< Offset: 0x000(R/W) SysTick Control and StatusRegister */
__IO uint32_t LOAD; /*!< Offset: 0x004(R/W) SysTick Reload Value Register */
__IO uint32_t VAL; /*!< Offset: 0x008(R/W) SysTick Current ValueRegister */
__I uint32_t CALIB; /*!< Offset: 0x00C (R/ ) SysTick Calibration Register */
} SysTick_Type;
1) CTR register as shown in the figure:
Bit 0: ENABLE, Systick enable bit (0: turn off Systick function; 1: turn on Systick function)
Bit 1: TICKINT, Systick interrupt enable bit (0: disable Systick interrupt; 1: enable Systick interrupt)
Bit 2: CLKSOURCE, Systick clock source selection (0: use HCLK/8 as Systick clock; 1: use HCLK as Systick clock)
Bit 16: COUNTFLAG, Systick count comparison flag, if SysTick has counted to 0 after the last read of this register, this bit is 1. If this bit is read, it will be automatically cleared.
2) The LOAD register is as shown in the figure:
Systick is a decrementing timer. When the timer decrements to 0, the value in the reload register will be reloaded and continue to decrement. STK_LOAD reload register is a 24-bit register with a maximum count of 0xFFFFFF.
3) VAL register as shown in the figure:
This is also a 24-bit register. When read, it returns the current countdown value. Writing it clears it to zero and also clears the COUNTFLAG flag in the SysTick control and status register.
4) CALIB register as shown in the figure
Generally not used
3. Source code
delay.h
#ifndef _DELAY_H_H_H
#define _DELAY_H_H_H
#include "stm32f4xx.h"
void delay_init(u8 SYSCLK);
void delay_ms(u16 nms);
void delay_us(u32 nus);
#endif
delay.c
#include "delay.h"
static u8 fac_us=0;
static u16 fac_ms=0;
void delay_init(u8 SYSCLK)
{
SysTick->CTRL&=~(1<<2);
fac_us=SYSCLK/8;
fac_ms=((u32)SYSCLK*1000)/8;
}
void delay_xms(u16 nms)
{
u32 temp;
SysTick->LOAD=(u32)nms*fac_ms;
SysTick->VAL =0x00;
SysTick->CTRL=0x01 ;
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(1<<16)));
SysTick->CTRL=0x00;
SysTick->VAL =0X00;
}
void delay_ms(u16 nms)
{
u8 repeat=nms/540;
u16 remain=nms%540;
while(repeat)
{
delay_xms(540);
repeat--;
}
if(remain)delay_xms(remain);
}
void delay_us(u32 nus)
{
u32 temp;
SysTick->LOAD=nus*fac_us;
SysTick->VAL=0x00;
SysTick->CTRL=0x01 ;
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&!(temp&(1<<16)));
SysTick->CTRL=0x00;
SysTick->VAL =0X00;
}
Previous article:【stm32f407】IO pin multiplexing and mapping
Next article:【stm32f407】Clock tree and SystemInit analysis
Recommended ReadingLatest update time:2024-11-16 17:42
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Specifications of several types of batteries
- [Lazy people take care of fish tank control system] Debug method in ON Semiconductor IDE environment
- W5500 UDP Issues
- Please tell me what does it mean to subscribe to OBJECT resources in the China Mobile IoT ONENET platform?
- Low power design technology overview.doc
- Can anyone recommend a DCDC module with an isolation strength of more than 6000VAC?
- Design a circuit for corridor voice-controlled lights that includes a single-chip microcomputer.
- Getting Started with the ST SensorTile.box Sensor Kit (2) Getting Started Mode Experience
- FAQ_How to implement low power mode and low power wake-up function in 24G private protocol applications
- Problem calling EPwm1Regs.TBCTR value