🕒

Exploring STM32 Timers and Configurations

Sep 23, 2024

Lecture Notes: STM32 Timers and Microcontrollers

Introduction

  • Importance of timers in modern microcontrollers:
    • Counting clock pulses
    • Creating delays
    • Generating periodic interrupts
    • Measuring time between events
    • Setting up pulse width modulation signals

Understanding Timers in STM32

  • Main CPU Clock (HClock):

    • Connects to prescalers to divide the clock
    • Example with Nucleo board at 80 MHz:
      • Prescaler set to divide by 80 results in 1 MHz clock signal
  • Timer Operations:

    • Timers count from zero to a set maximum value
    • Increment once per microsecond with 1 MHz clock
    • STM32 microcontrollers often have multiple timers
    • Example microcontroller: STM32L476RG

STM32L476RG Timers and Configuration

  • Datasheet Overview:

    • General-purpose timers (e.g., Timers 16 and 17)
    • Special-purpose timers like watchdog timers
    • HAL API uses SysTick for delays (important for real-time systems)
  • Clock Tree and Timer Connections:

    • Timer 16 connected to clock multiplier, APB2 prescaler
    • Prescalers affect how timers operate (e.g., APB2 set to 8, timer prescaler to 4)

CubeMX and IDE Setup

  • Project Setup:

    • Naming and configuration in STM32 Cube IDE
    • Clock configuration and prescaler settings
    • Default settings: 80 MHz CPU clock, prescaler adjustments
  • Timer Activation:

    • Using CubeMX to activate timer 16
    • Timer functionalities (Input Capture, Output Compare, PWM)
    • Prescaler setting: 80 (enter 79 due to zero-indexing)
    • Auto Reload Register set for 16-bit max value (65535)

Timer Example Applications

  • Simple Timer Application:

    • Using HAL APIs to read CNT register and measure elapsed time
    • Serial terminal output for debugging
    • Limitations: Maximum measurement duration of ~65 milliseconds
  • Blinky Example with Timer:

    • Adjust prescaler for 1 tick every 100 microseconds
    • LED toggling without blocking CPU cycles
    • Non-blocking code execution

Advanced Use: Timer Interrupts

  • Interrupts for Timer Events:

    • Timer value increments and triggers interrupts on reset
    • Timer Period Elapsed Callback for handling interrupts
  • CubeMX Configuration for Interrupts:

    • Set timer period and enable NVIC settings
    • Use HAL functions for starting timers in interrupt mode
    • Implement callback function for LED toggling

Conclusion

  • Recap of using timers in STM32 microcontrollers
  • Encouragement to explore further and use provided resources
  • Link to demo code and encouragement to subscribe for more content

Note: The lecture included practical examples and demonstrations using the STM32 Cube IDE and HAL APIs to configure and utilize timers effectively in an embedded system.