STM32 delay function analysis

Publisher:陈风102Latest update time:2018-04-23 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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:

Write the picture description here

2. Efficiency description of for and while loops

Keywords:STM32 Reference address:STM32 delay function analysis

Previous article:The Delay_ms() function is stuck during STM32 hardware debugging
Next article:STM32F103C8T6 reads PT100 resistance value through MAX31865

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号