How to create delay in nano/micro seconds using timers in stm32

  Рет қаралды 47,315

ControllersTech

ControllersTech

Күн бұрын

Пікірлер: 118
@박동진-y9q
@박동진-y9q 4 жыл бұрын
it helped me a lot, i really thank you for sharing this video
@yohannsalliege1974
@yohannsalliege1974 2 жыл бұрын
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
@youssefsallak3339
@youssefsallak3339 Жыл бұрын
I have a question how can I calculate a very short pulse between 1nanoseconds this is to calculate the speed of light and thanks
@sangtruong8833
@sangtruong8833 Жыл бұрын
at 6.33, the screen shows pulse width about 0.1 nano second, is right?
@maso553
@maso553 Жыл бұрын
Hi Controllers tech, what is the name of the device being used an an Oscilloscope in the video?
@ControllersTech
@ControllersTech Жыл бұрын
DSO138
@jayasharma4208
@jayasharma4208 4 жыл бұрын
Thank you sir, your videos are very helpful!, Can I implement same procedure for getting microsecond delay in nucleo STM8S?
@ControllersTech
@ControllersTech 4 жыл бұрын
Yeah the same logic can be applied for any microcontroller
@uguraltunbilek
@uguraltunbilek 2 жыл бұрын
nice video .Thank you so much for this valuable information
@devjeetmandal1472
@devjeetmandal1472 5 жыл бұрын
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
@ControllersTech
@ControllersTech 5 жыл бұрын
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
@devjeetmandal1472 5 жыл бұрын
@@ControllersTech To run my timer in 100MHz how are you calculating the values for prescale and arr regsiter.??.
@MrHause13
@MrHause13 4 жыл бұрын
@@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.
@TheOldProgramming
@TheOldProgramming 4 жыл бұрын
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 :)
@ControllersTech
@ControllersTech 4 жыл бұрын
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
@TheOldProgramming
@TheOldProgramming 4 жыл бұрын
@@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?
@ControllersTech
@ControllersTech 4 жыл бұрын
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
@iforce2d
@iforce2d 4 жыл бұрын
Thankyou, very helpful.
@malekmahar
@malekmahar 3 жыл бұрын
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
@ControllersTech
@ControllersTech 3 жыл бұрын
What delay value are u using ?
@saikrishnakaruturi2958
@saikrishnakaruturi2958 Жыл бұрын
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
@ControllersTech Жыл бұрын
I think you are looking for dead time. Google it, there are some examples on it. I haven't covered it yet
@saikrishnakaruturi2958
@saikrishnakaruturi2958 Жыл бұрын
@@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
@nielshib635
@nielshib635 2 жыл бұрын
Clock is 80Mhz and pre scalar is set to 80. What is the maximum us delay that can be achieved using this function?
@ControllersTech
@ControllersTech 2 жыл бұрын
65535 us (16 bit counter, so max = 0xffff)
@willleemaipipi
@willleemaipipi 4 жыл бұрын
Why d not just use Hal_delay() function? any difference betrween?
@ControllersTech
@ControllersTech 4 жыл бұрын
Hal delay provides minimum delay of 1 millisecond. This video shows how to create delays in microsecond
@jayasharma4208
@jayasharma4208 4 жыл бұрын
I am using stm32f0 board, and maximum hclk frequency is 48MHZ , how to generate delay of microsecond????
@ControllersTech
@ControllersTech 4 жыл бұрын
Watch the video properly. It depends on APB clock, where the timer is connected.
@The2Coolest2
@The2Coolest2 3 жыл бұрын
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.
@ControllersTech
@ControllersTech 3 жыл бұрын
Well you should change everything based on your controller..
@The2Coolest2
@The2Coolest2 3 жыл бұрын
@@ControllersTech I meant I did not change anything over the months I used it.
@ControllersTech
@ControllersTech 3 жыл бұрын
There must have been some changes.. you need to recheck every detail i guess..
@paulg.3067
@paulg.3067 4 жыл бұрын
Is this your own voice? If not, what speech synthesis are you using?
@ControllersTech
@ControllersTech 4 жыл бұрын
I use amazon polly. Neural engine
@ganeshgaikwad2158
@ganeshgaikwad2158 5 жыл бұрын
Hello sir.. please guide me for conncting compact flash card to stm32f373 ....
@albertziatdinov1373
@albertziatdinov1373 4 жыл бұрын
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 :))))
@ControllersTech
@ControllersTech 4 жыл бұрын
The code does have HAL tim base start. I guess i forgot to mention it. I will put it in the description..
@yunusemreyagan4946
@yunusemreyagan4946 4 жыл бұрын
Hello controllers Tech. Can i add dead time to PWM signals generated in Timer 3 channels on stm32f407 discovery board?
@ControllersTech
@ControllersTech 4 жыл бұрын
Dead time ?
@yunusemreyagan4946
@yunusemreyagan4946 4 жыл бұрын
@@ControllersTech yes
@emreiris114
@emreiris114 4 жыл бұрын
Yes. But It requires a compliment timer.
@design4682
@design4682 3 жыл бұрын
@@emreiris114 can you share the code regarding this?
@JoaoNelsonLima
@JoaoNelsonLima 3 жыл бұрын
Great job guy
@AnhTuNguyenanhtunguyen
@AnhTuNguyenanhtunguyen 4 жыл бұрын
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
@ControllersTech 4 жыл бұрын
Did u start the timer in the main function.. Hal_Tim_base_start
@AnhTuNguyenanhtunguyen
@AnhTuNguyenanhtunguyen 4 жыл бұрын
​@@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.
@ControllersTech
@ControllersTech 4 жыл бұрын
Can u telegram me @controllerstech
@kobalov1
@kobalov1 4 жыл бұрын
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
@ControllersTech
@ControllersTech 4 жыл бұрын
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
@kobalov1
@kobalov1 4 жыл бұрын
@@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 !!!!
@sujoybhattacharya9938
@sujoybhattacharya9938 4 жыл бұрын
This is not able to give me 10ns delay in stm32h743zi2?
@vinithkumar5964
@vinithkumar5964 17 күн бұрын
In this video he is explaining something and in reference page , its completely different clock setting. which one to follow
@ControllersTech
@ControllersTech 17 күн бұрын
The clock settings depends on your mcu. How to configure it is explained in both, the video and on the reference page as well.
@mohamedazharudeen6477
@mohamedazharudeen6477 4 жыл бұрын
what is the settings for creating milli seconds delay using this fucntion
@ControllersTech
@ControllersTech 4 жыл бұрын
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
@johnsaeid95
@johnsaeid95 4 жыл бұрын
hello what if have 80MHZ for timer then I need trigger 48000 in one second Is that doable ?
@ControllersTech
@ControllersTech 4 жыл бұрын
You want 48000 times high (freq 96000), or both high and low (freq 48000) ?
@ControllersTech
@ControllersTech 4 жыл бұрын
In either case u can use the prescalar value = TIM CLK / Required frequency. Keeping the ARR at max
@johnsaeid95
@johnsaeid95 4 жыл бұрын
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 ??
@ControllersTech
@ControllersTech 4 жыл бұрын
In that case, better use the APB timer clock as 48 KHz. And prescalar as 0
@johnsaeid95
@johnsaeid95 4 жыл бұрын
@@ControllersTech how is this possible I need to keep my sysCLK 80MHZ anyway I sent you an email on admin@controllerstech.com
@SadiqAli-qp3kl
@SadiqAli-qp3kl 2 жыл бұрын
Can you guide for 100ns delay, with 48mhz clock....using stm32f07
@ControllersTech
@ControllersTech 2 жыл бұрын
Just follow the video. It explains the process
@SadiqAli-qp3kl
@SadiqAli-qp3kl 2 жыл бұрын
@@ControllersTech I tried but its just limiting to 1 micros sec.
@ControllersTech
@ControllersTech 2 жыл бұрын
Use a better mcu with higher speed
@polatozcanlar2600
@polatozcanlar2600 4 жыл бұрын
100 000 000/(1*65536)=1525.87 Hz How does it works on 100 mhz? Am I wrong?
@ControllersTech
@ControllersTech 4 жыл бұрын
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.
@polatozcanlar2600
@polatozcanlar2600 4 жыл бұрын
​@@ControllersTech TY for answer but in 2:23 isnt it prescalar? İts 1 or i am blind?
@ControllersTech
@ControllersTech 4 жыл бұрын
that's for the nanosecond delay. prescalar 1 means the timer clock would be at 100 MHz, and each count will take 10ns.
@binodanio2199
@binodanio2199 Жыл бұрын
where is the prescalar as 100, i see only prescalar as 1-1 i 2:23@@ControllersTech
@ControllersTech
@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.
@해헤-u9e
@해헤-u9e 2 жыл бұрын
thanks to help.
@devjeetmandal1472
@devjeetmandal1472 5 жыл бұрын
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
@ControllersTech 5 жыл бұрын
What's the timer frequency ?
@devjeetmandal1472
@devjeetmandal1472 5 жыл бұрын
APB2 TIMER CLOCKS :180MHz
@ControllersTech
@ControllersTech 5 жыл бұрын
First bring ur timer frequency to 100MHz, than set the prescalar to 0 and ARR to 0xffff-1
@devjeetmandal1472
@devjeetmandal1472 5 жыл бұрын
@@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
@ControllersTech
@ControllersTech 5 жыл бұрын
I need to see the code. Telegram me @controllerstech
@exaltedcodiing6655
@exaltedcodiing6655 3 жыл бұрын
I have exactly do what you have done, but my programme is not giving any delay. prescaler=0 arr=0xffff-1 APB2=100Mhz
@ControllersTech
@ControllersTech 3 жыл бұрын
In the main function, call the timer start Hal_tim_start(handler)
@exaltedcodiing6655
@exaltedcodiing6655 3 жыл бұрын
the frequency or time do not match the delay timming
@ControllersTech
@ControllersTech 3 жыл бұрын
U can contact on discord or telegram
@exaltedcodiing6655
@exaltedcodiing6655 3 жыл бұрын
@@ControllersTech i am there,
@RixtronixLAB
@RixtronixLAB 4 жыл бұрын
How can I get 20 seconds timer3 interrupt ? cheers
@ControllersTech
@ControllersTech 4 жыл бұрын
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
@RixtronixLAB
@RixtronixLAB 4 жыл бұрын
@@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;
@RixtronixLAB
@RixtronixLAB 4 жыл бұрын
Make sure you enable the update event interrupt What should I update?
@ControllersTech
@ControllersTech 4 жыл бұрын
But this will give you the delay of 8 sec max..
@RixtronixLAB
@RixtronixLAB 4 жыл бұрын
@@ControllersTech How can I make it more? TIM_TimeBaseStructure.TIM_ClockDivision = 1;?
@ramalingeswararaobhavaraju5813
@ramalingeswararaobhavaraju5813 4 жыл бұрын
Thank.
@innomus9109
@innomus9109 4 жыл бұрын
Is this code blocking mode?
@ControllersTech
@ControllersTech 4 жыл бұрын
Yes
@innomus9109
@innomus9109 4 жыл бұрын
@@ControllersTech Hi thanks for the reply. Any suggestions how I can have the same delays but in none blocking mode?
@ControllersTech
@ControllersTech 4 жыл бұрын
Use timer interrupt if the delay is large. Interrupt is pointless in smaller delays
@juhigullu
@juhigullu 4 жыл бұрын
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
@ControllersTech
@ControllersTech 4 жыл бұрын
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 ?
@juhigullu
@juhigullu 4 жыл бұрын
@@ControllersTech 84
@juhigullu
@juhigullu 4 жыл бұрын
so how to put modifications?
@ControllersTech
@ControllersTech 4 жыл бұрын
Put prescalar as 84-1 and ARR as 0xffff-1
@andrey7530
@andrey7530 4 жыл бұрын
Ths man!!!
@gharibissam7033
@gharibissam7033 4 жыл бұрын
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
@ControllersTech
@ControllersTech 4 жыл бұрын
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.
@gharibissam7033
@gharibissam7033 4 жыл бұрын
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
@ControllersTech
@ControllersTech 4 жыл бұрын
Yeah sure... Send the project.
@gharibissam7033
@gharibissam7033 4 жыл бұрын
Please chick your email box. Thank you
@gharibissam7033
@gharibissam7033 4 жыл бұрын
Hello Sir, I'd like to know if you have received my email message. Thank you for help
SD CARD using SPI in STM32 || Cube-IDE || File handling || uart
16:42
ControllersTech
Рет қаралды 89 М.
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 13 МЛН
Synyptas 4 | Жігіттер сынып қалды| 3 Bolim
19:27
kak budto
Рет қаралды 1,1 МЛН
小天使和小丑太会演了!#小丑#天使#家庭#搞笑
00:25
家庭搞笑日记
Рет қаралды 54 МЛН
4. How to create a precision delay with stm32 timer
17:21
CMTEQ
Рет қаралды 2,1 М.
Stm32 Delay Using Timers or Systick
24:42
Eddie Amaya
Рет қаралды 36 М.
PoE+ NVMe beats Raspberry Pi to the punch
15:31
Jeff Geerling
Рет қаралды 181 М.
Using I2C for any device on STM32 with HAL | VIDEO 26
37:51
Matej Blagšič
Рет қаралды 78 М.
HAL #10: HowTo Timer with Interrupt
5:18
Web learning
Рет қаралды 49 М.
STM32 Guide #3: PWM + Timers
20:24
Mitch Davis
Рет қаралды 145 М.
Receive data using UART in STM32 || Poll || Interrupt || DMA
14:07
ControllersTech
Рет қаралды 125 М.