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

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 มิ.ย. 2024
  • Purchase the Products shown in this video from :: controllerstech.store
    ________________________________________________________________________________________
    You have to start timer after initializing everything. Before while loop, just put the function HAL_Tim_Base_Start (&htim1);
    To download the code and for more explanation, goto controllerstech.com/create-1-...
    Check out more VIDEOS on TIMERS in STM32 • STM32 TIMERS
    ________________________________________________________________________________________
    ****** SUPPORT US BY DONATING*****
    paypal.me/controllertech
    ******Join the Membership******
    / @controllerstech
    Join the Discord Server / discord
    Join the Telegram Group t.me/controllerstechdiscuss
    Follow me on Instagram / controllerstech
    For more info, visit www.controllerstech.com
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 116

  • @user-jr6zs2nx7z
    @user-jr6zs2nx7z 4 ปีที่แล้ว +2

    it helped me a lot, i really thank you for sharing this video

  • @uguraltunbilek
    @uguraltunbilek ปีที่แล้ว

    nice video .Thank you so much for this valuable information

  • @iforce2d
    @iforce2d 4 ปีที่แล้ว

    Thankyou, very helpful.

  • @JoaoNelsonLima
    @JoaoNelsonLima 3 ปีที่แล้ว

    Great job guy

  • @yohannsalliege1974
    @yohannsalliege1974 2 ปีที่แล้ว +1

    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

  • @jayasharma4208
    @jayasharma4208 3 ปีที่แล้ว

    Thank you sir, your videos are very helpful!, Can I implement same procedure for getting microsecond delay in nucleo STM8S?

    • @ControllersTech
      @ControllersTech  3 ปีที่แล้ว +2

      Yeah the same logic can be applied for any microcontroller

  • @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

  • @user-tu2zc9em5s
    @user-tu2zc9em5s 2 ปีที่แล้ว

    thanks to help.

  • @ganeshgaikwad2158
    @ganeshgaikwad2158 4 ปีที่แล้ว

    Hello sir.. please guide me for conncting compact flash card to stm32f373 ....

  • @sangtruong8833
    @sangtruong8833 ปีที่แล้ว

    at 6.33, the screen shows pulse width about 0.1 nano second, is right?

  • @ramalingeswararaobhavaraju5813
    @ramalingeswararaobhavaraju5813 3 ปีที่แล้ว

    Thank.

  • @devjeetmandal1472
    @devjeetmandal1472 4 ปีที่แล้ว

    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  4 ปีที่แล้ว +1

      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 4 ปีที่แล้ว

      @@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.

  • @maso553
    @maso553 ปีที่แล้ว

    Hi Controllers tech, what is the name of the device being used an an Oscilloscope in the video?

  • @sujoybhattacharya9938
    @sujoybhattacharya9938 3 ปีที่แล้ว

    This is not able to give me 10ns delay in stm32h743zi2?

  • @TheOldProgramming
    @TheOldProgramming 4 ปีที่แล้ว +1

    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

  • @andrey7530
    @andrey7530 4 ปีที่แล้ว

    Ths man!!!

  • @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

  • @jayasharma4208
    @jayasharma4208 3 ปีที่แล้ว +1

    I am using stm32f0 board, and maximum hclk frequency is 48MHZ , how to generate delay of microsecond????

    • @ControllersTech
      @ControllersTech  3 ปีที่แล้ว +1

      Watch the video properly. It depends on APB clock, where the timer is connected.

  • @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 3 ปีที่แล้ว

      Yes. But It requires a compliment timer.

    • @design4682
      @design4682 3 ปีที่แล้ว

      @@emreiris114 can you share the code regarding this?

  • @willleemaipipi
    @willleemaipipi 4 ปีที่แล้ว

    Why d not just use Hal_delay() function? any difference betrween?

    • @ControllersTech
      @ControllersTech  4 ปีที่แล้ว +3

      Hal delay provides minimum delay of 1 millisecond. This video shows how to create delays in microsecond

  • @nielshib635
    @nielshib635 ปีที่แล้ว

    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)

  • @paulg.3067
    @paulg.3067 4 ปีที่แล้ว

    Is this your own voice? If not, what speech synthesis are you using?

  • @mohamedazharudeen6477
    @mohamedazharudeen6477 3 ปีที่แล้ว

    what is the settings for creating milli seconds delay using this fucntion

    • @ControllersTech
      @ControllersTech  3 ปีที่แล้ว

      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

  • @The2Coolest2
    @The2Coolest2 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

      Well you should change everything based on your controller..

    • @The2Coolest2
      @The2Coolest2 2 ปีที่แล้ว

      @@ControllersTech I meant I did not change anything over the months I used it.

    • @ControllersTech
      @ControllersTech  2 ปีที่แล้ว

      There must have been some changes.. you need to recheck every detail i guess..

  • @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

  • @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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      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 !!!!

  • @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

  • @albertziatdinov1373
    @albertziatdinov1373 4 ปีที่แล้ว +1

    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..

  • @devjeetmandal1472
    @devjeetmandal1472 4 ปีที่แล้ว

    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  4 ปีที่แล้ว

      What's the timer frequency ?

    • @devjeetmandal1472
      @devjeetmandal1472 4 ปีที่แล้ว

      APB2 TIMER CLOCKS :180MHz

    • @ControllersTech
      @ControllersTech  4 ปีที่แล้ว +1

      First bring ur timer frequency to 100MHz, than set the prescalar to 0 and ARR to 0xffff-1

    • @devjeetmandal1472
      @devjeetmandal1472 4 ปีที่แล้ว

      @@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  4 ปีที่แล้ว

      I need to see the code. Telegram me @controllerstech

  • @polatozcanlar2600
    @polatozcanlar2600 4 ปีที่แล้ว +1

    100 000 000/(1*65536)=1525.87 Hz How does it works on 100 mhz? Am I wrong?

    • @ControllersTech
      @ControllersTech  4 ปีที่แล้ว +1

      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 9 หลายเดือนก่อน

      where is the prescalar as 100, i see only prescalar as 1-1 i 2:23@@ControllersTech

    • @ControllersTech
      @ControllersTech  9 หลายเดือนก่อน +1

      @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.

  • @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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      U can contact on discord or telegram

    • @exaltedcodiing6655
      @exaltedcodiing6655 3 ปีที่แล้ว

      @@ControllersTech i am there,

  • @Bianchi77
    @Bianchi77 3 ปีที่แล้ว

    How can I get 20 seconds timer3 interrupt ? cheers

    • @ControllersTech
      @ControllersTech  3 ปีที่แล้ว

      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

    • @Bianchi77
      @Bianchi77 3 ปีที่แล้ว

      @@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;

    • @Bianchi77
      @Bianchi77 3 ปีที่แล้ว

      Make sure you enable the update event interrupt
      What should I update?

    • @ControllersTech
      @ControllersTech  3 ปีที่แล้ว

      But this will give you the delay of 8 sec max..

    • @Bianchi77
      @Bianchi77 3 ปีที่แล้ว

      @@ControllersTech How can I make it more?
      TIM_TimeBaseStructure.TIM_ClockDivision = 1;?

  • @innomus9109
    @innomus9109 3 ปีที่แล้ว

    Is this code blocking mode?

    • @ControllersTech
      @ControllersTech  3 ปีที่แล้ว +1

      Yes

    • @innomus9109
      @innomus9109 3 ปีที่แล้ว

      @@ControllersTech Hi thanks for the reply. Any suggestions how I can have the same delays but in none blocking mode?

    • @ControllersTech
      @ControllersTech  3 ปีที่แล้ว

      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

  • @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