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

preview_player
Показать описание
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.

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.

Product Links:

Related Videos:
Getting Started with STM32 and Nucleo Part 1: Introduction to STM32CubeIDE and Blinky

Getting Started With STM32 and Nucleo Part 2: How to Use I2C to Read Temperature Sensor TMP102

Getting Started With STM32 & Nucleo Part 4: Working with ADC and DMA

Getting Started With STM32 and Nucleo Part 5: How to Use SPI

Programming the Adafruit Feather STM32F405 Express with STM32CubeIDE

TinyML: Getting Started with STM32 X-CUBE-AI

Getting Started With STM32 and Nucleo Part 3: FreeRTOS - How To Run Multiple Threads w/ CMSIS-RTOS

Related Project Links:

Related Articles:
Рекомендации по теме
Комментарии
Автор

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!

kenwallace
Автор

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

Embedonix
Автор

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

saravanaperumalannamalai
Автор

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)

akism
Автор

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

thenoisyelectron
Автор

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

irod
Автор

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)

sixsunss
Автор

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

bobesfanchi
Автор

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

yellowcrescent
Автор

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.

hamedelahi
Автор

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.

kiddjmadd
Автор

Very useful series. Thanks for these videos.

zetaconvex
Автор

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

timothynguyen
Автор

These videos are super helpful. Thanks!

sdp
Автор

This was an excellent video. Thanks a lot man!

mkashty
Автор

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

jusroc
Автор

Hi Shawn Hymel I am working on a simple implementation that checks each character being received using HAL_UART_Recieve_IT and when \n or \r 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.

noopursirmokadam
Автор

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

winstonsabellona
Автор

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

kenwallace
Автор

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?

bennguyen