This video serves as a comprehensive introduction to using timers on the STM32 microcontroller with the CubeIDE and HAL library. It begins by explaining the fundamental concepts of timers as clock-driven counters, the role of prescalers in adjusting frequency, and the overall timer architecture, including the different APB prescalers and the categorization of timers into Basic, General Purpose, and Advanced types. The tutorial then demonstrates four practical examples: how to measure code execution time, how to implement non-blocking delays to avoid halting the processor, how to use timer interrupts for efficient, periodic tasks, and finally, how to generate and dynamically control Pulse Width Modulation (PWM) signals for applications like motor control or dimming LEDs.
A Guide to STM32 Timers: From Basics to PWM
0:00:10 - What is a Timer?: A timer is a hardware counter that increments with each clock pulse. To extend the maximum time that can be measured, a prescaler is used to divide the clock frequency, slowing down the counter's increment rate.
0:01:15 - STM32 Timer Architecture: The STM32G4 has 12 timers fed by two different APB prescalers (APB1 and APB2). These timers are grouped into three categories: Basic (for simple timing and interrupts), General Purpose, and Advanced Control (which support advanced features like PWM output on GPIO pins).
0:02:31 - Key Registers and Calculation: The timer's behavior is controlled by the Auto-Reload Register (ARR), which sets the counting period, and the Prescaler (PSC). A critical detail is that the hardware automatically adds one to these values, so you must enter your desired value minus one in the configuration software.
0:03:31 - Example 1: Measuring Execution Time: The video demonstrates how to use a basic timer (Timer 6) to measure how long a piece of code takes to run. By capturing the counter value before and after a 50ms delay function, it's shown that the function actually takes nearly 51ms to execute.
10:04 - Example 2: Non-Blocking Delays: This example shows how to create delays without using the HAL_Delay() function, which halts the CPU. By continuously checking the timer's value in a loop, an LED is toggled every 1.5 seconds without blocking other code from running.
14:40 - Example 3: Using Timer Interrupts: A more efficient method for periodic tasks is demonstrated using a "period elapsed interrupt." A timer is configured to trigger an interrupt callback function every second. The code to toggle the LED is placed inside this callback, freeing the main loop entirely.
17:52 - Example 4: Generating PWM Signals: The tutorial explains how to generate a Pulse Width Modulation (PWM) signal using a general-purpose timer (Timer 3). The timer's ARR sets the PWM frequency (1 kHz), and the Capture/Compare Register (CCR) sets the duty cycle.
22:52 - Dynamic PWM Control: To showcase the flexibility of PWM, the code is updated to read the state of the onboard push-button. When the button is pressed, the PWM duty cycle is changed from 30% to 50% in real-time, with the output verified on an oscilloscope.