How to create 1 microsecond delay in STM32

ARM, Embedded, STM32
6 Comments

Description

THIS METHOD IS ONLY TESTED WITH CORTEX M3 and M4 SERIES MICROCONTROLLERS. FOR MORE EFFECTIVE AND UNIVERSAL METHOD PLEASE VISIT THE VIDEO HERE

HAL_Delay is able to provide minimum 1ms delay but when it comes to microsecond, there isn’t any predefined function to create 1us delay. In this tutorial, I am going to show you how to create 1 microsecond delay in STM32. The process will be same for all the STM32 devices, you need to make a minor change though.

WHY Delay in MicroSeconds

Delay in microseconds is needed in order to interface some sensors for eg- DHT11/22. These sensors contains only one PIN for the data transfer and data is transferred bidirectionally. They need precise timing in order to work. For eg- To initialize DHT22, microcontroller need to pull the data pin low for 500us and than pull high for 40us. After that DHT22 start transmitting it’s response by pulling the line low for 80us followed by pulling high for 80us.

After searching a lot on the internet, finally I got the code which works. I don’t remember where I got it from so if someone knows the writer, do let me know in the comments below. I will give the credits for the work.

I will show the working on the oscilloscope, You can check the  RESULTS tab for more details. I am using STM32F103C8 cortex M3 microcontroller but the process is same for other STM32 devices.

How to setup

This project contains a header file (dwt_stm32_delay.h) and a c file (dwt_stm32_delay.c). We need to include it in the main project. I am using keil and the process is described below:

First setup the project from the CubeMx and right click the Application/User and select add existing files to group

add both files in the project

Next we have to include the path in the project so follow along

Now include the location of the folder, where the files are copied

As you can see above that the folder is included in the path

Next you have to include dwt_stm32_delay.h in the main file by using #include “dwt_stm32_delay.h” and build the project. Now you will be able to see the *.h and *.c file in the list.

NOTE:-  Inside dwt_stm32_delay.h, change the #include”*.h” file according to your microcontroller.

You can use DWT_Delay_Init (); to initialize the delay and DWT_Delay_us (microseconds) for the delay in us.

 

Result

The following is the result I got by toggling the pin at 10us, 100us and 1000us respectively.

Pin Toggle after each 10 microseconds

Pin Toggle after each 100 microseconds

Pin Toggle after each 1000 microseconds

 

 

DOWNLOAD

You can buy me a coffee sensor 🙂

download the CODE below

, , , ,

6
Leave a Reply

avatar
4 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
DmitryPaco CantonSalvatoreadminNagesh Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Iraj Alipour
Guest
Iraj Alipour

Thanks for your code . it`s very good

Nagesh
Guest
Nagesh

Hai ,Can you help me to get HAL microsecond delay in STM32L073RZ board for one wire communication

Salvatore
Guest
Salvatore

Hi,
This code doesn’t work on STM32L073RZ.
The DWT (Data Watchpoint and Tracing) is only available on Cortex M3/4/7.
So, if you want a delay of 1 us, you can use a Timer or the systick.

Dmitry
Guest
Dmitry

Hello! I left my comment on youtube video. But this theme is a little bit old, I do not know will You answer me here or on youtube. Thank you very much. It works with stm32f4-discovery. But I have one problem with nucleo f767zi. It works when I programm by Keil and all the time after, until I power off and power on again. After that my code freezes when meet dwt_delay. I programm again – all works again. Do You have any ideas why? F767zi get clock not from ceramic crystal, it goes bypass clock source from st-link on… Read more »

Menu