1. Function prototype
In the function library officially provided by STM32, you can find functions like HAL_Delay(). This function uses a timer to achieve a more accurate time delay and provides it to the user for calling.
This function is usually included in a function like stm32f4xx_hal.c. The function prototype is as follows:
__weak void HAL_Delay(__IO uint32_t Delay)
{
uint32_t tickstart = 0U;
tickstart = HAL_GetTick();
while((HAL_GetTick() - tickstart) < Delay)
{
}
}
The input parameter is the time required for delay, in milliseconds (ms). The HAL_GetTick() function is called to obtain the count value uwTick, which is incremented in the mid-segment service function.
__weak uint32_t HAL_GetTick(void)
{
return uwTick;
}
The interrupt service function is as follows:
void SysTick_Handler(void)
{
uwTick++;
}
This interrupt service function is the interrupt response of the system timer SysTick. The initialization function of the timer HAL_InitTick() is defined in the stm32f4xx_hal.c file and is called in the HAL_Init() function.
Check its initialization function HAl_InitTick(), the content is as follows:
__weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
/*Configure the SysTick to have interrupt in 1ms time basis*/
HAL_SYSTICK_Config(SystemCoreClock/1000U);
/*Configure the SysTick IRQ priority */
HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0U);
/* Return function status */
return HAL_OK;
}
This function first sets the interrupt generation cycle for the timer, for example, 1ms in the current case, which means an interrupt is generated every millisecond. The second step is to set the interrupt priority for the timer.
2. Function Description
When using delay, the user directly calls the function HAl_Delay(time) and fills in the required delay time in milliseconds. For example, if 5000 is filled in, it means a delay of 5 seconds. During this period, the MCU will generate 5000 interrupts, and the interrupt service function will increase the count value by one 5000 times.
The core statement of the delay function is the while loop, as follows:
while((HAL_GetTick() - tickstart) < Delay)
{
}
This function will keep looping when the condition is met, but since the loop body is empty, the loop does not actually generate any operations until the condition is not met, that is, the count value minus the start delay value after the count value is continuously incremented is greater than the delay value. At this time, the condition is not met, the loop ends, and the program continues to execute.
Regarding the while loop above, you can also use a version written using a for loop, as follows:
for( ;(HAL_GetTick() - tickstart) < Delay; );
That is, using only one condition of the for loop, this code is equivalent to the following code:
for(;;)
{
if((HAL_GetTick() - tickstart) > Delay)
break;
}
3. More explanation
1. Regarding the SysTick timer, the data sheet describes it as follows:
2. Efficiency description of for and while loops
Previous article:The Delay_ms() function is stuck during STM32 hardware debugging
Next article:STM32F103C8T6 reads PT100 resistance value through MAX31865
- Popular Resources
- Popular amplifiers
- Learn ARM development(14)
- Learn ARM development(15)
- 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)
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
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- 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?
- Microstrip transmission line impedance matching engineering example
- Portable health monitor for the elderly
- [Chuanglong TL570x-EVM] HDMI error
- CircuitPython 5.0.0 Beta 1 released
- Features of CAN bus
- [Help] What is the purpose of impulse withstand voltage test? What is the difference between it and surge?
- Pi Cast celebrates Raspberry Pi's 10th anniversary
- Verilog task call
- [RVB2601 Creative Application Development] + 2 DEMO Trials
- EEWORLD University ---- RT-Thread Studio