Getting Started with STM32 and Nucleo Part 6: Timers and Timer Interrupts | Digi-Key Electronics

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ส.ค. 2020
  • In this tutorial, Shawn shows you how to set up timers in STM32 and use those timers to measure execution time, create non-blocking code, and trigger interrupts.
    If you are not familiar with STM32CubeIDE, we recommend watching this video first: • Getting Started with S...
    We first cover the basics of timers in STM32. A timer is a piece of hardware in the microcontroller that continually increments a stored value each clock pulse it receives. Most general purpose timers in microcontrollers are connected to the main CPU clock through one or more prescalers. A prescaler acts to divide the cock. For example, an 80 MHz clock that goes through a prescaler of 80 would cause a timer to increment (tick) at a rate of 1 MHz.
    These timers can be used to trigger interrupts to run arbitrary code at given intervals, measure the time between events, or automatically control hardware, as in the case of output compare and pulse width modulation (PWM).
    We show you how to configure a timer using STM32CubeIDE by setting a prescaler and a maximum counter value. When a timer reaches this maximum value, it will reset back to 0 (for our chosen general purpose timer).
    From there, we create a demo using the STM32 HAL framework to first read timer values to measure execution time of another piece of code. Then, we show how to use timer values to create a non-blocking blink example. Finally, we set up a timer interrupt that triggers once per second. In the interrupt handler, we simply toggle the LED.
    Please note that we are using the Nucleo-L476RG for this video, which can be found here: www.digikey.com/product-detai...
    Product Links:
    Nucleo-L476RG - www.digikey.com/product-detai...
    Related Videos:
    Getting Started with STM32 and Nucleo Part 1: Introduction to STM32CubeIDE and Blinky
    • Getting Started with S...
    Getting Started With STM32 and Nucleo Part 2: How to Use I2C to Read Temperature Sensor TMP102
    • Getting Started With S...
    Getting Started With STM32 & Nucleo Part 4: Working with ADC and DMA
    • Getting Started With S...
    Getting Started With STM32 and Nucleo Part 5: How to Use SPI
    • Getting Started with S...
    Programming the Adafruit Feather STM32F405 Express with STM32CubeIDE
    • Programming the Adafru...
    TinyML: Getting Started with STM32 X-CUBE-AI
    • TinyML: Getting Starte...
    Getting Started With STM32 and Nucleo Part 3: FreeRTOS - How To Run Multiple Threads w/ CMSIS-RTOS
    • Getting Started With S...
    Related Project Links:
    Getting Started with STM32 - Timers and Timer Interrupts - www.digikey.com/en/maker/proj...
    Related Articles:
    Getting Started with STM32 - How to Use SPI - www.digikey.com/en/maker/proj...
    Making a Temperature Logger with the Adafruit Feather STM32F405 Express - www.digikey.com/en/maker/proj... -
    TinyML: Getting Started with STM32 X-CUBE-AI - www.digikey.com/en/maker/proj...
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @iro3d
    @iro3d 3 ปีที่แล้ว +12

    A very useful video. Though it would have been easier to watch if there were at least a 1000 milliseconds pause between sentences.

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

    Shawn, you structured it very well. 1hr Lecture shortened under 15 mins. Worth every sec spent on this video series.

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

      It's info dense, but easy to backtrack if you miss something. I'm enjoying this tutorial, while arranging the hardware.

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

    This engineer is the best in the business.... clear and concise and to the point! can not get any better!

  • @ak17ism
    @ak17ism 3 ปีที่แล้ว +25

    Thanks Shawn! These videos are awesome, hope to see more STM32 tutorials regardless of the subject! it may be a wormhole, but it'd be interesting to see a generic getting started with TFT display (TFT, SDRAM setup and where to go on the data sheet to find these)

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

    Absolutely loving these STM32 tutorials! Thank you for putting time into these!

  • @kenwallace6493
    @kenwallace6493 3 ปีที่แล้ว +6

    Shawn may actually make HAL useful to us bare-metal skeptics. He goes a mile-a-minute but one can always stop and replay. Great stuff!

  • @sixsunss8032
    @sixsunss8032 3 ปีที่แล้ว +7

    Your tutorial is so clear for a beginner! Thanks so much!!! Please keep it in this style and hope to see more videos from you!!! (only one suggestion, the opening music is quite loud compared to the rest of the video)

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

    This was an excellent video. I was looking for the definition of callback function and I came across this video and learned a lot.

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

    Thank you so much for these videos. I saw the whole 6 videos. They are really excellent. Can you continue these videos? I will see all of them. And I also like to know about TFT LCDs and more other examples.

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

    Nice! Also, if you don't need any of the timer functionality (like triggering ISRs/callbacks or toggling pins), an easy way to measure times to the nearest millisecond is to use HAL_GetTick(), although you may have to handle overflow cases for very long-running systems (overflow happens after ~49 days and it wraps back around to zero). This function is already setup in HAL_Init(), so no additional setup is necessary.
    uint32_t start_time = HAL_GetTick();
    // do something
    uint32_t total_time = HAL_GetTick() - start_time; // total time elapsed in milliseconds

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

    Will wait for new series. Thank you!

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

    Love the use of "sprintf!" Had my encoder (also a subset of timers) sending data, but I'd hit a wall trying to get a comprehensible value to the serial interface. Matching my code to the video confirmed encoder was working as expected without needing to mess with the raw hex that I'd been trying as a first pass.

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

    These videos are gold. Thanks!

  • @timothynguyen4305
    @timothynguyen4305 3 ปีที่แล้ว +5

    Thanks Shawn for these videos! From a second year EE student :)

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

    Very useful series. Thanks for these videos.

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

    Great course, Shawn, many thanks!

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

    These videos are super helpful. Thanks!

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

    This was an excellent video. Thanks a lot man!

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

    Amazing tutorial! Thank you very much

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

    thanks. i hope there will be more of this series about stm32 and nucleo dev board

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

    Great Video, thank you so much.

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

    Thanks for video - great series btw; in the interrupt case, I would "if (htim->Instance == TIM16)" which is an included definition in the HAL - this would make things more compatible should the viewers ever want to move that callback out of the main.c (without having to extern htim16 for example)

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

      Thanks for this, just what I was looking for...

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

    This was really helpful!

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

    Just a quick question on the subject. I am fairly new to Embedded electronics.
    I am planning to design a midi control surface that incorporates lots of rotary encoders on (as many as possible). I was wondering how I could ascertain how many GPIOs that have "attached" interrupts on them. Would be grateful for any advice here. Otherwise keep up the good work! thanks

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

    Great video. Maybe in the future you could also do one on RTC and sleep/stop/suspend. Thx again.

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

    what a great tutorial!

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

    Hi Shawn Hymel I am working on a simple implementation that checks each character being received using HAL_UART_Recieve_IT and when
    or
    is recieved it gets copied to another buffer. I have enabled USART as a global interrupt and wish to know how and where to use the Receive call back function. I am stuck and confused about this from a long time.

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

    Keep up the good work!

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

    Nice video, keep it up, thank you :)

  • @anuragmahajan5919
    @anuragmahajan5919 หลายเดือนก่อน

    Thanks for the video!

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

    I love these videos

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

    Please add these 2 parts in your STM32 playlist!

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

    Maybe Shawn knows but for everyone else, there's a built terminal in the STM program. Near the bottom right, on that 'console' tab if you lick the icon next to the pc monitor.

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

      I've played with it, and it seems to give me a console out of the IDE or to my PC. I couldn't ever seem to get it to give me a serial connection to my board, so I stuck with using an external program for that. I'd definitely love to know if there's a way to open a serial connection! Maybe I just missed something.

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

      He's right. It works just as good as Putty.
      imgur.com/a/mfOAMig
      The "Arduino" named board is a Nucleo-G474RE.

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

      @@georgeborsa5346 Good to know, thanks!

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

    perfect! Thanks a lot

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

    Great video, and I'm learning a lot, but haven't forgot to show how to enable UART2 in configuration wizard, and what mode to choose?

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

      Do you know to enable the UART2 in configuration

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

    It's interesting that Timer's 'auto-reload preload' can be set to Disable, and you don't need to manually reload the counter in code!
    Regarding using a Timer for polling elapsed time, how does it differ from using HAL_GetTick(), i.e. no Timer?
    If using the Timer to trigger an ADC reading.. it seems the number of samples captured for an input sine-wave (single cycle), is dependent on the number of channels in the Scan. In other words, if NbrOfConversion=1, then the ADC will capture samples equal to the Timer rate.. but if NbrOfConversion>1, then only one channel gets converted per trigger event, the next channel in the scan list is captured only on the next trigger event?
    Speaking of Output-Compare, what does the "Pulse" field do? It seems it's necessary to be set to 1? For example, if Output-Compare is set to to Toggle on Match, with Trigger TRGO Event = Update , and if the Timer Clock is 120Mhz (Prescaler = 60-1, Preload 62-1), then the Ouput-Compare GPIO should toggle at 32khz.. so assuming one channel in the Scan list, how often will the ADC sample the channel?

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

    This may be a dumb question but why don't we just use the timer rollover interrupt handler to write our LED toggle code? It would normally appear above main. What's the purpose of callback functions??

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

    So do we take value of HCLK or APB1/APB2 Timer clock to prescale upto 1 MHz? Asking because you said if we change the mulplier and prescaler values of HCLK then we will have to consider different values.

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

    Amazing tutorail

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

    Great stuff

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

    It would be nice to know how to register callbacks for individual timers

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

    first of all thank you for your videos, they are very helpful :)
    In this video at 9:44 u are saying that it also works if the timer rolls over zero. But if I run this code exactly the way u do it, this does not work... So I had to set the counter to zero after each second to avoid this. Did I make a mistake or how else should it work without this reset?

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

      Same thing happening to me. How can you reset the timer?

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

      @@Schmalik i used __HAL_TIM_SetCounter(&htim16, 0); to reset the timer to zero

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

      @@flexi1232 i was searching to see if anyone mentioned this! thanks

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

    awesome video! can you please make a video on using stm32 as a usb HID device

  • @m-electronics5977
    @m-electronics5977 หลายเดือนก่อน

    Are existing other tutorials to make this only in CMSIS? Because learn the HAL with complete other names isn't productive when you had start with the CMSIS.

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

    Thank you very much

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

    For the first example i am getting 5.6mS instead of 50mS , I am using a STM32F303K8T6 with 72Mhz clock and prescaler of 72-1 all others settings the same. ?!?

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

    Why you left the variable "uart_buf_len" type int not uint16_t?

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

    Thank You!!! Please make a video on HAL Systick Timer.

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

    Thanks!!!

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

    Why if i set Period=0, timer doesn't run? I would like to generate an update event at every tick, which would require a Period=1-1, that would make sense... I know I can change the prescalar value to obtain the same update frequency but this "-1" is quite stupid...

  • @samsman007
    @samsman007 10 หลายเดือนก่อน +1

    Why Timer16 in the first place? Or could it just be any other timer?

  • @Frisky0563
    @Frisky0563 11 หลายเดือนก่อน

    Hi there is there a calculator for STM32 to help me figure out how to setup clocks, prescaler, multipliers output capture for 833.33 microseconds. I’m using the 476RG 106RG nucleo boards thank you.

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

    I hope It will be continue

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

    I wasn’t aware of all the clock functions that were imbedded in this astm32. This will take awhile to grasp, and to find new applications. Just thinking about it hurts. 😔

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

      It's a lot! That's why I had to only focus on a few functions at once with the video. I still don't know how to use all of the features (or even what some of them mean!). One step at a time..learning how to measure time (such as milliseconds) is a good start :)

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

      Shawn Hymel - it May take a team!

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

    what are callbacks and why are they called so?

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

      Callbacks are functions that you define in your code that are called from somewhere else (usually in a library or back-end). Often, a pointer to the function can be passed as an argument to that back-end code so that you can name the callback function whatever you want. It allows you to define a function and let something else call it, even if you don't know when that will be (they form the basis for event-driven programming). Here is a good example of how a callback might work in C: stackoverflow.com/questions/142789/what-is-a-callback-in-c-and-how-are-they-implemented

  • @clivedsouza6213
    @clivedsouza6213 6 หลายเดือนก่อน

    This dude rips.

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

    Shawn, I think you forgot about digital inputs.

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

    Where is video about PWM???

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

    At 10:00 this shouldn't work after a while right?
    If timer_val is 65,000,
    then __HAL_TIM_GET_COUNTER(&htim16) runs, this will give a value like 400, since it rolled over
    400-65,000 >= 10,000 is false
    Unless maybe you do some casting, but then it will almost always be true
    You have to reset the timer, or have some extra logic

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

      indeed, you have to account for roll over manually, or reset counter to zero using __HAL_TIM_SET_COUNTER(&htim, 0);

  • @KR-pj6ol
    @KR-pj6ol 2 ปีที่แล้ว

    more videos please ........................................!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!........................................!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • @JinFeeiLoh
    @JinFeeiLoh 2 หลายเดือนก่อน

    9:48 if (__HAL_TIM_GET_COUNTER(&htim16) - timer_val >= 10000)... isn't correct (missing cast). You need to do something like this:
    uint16_t tim16 = __HAL_TIM_GET_COUNTER(&htim16);
    if ((uint16_t)(tim16 - timer_val) >= 10000){
    // ... do something
    timer_val = tim16;
    }

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

    Hi Digikey....can I have a sample of nucleo board from you 😊😊.

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

    .

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

    to much bla bla no examples for beginners as simple

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

    you explaining really fast .relax mate .why you explain difficult