In today’s STM32 tutorial we are exploring other ways to quickly or properly creating delays. We will also talk about little pro and cons of stm32 delay ms function using these different methods. You will also notice in this tutorial that we will recommend to use HAL library as suggested by ST(manufactures of STM32 based micro controllers). There are also need to create some microsecond delay like delay_us function. But we will leave that part on reader to further explore and experiment the ways as learning exercise. So let’s not waste time and directly dive in to the real fun.
Delay is very important function in any embedded system application. Specially if we are talking about Arduino or similar embedded development platform like STM32 based. Even a simple Hello world! embedded system application like blinking the LED, also require this delay function. Delay_ms function is normally abbreviated to create millisecond delay. We require at-least millisecond delay so that we can create pause for human visualize able perception. If we take our previous example where we created our first Hello world application using STM32CubeIDE, you may have been noticed that we used Hal_Delay function. This is normal and popular way to create delays in STM32 platform specially in modern development tool-chain using cubemx or STM32CubeIDE( or specifically talking) using HAL library.
Available Methods for Creating Delay
Before comparing let’s list down the possible ways to create the stm32 delay ms function in your c/c++ projects. It does not matter either you using Keil UVision or TrueStudio or STM32CubeMX IDE. The methods will remain same if you are using C/C++ language for your project.
- Idle looping based software delays
- delay ms function using systick timer
- general purpose timer based delays
- HAL based delay function
There are also some other custom ways to create delay, one example could be seen in this example.
STM32 delay ms function: HAL Delay
ST now recommend to use HAL libraries over old standard peripheral libraries. Previous std libraries do not provide any method for creating delays but modern HAL based libraries provide efficient and accurate way to create delay. You can use hal delay as shown in code below.
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
STM32 delay ms function: Software based
Software based delay in STM32 is simply just like creating delay for any other micrcontroller which support C/C++. The only difference that you may see in the code is the __IO macro which will show that the value should do not be optimized while creating optimized final object code. Otherwise, it’s simply a nop based for or while loop. Most common practice is to use for loop like this
Problem with Software based delays:
Although creating for loop is easy without forcing us to write lot of comlex code. It is still not accurate. You cannot correctly estimate the delay. You can only make assumptions. So it is only advisable where you do not need accurate solution. One more point to keep in mind is that these delays are blocking calls. Which means, your code will get stuck when goes into the delay loop and will stay there until and unless the loop is complete.