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

Поделиться
HTML-код
  • Опубликовано: 12 сен 2024

Комментарии • 116

  • @박동진-y9q
    @박동진-y9q 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.

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

  • @JoaoNelsonLima
    @JoaoNelsonLima 3 года назад

    Great job guy

  • @paulg.3067
    @paulg.3067 4 года назад

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

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

  • @jayasharma4208
    @jayasharma4208 4 года назад +1

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

    • @ControllersTech
      @ControllersTech  4 года назад +1

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

  • @maso553
    @maso553 Год назад

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

  • @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 года назад +2

      Yeah the same logic can be applied for any microcontroller

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

  • @해헤-u9e
    @해헤-u9e 2 года назад

    thanks to help.

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

  • @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 года назад +2

      65535 us (16 bit counter, so max = 0xffff)

  • @ganeshgaikwad2158
    @ganeshgaikwad2158 5 лет назад

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

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

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

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

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

  • @sujoybhattacharya9938
    @sujoybhattacharya9938 4 года назад

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

  • @ramalingeswararaobhavaraju5813
    @ramalingeswararaobhavaraju5813 4 года назад

    Thank.

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

  • @yunusemreyagan4946
    @yunusemreyagan4946 4 года назад

    Hello controllers Tech.
    Can i add dead time to PWM signals generated in Timer 3 channels on stm32f407 discovery board?

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

  • @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 Год назад

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

    • @ControllersTech
      @ControllersTech  Год назад +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.

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

  • @andrey7530
    @andrey7530 4 года назад

    Ths man!!!

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

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

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

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

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

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

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