First of all thanks for this easy and useful tutorial. I wanted to clarify few things and i hope you can give me some answers for it. Suppose my mcu is running at 180MHz and my timer1 is at 90MHz, then how are you determining the lowest possible delay in ns? As you said your controller can generate only at least of 10ns delay, i wanted to know how you came to that conclusion that it can generate only at least of 10ns? What are the required calculations needed so by choosing any mcu of any clock speed i can determine the lowest possible delay i can generate.? Regards
See, let's consider that the timer 1 is running at 100 MHz. That means one cycle would take (1/100Mhz) = 10ns. That's how it is calculated. Also if your timer is running at 90 Mhz, than one cycle would take (1/90MHz) = 11 ns. If you want to create a delay of 1ns, your timer should have a frequency of (1/10^-9) = 1000 MHz. That's not possible for now.
@@devjeetmandal1472 I don't undarstand your question. You said that your timer1 is at 90MHz. So you can't get 10ns, because u need at least 100MHz clock. You set clock for timer as you want because it is possible in stm32. So for example you take the highest speed (why not) what is 90MHz in your case. So you know that it's not impossible to get 10ns delay. So go for the next one which is 100ns. All what you need is to set cycle to 10MHz, because it give you 100ns. So set your prescaler as (9-1). 90MHz/9=9MHz. And arr register the highest possible value. You don't really use the arr register because you don't use interrupt after counting to the set value. Timer is just counting to set value, then after reach top value it is set to 0. Other way is to set clock for this timer to 10MHz then prescaler to (1-1). The arr register is use to set length (to which numer your timer count) with defined frequency.
Hi thanks for the tutorial! If my max clock speed is 80mhz can i divide it 800-1 and get a timer which counts 10microseconds? Then can i have an interrupt that triggers when the timer reaches 10microseconds after its activation? This is needed for me to be able to read hc-sr04 sensor without active waiting with a while loop? Would it work? EDIT: What will be the counter value? Thanks in advance :)
Yeah that would work. You need the periodic delay. Also prescalar doesn't depends on hclk clock. You need to check the APB timer clock od the timer that you are using. In case apb is 80MHz than prescalar can be 800 and counter 0. This would create a delay of 10 MHz
@@ControllersTech Hi thanks for the answer! I am trying to measure the time for distance measure using the timer(set at 1us) and an interrupt(set on rising and falling edge) on the echo pin of the hc-sr04 sensor to read everything in non blocking mode. Basically i use the same timer for triggring the sensor and then when the interrupt rises i just restart the timer and each time the timer counts i check if the echo pin goes low and if so i stop and compute the distance using the time counted. But this is not working D: I have to read 4 hc-sr04 sensors. If i use direct input mode on 4 channels of the same timer can i measure the time from each these 4 channels concurrently? Would this work?
i don't understand the point of using the interrupt. It's just 10us, and the entire processing doesn't take much time. you can still run other functions
Thank you very much for this method. But it can't works, cause you not startet TIM Base generation. For correct starting must write HAL_TIM_Base_Start(&htim1); before while(1) loop. Either your TIM Base Generation startet somewhere automatically - it is HAL man :))))
Is it possible sir, after the update event is occured on timer2 in timer synchronisation is it possible to add delay for that timer2. Please give me your reply?
@@ControllersTech no sir not the dead time I want to give some delay after the update event is occurred. So that what I want to do to get some delay after the update event is occurred
My delay function is suddenly not working and I do not know why. It's been working for months. For example, a 10uS delay is producing a 17uS delay instead on the oscilloscope. I did not change anything.
I thing you didn't understood it properly we entered the prescalar as 100, so the timer clock is 100/100 = 1 MHz Each count in the ARR takes 1 us yeah the ARR is 65536, but we are not counting upto TOP of the counter. just count as many microseconds delay you need.
@binodanio2199 prescaler of 100 is not used in the video. But if you want the delay in us, with the APB timer clock of 100MHz you need to set the prescaler 100.
Firstly, thanks to your video. But I tried a lot with stm32f446re nucleo and the LED doesn't blink. I have even used usb logic analyzer and still got nothing...
@@ControllersTech I mean, I did the same as you but the delay is not same. I used Nucleo STM32F446RE with APB2 is 100 MHz, Prescaler 1-1, AutoReload Register is 0xffff-1 and delay function with delay parameter is 10000 but the result is not 10ns.
my timer just doesnt start :( it stays at while loop forever. I rly don't understand why it doesnt work I have set up everything in config and have exact same code. Maybe my ADC1 analog read is somehow messing with the timer ? or my definition of outputs idk
If u want to test the timer, run the timer alone. Don't use any adc code. Just blink the led after some delay Make sure that you have started time using hal tim base start function
@@ControllersTech I don't see the command to start timer in your timer tutorial but thank you very much I added it and followed temperature one wire sensor code and after some trying I got it working :) it's reading the temperature and my analog read from potentiometer is also working!!! Thank you so much :D !!!!
No I want 48khz (the timer occurs 48khz ) The reason for the question is that I guess 48khz can't be achieved exactly there will be always 10 or 20 above or lower 48khz , Am I right ??
I tried to create 10ns delay but it doesn't work... I have set prescale to 0 and arr register to 18. And it gives me 1us delay. My controller is stm32f44zet6 nucleo
@@ControllersTech yeah it was a mistake i kept timer1 at 180MHz now i have changed it to 100MHz. But somehow i am getting rise time 20ns and fall time 30ns. Period is around 1.18us and frequency is 847.4 KHz
Just divide the timer 3 prescalar such that the timer frequency is 1000 hz. That means the timer will take 1ms for each count. Now set the ARR as 20000 to have a delay of 20 sec. Make sure you enable the update event interrupt
my hclk is showing max 84hz when i try to change it to 180 it is not changing ...showing 84hz max ..what to do? then what to put the value at apb2? i am using stm32f401re
Awesome work ! Thank you so much. micro-seconds works fine with me but I still have problem with ns delay, it doesn't work... I have set prescale and arr register to many possibilities. And it gives me always a delay value > 1us . My controller is STM32F413ZH nucleo. I can send you my code. Many thanks for your help
Thank you for fast responding. Actually it's 100 MHz and I need a delay of 50 ns for my application. Do you like that I send you my code via your email admin@controllerstech.com ? Thank you so much
it helped me a lot, i really thank you for sharing this video
nice video .Thank you so much for this valuable information
Thankyou, very helpful.
Hi I have the same nucleo F446RE but didn't manage to get 10 ns, the minimum I can get with delay (1) is around 530 ns, it acts like there is a limit
Great job guy
Is this your own voice? If not, what speech synthesis are you using?
I use amazon polly. Neural engine
I have a question how can I calculate a very short pulse between 1nanoseconds this is to calculate the speed of light and thanks
at 6.33, the screen shows pulse width about 0.1 nano second, is right?
I am using stm32f0 board, and maximum hclk frequency is 48MHZ , how to generate delay of microsecond????
Watch the video properly. It depends on APB clock, where the timer is connected.
Hi Controllers tech, what is the name of the device being used an an Oscilloscope in the video?
DSO138
Thank you sir, your videos are very helpful!, Can I implement same procedure for getting microsecond delay in nucleo STM8S?
Yeah the same logic can be applied for any microcontroller
First of all thanks for this easy and useful tutorial. I wanted to clarify few things and i hope you can give me some answers for it. Suppose my mcu is running at 180MHz and my timer1 is at 90MHz, then how are you determining the lowest possible delay in ns? As you said your controller can generate only at least of 10ns delay, i wanted to know how you came to that conclusion that it can generate only at least of 10ns? What are the required calculations needed so by choosing any mcu of any clock speed i can determine the lowest possible delay i can generate.?
Regards
See, let's consider that the timer 1 is running at 100 MHz. That means one cycle would take (1/100Mhz) = 10ns. That's how it is calculated.
Also if your timer is running at 90 Mhz, than one cycle would take (1/90MHz) = 11 ns.
If you want to create a delay of 1ns, your timer should have a frequency of (1/10^-9) = 1000 MHz.
That's not possible for now.
@@ControllersTech To run my timer in 100MHz how are you calculating the values for prescale and arr regsiter.??.
@@devjeetmandal1472 I don't undarstand your question. You said that your timer1 is at 90MHz. So you can't get 10ns, because u need at least 100MHz clock. You set clock for timer as you want because it is possible in stm32. So for example you take the highest speed (why not) what is 90MHz in your case. So you know that it's not impossible to get 10ns delay. So go for the next one which is 100ns. All what you need is to set cycle to 10MHz, because it give you 100ns. So set your prescaler as (9-1). 90MHz/9=9MHz. And arr register the highest possible value. You don't really use the arr register because you don't use interrupt after counting to the set value. Timer is just counting to set value, then after reach top value it is set to 0. Other way is to set clock for this timer to 10MHz then prescaler to (1-1). The arr register is use to set length (to which numer your timer count) with defined frequency.
thanks to help.
Hi thanks for the tutorial! If my max clock speed is 80mhz can i divide it 800-1 and get a timer which counts 10microseconds? Then can i have an interrupt that triggers when the timer reaches 10microseconds after its activation? This is needed for me to be able to read hc-sr04 sensor without active waiting with a while loop? Would it work? EDIT: What will be the counter value? Thanks in advance :)
Yeah that would work. You need the periodic delay. Also prescalar doesn't depends on hclk clock. You need to check the APB timer clock od the timer that you are using.
In case apb is 80MHz than prescalar can be 800 and counter 0. This would create a delay of 10 MHz
@@ControllersTech Hi thanks for the answer! I am trying to measure the time for distance measure using the timer(set at 1us) and an interrupt(set on rising and falling edge) on the echo pin of the hc-sr04 sensor to read everything in non blocking mode.
Basically i use the same timer for triggring the sensor and then when the interrupt rises i just restart the timer and each time the timer counts i check if the echo pin goes low and if so i stop and compute the distance using the time counted.
But this is not working D:
I have to read 4 hc-sr04 sensors. If i use direct input mode on 4 channels of the same timer can i measure the time from each these 4 channels concurrently? Would this work?
i don't understand the point of using the interrupt. It's just 10us, and the entire processing doesn't take much time. you can still run other functions
Clock is 80Mhz and pre scalar is set to 80. What is the maximum us delay that can be achieved using this function?
65535 us (16 bit counter, so max = 0xffff)
Hello sir.. please guide me for conncting compact flash card to stm32f373 ....
Why d not just use Hal_delay() function? any difference betrween?
Hal delay provides minimum delay of 1 millisecond. This video shows how to create delays in microsecond
Thank you very much for this method. But it can't works, cause you not startet TIM Base generation. For correct starting must write HAL_TIM_Base_Start(&htim1); before while(1) loop. Either your TIM Base Generation startet somewhere automatically - it is HAL man :))))
The code does have HAL tim base start. I guess i forgot to mention it. I will put it in the description..
Hi, I followed every step you did, including HAL_TIM_Base_Start and the code doesn't run, it gets blocked in the delay_us function
What delay value are u using ?
Is it possible sir, after the update event is occured on timer2 in timer synchronisation is it possible to add delay for that timer2. Please give me your reply?
I think you are looking for dead time. Google it, there are some examples on it. I haven't covered it yet
@@ControllersTech no sir not the dead time I want to give some delay after the update event is occurred. So that what I want to do to get some delay after the update event is occurred
This is not able to give me 10ns delay in stm32h743zi2?
Thank.
My delay function is suddenly not working and I do not know why. It's been working for months. For example, a 10uS delay is producing a 17uS delay instead on the oscilloscope. I did not change anything.
Well you should change everything based on your controller..
@@ControllersTech I meant I did not change anything over the months I used it.
There must have been some changes.. you need to recheck every detail i guess..
Hello controllers Tech.
Can i add dead time to PWM signals generated in Timer 3 channels on stm32f407 discovery board?
Dead time ?
@@ControllersTech yes
Yes. But It requires a compliment timer.
@@emreiris114 can you share the code regarding this?
what is the settings for creating milli seconds delay using this fucntion
You just have to divide the clock such that the final timer clock will be of 1000 hz. So each count in ARR will take 1 millisecond
100 000 000/(1*65536)=1525.87 Hz How does it works on 100 mhz? Am I wrong?
I thing you didn't understood it properly
we entered the prescalar as 100, so the timer clock is 100/100 = 1 MHz
Each count in the ARR takes 1 us
yeah the ARR is 65536, but we are not counting upto TOP of the counter.
just count as many microseconds delay you need.
@@ControllersTech TY for answer but in 2:23 isnt it prescalar? İts 1 or i am blind?
that's for the nanosecond delay. prescalar 1 means the timer clock would be at 100 MHz, and each count will take 10ns.
where is the prescalar as 100, i see only prescalar as 1-1 i 2:23@@ControllersTech
@binodanio2199 prescaler of 100 is not used in the video. But if you want the delay in us, with the APB timer clock of 100MHz you need to set the prescaler 100.
Firstly, thanks to your video. But I tried a lot with stm32f446re nucleo and the LED doesn't blink. I have even used usb logic analyzer and still got nothing...
Did u start the timer in the main function.. Hal_Tim_base_start
@@ControllersTech I mean, I did the same as you but the delay is not same. I used Nucleo STM32F446RE with APB2 is 100 MHz, Prescaler 1-1, AutoReload Register is 0xffff-1 and delay function with delay parameter is 10000 but the result is not 10ns.
Can u telegram me @controllerstech
my timer just doesnt start :( it stays at while loop forever. I rly don't understand why it doesnt work I have set up everything in config and have exact same code. Maybe my ADC1 analog read is somehow messing with the timer ? or my definition of outputs idk
If u want to test the timer, run the timer alone. Don't use any adc code. Just blink the led after some delay
Make sure that you have started time using hal tim base start function
@@ControllersTech I don't see the command to start timer in your timer tutorial but thank you very much I added it and followed temperature one wire sensor code and after some trying I got it working :) it's reading the temperature and my analog read from potentiometer is also working!!! Thank you so much :D !!!!
Ths man!!!
Can you guide for 100ns delay, with 48mhz clock....using stm32f07
Just follow the video. It explains the process
@@ControllersTech I tried but its just limiting to 1 micros sec.
Use a better mcu with higher speed
hello
what if have 80MHZ for timer
then I need trigger 48000 in one second
Is that doable ?
You want 48000 times high (freq 96000), or both high and low (freq 48000) ?
In either case u can use the prescalar value = TIM CLK / Required frequency. Keeping the ARR at max
No I want 48khz (the timer occurs 48khz )
The reason for the question is that I guess 48khz can't be achieved exactly there will be always 10 or 20 above or lower 48khz , Am I right ??
In that case, better use the APB timer clock as 48 KHz. And prescalar as 0
@@ControllersTech how is this possible I need to keep my sysCLK 80MHZ
anyway I sent you an email on admin@controllerstech.com
I have exactly do what you have done, but my programme is not giving any delay.
prescaler=0
arr=0xffff-1
APB2=100Mhz
In the main function, call the timer start
Hal_tim_start(handler)
the frequency or time do not match the delay timming
U can contact on discord or telegram
@@ControllersTech i am there,
Is this code blocking mode?
Yes
@@ControllersTech Hi thanks for the reply. Any suggestions how I can have the same delays but in none blocking mode?
Use timer interrupt if the delay is large. Interrupt is pointless in smaller delays
I tried to create 10ns delay but it doesn't work... I have set prescale to 0 and arr register to 18. And it gives me 1us delay. My controller is stm32f44zet6 nucleo
What's the timer frequency ?
APB2 TIMER CLOCKS :180MHz
First bring ur timer frequency to 100MHz, than set the prescalar to 0 and ARR to 0xffff-1
@@ControllersTech yeah it was a mistake i kept timer1 at 180MHz now i have changed it to 100MHz. But somehow i am getting rise time 20ns and fall time 30ns. Period is around 1.18us and frequency is 847.4 KHz
I need to see the code. Telegram me @controllerstech
How can I get 20 seconds timer3 interrupt ? cheers
Just divide the timer 3 prescalar such that the timer frequency is 1000 hz. That means the timer will take 1ms for each count. Now set the ARR as 20000 to have a delay of 20 sec.
Make sure you enable the update event interrupt
@@ControllersTech PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 1000) - 1; //TIM3 counter clock at 1000Hz
__IO uint16_t CCR1_Val = 1000/1;
__IO uint16_t CCR2_Val = 1000/0.5;
__IO uint16_t CCR3_Val = 1000/0.25;
__IO uint16_t CCR4_Val = 1000/0.125;
Make sure you enable the update event interrupt
What should I update?
But this will give you the delay of 8 sec max..
@@ControllersTech How can I make it more?
TIM_TimeBaseStructure.TIM_ClockDivision = 1;?
my hclk is showing max 84hz when i try to change it to 180 it is not changing ...showing 84hz max ..what to do?
then what to put the value at apb2?
i am using stm32f401re
The max clock in F401RE is 84MHz. You can't run it higher than that. I use f446re, that have 180 MHz.
How much is APB2 clock ?
@@ControllersTech 84
so how to put modifications?
Put prescalar as 84-1 and ARR as 0xffff-1
Awesome work ! Thank you so much.
micro-seconds works fine with me but I still have problem with ns delay, it doesn't work... I have set prescale and arr register to many possibilities. And it gives me always a delay value > 1us . My controller is STM32F413ZH nucleo. I can send you my code.
Many thanks for your help
What's the max HCLK possible for your controller ?
To create delay of 1ns, timer frequency must be 1GHz
For 10ns -> 100 MHz
For 100ns -> 10 MHz.
Thank you for fast responding. Actually it's 100 MHz and I need a delay of 50 ns for my application. Do you like that I send you my code via your email admin@controllerstech.com ?
Thank you so much
Yeah sure... Send the project.
Please chick your email box. Thank you
Hello Sir,
I'd like to know if you have received my email message.
Thank you for help