HAL library 3- DELAY for STM32Fxxx

Delay functions are needed in your program, no matter how optimized and fast program you wanna do.

Delay functions

I’ve updated my delay library to support milliseconds and microseconds delays. Milliseconds based delay is done using systick timer which makes interrupts every 1ms generated by HAL library. For microseconds based delay, DWT cycle counter is used to get maximal optimized delay.

DWT unit is for F4 and F7 only, F0 series does not have DWT, because of Cortex-M0.

Software timers

Library supports software timers generation. Software timer has resolution of 1ms and counts down. It can be used to generate events each X milliseconds in your project.


Read more about new HAL libraries


  • Delay for amount of milliseconds
  • Delay for amount of microseconds
  • Software timers with callback support


  • HAL
  • TM
    • STM32Fxxx HAL
    • defines.h

Functions and enumerations

Example 1

Example 2

Example 3

Projects are available on Github, download all libraries below.

TM STM32 Libraries

STM32 libraries based on STM32Fxxx HAL drivers.


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Pingback: All STM32 HAL libraries - STM32F4 Discovery()

  • h.mehrabian

    I have an error at line 256 in stm32f4xx_hal_gpio.h in Delay Project that download from Github. Would u mind if u check this?

    • Would you mind to tell which one?

      • h.mehrabian

        It seems, I was wrong. Sorry!!

  • Daniel

    How can I create a microseconds timer? i need to read the DHT11 Data signal…

  • Dalton john

    Hi Majerle, i dont undestand, why is error “defines.h” file not found? thanks 🙂

    • You dobt have it. Create it.

      • Dalton john

        where i do put file “defines.h” in User or in MDK ?

        • Doesnt care. Folders in keil are just for user organization.

          • Dalton john

            thank Majerle 🙂

  • Andy JT

    Hi, Is there something different to get the DWT to enable on STM32F7 ?
    When I try DWT->CTRL |= 0x00000001; then the register does not change (it stays at 0x40000000)
    Thanks in advance.

    • It is the same. Check my “GENERAL” library how I do it.

      • Andy JT

        It’s the code from your library that I’m using, like I say, the register to start DWT does not change.
        Is there some protection on the register memory maybe ?
        The device is STM32F746NG
        This is the code:

  • MK

    What is the reason for diving by 5 in line:
    micros *= (SystemCoreClock / 1000000) / 5;
    Thanks in advance for your answer

    • This is used if you don’t want timer. Division is because of number of instructions per one counter in ARM and while loop. This delay is very inefficient because each optimization level changes this.