For STM32, there is a tool that is often used but rarely mentioned in the data sheet, that is Systick. This tool is common to all microcontrollers with the cortex-M0 core. It is a system timer, and its main purpose is to provide a 100Hz (10ms) timing beat for the embedded operating system. Of course, it can also be used for other purposes such as ordinary timing. Next, let's start to understand this timer.
register
The system timer includes four registers, SYST_CSR, SYST_RVR, SYST_CVR, SYST_CALIB. The definitions are as follows:
For these registers, we can translate:
SYST_CSR register, system timer control and status register
SYST_RVR register, system timer reload value register
SYST_CVR register, system timer current value register
SYST_CALIB register, system timer calibration register
This is very clear, let's look at these registers one by one.
First is the SYST_CSR register:
The CSR register uses 4 bits, bit0 is used to start the timer, bit1 is used to generate an interrupt, bit2 is used to select whether the timer clock source is equal to the main clock or equal to half of the main clock, and bit16 is the status of the timer.
The SYST_RVR register
RELOAD value can be any value in the range of 0x00000001-0x00FFFFFF. You can set the RELOAD value to 0, which will not have any effect, because the SysTick exception request and COUNTFLAG are both activated when the count value changes from 1 to 0. If you want to generate a multi-trigger timer with a period of N processor clock cycles, you can set the RELOAD value to N-1. For example, if you require a SysTick interrupt to be triggered every 100 clock pulses, RELOAD is set to 99.
SYST_CVR register
The CVR register uses bits 0 to 23, which is a 24-bit number. This is a status register. When the timer starts running, this value is constantly changing. After getting the initial value from the RVR register, it counts down to 0.
SYST_CALIB register
The calibration register SYST_CALIB does not need to be considered by us, as it is configured before leaving the factory.
function
The system's own Systick function is provided by CMSIS (for more information about what CMSIS is, go to Baidu and search it), and is located in the core_cm0.h file. In the 656th line, there is the following function
__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
{
if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */
SysTick->LOAD = ticks - 1; /* set reload register */
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Systick Interrupt */
SysTick->VAL = 0; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_TICKINT_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
return (0); /* Function successful */
}
From the statement “SysTick->LOAD = ticks – 1;” inside the function, we know that ticks is the LOAD value, that is, the reload value, which represents the count of two interrupts.
For example, to generate a 10ms interrupt, you can call the function in the program as follows: Systick_Config(SystemCoreClock/100); SystemCoreClock in the function parameter is the value of the current main frequency. If the current main frequency is 48MHz, SystemCoreClock is 48 000 000, 48 000 000/100=480 000. After we bring in the parameters, LOAD=479 999, that is, after the timer starts running, the timer value will decrease from 479 999 to 0, enter the interrupt function, and then decrease from 479 999 to 0 again, and so on. As for why it takes 10ms to decrease from 479 999 to 0? Knowing how long it takes to decrease by one value, then how long it takes to decrease by 480 000, you will know. To know how long it takes to decrease by one value, you need to know the clock of the current timer. From the register CSR, we know that there are two types of timer clocks, one is equal to the main frequency, and the other is equal to half of the main frequency, which is determined by bit 2 in the CSR register.
In the function, bits 0, 1 and 2 of the control register are all set to 1. By comparing the previous register definition, the clock is set to be equal to the main frequency, the system timer interrupt is turned on, and the timer is allowed to run.
Now that we know the clock, we know the time required for the timer to decrement a value, which is 1/SystemCoreClock seconds. Converted into milliseconds, it is (1/SystemCoreClock)*1000=1000/SystemCoreClock milliseconds, which means that it takes 1000/SystemCoreClock milliseconds to decrement a value. So if you want to make the timing 10ms, that is, 10/(1000/SystemCoreClock)=SystemCoreClock/100, so our settings are correct. Similarly, you can calculate how long the timing needs to be, and bring it in with a parameter. It should be noted that the LOAD value is a 24-bit number, and the number brought in should not exceed the maximum value of a 24-bit number. Another thing to note is that the minimum LOAD value is 255. When you bring in a value less than 255 for the LOAD value, LOAD will automatically become 255. Since the timer SYST_RVR register is 24 bits, the maximum value is hexadecimal FFFFFF, which is decimal 16777215, so the maximum data that can be stored is 16777215+1=16777216;
Timer delay
Using the functions in CMSIS we can write the following functions:
__IO uint32_t msTick;
void SysTick_Handler(void) // interrupt function
{
msTick++;
}
void delay_ms(IO uint32_t ms)
{
uint32_t endTime=msTicks+ms;
SysTick_Config((SystemCoreClock/1000)*ms);
while(msTicks } It can delay the time with an accuracy of 1ms. In addition, SystemCoreClock/1000 can be modified to delay the time with different time.
Previous article:STM32 SysTick (system timer)
Next article:STM32 tick timer (Systick) detailed analysis
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- SparkRoad Review (7) - FPGA Serial Port Test
- Disable AD auto-start JLink
- Seeking guidance - stc microcontroller remote upgrade program
- Problems with creating sheet symbols for multi-page schematics
- Zigbee Z-Stack 3.0.1 Modify channels using broadcasting
- The STM32 FFT library calculates the amplitude normally, but the phase is different each time. Has anyone encountered this problem?
- EEWORLD University----UCD3138 Analog Front End (AFE) Module
- "Show goods" to come to a wave of commonly used development boards
- Measurement of the phase difference between a sine wave and a square wave
- [Sipeed LicheeRV 86 Panel Review] - 6 waft-ui component tests (3)