[STM32](https://www.ampheo.com/search/STM32) [microcontrollers](https://www.ampheo.com/c/microcontrollers) offer multiple types of timers, each optimized for specific applications such as timekeeping, PWM, input capture, output compare, and encoder interfacing. Here's a comprehensive overview of the STM32 Timer types and their typical use cases: ![sddefault (3)](https://hackmd.io/_uploads/Sy5YbUVzgx.jpg) **1. General-Purpose Timers (TIM2–TIM5, TIM9–TIM14)** **Features:** * 16-bit or 32-bit counters * Up/down counting * PWM generation * Input capture / output compare * Interrupt and DMA support **Applications:** * Generating periodic interrupts (e.g., system tick) * PWM for motor control or LED dimming * Measuring input pulse width (e.g., ultrasonic distance sensors) * Debouncing buttons with timer delay **2. Advanced-Control Timers (TIM1, TIM8)** **Features:** * All general-purpose features * Dead-time insertion * Complementary outputs (CH1/CH1N, etc.) * Break and fault inputs * Designed for motor control (e.g., 3-phase) **Applications:** * Motor control (BLDC, PMSM) * High-resolution PWM for power electronics * Inverter control * Safety-critical applications (with break/lock features) **3. Basic Timers (TIM6, TIM7)** **Features:** * Simple 16-bit timers with no input/output channels * Mainly used for time base generation * Can trigger DAC or ADC conversions **Applications:** * Time delays or periodic interrupts * Triggering DACs (e.g., waveform generation) * Scheduling RTOS tasks * Software timers for non-critical tasks **4. System Timer (SysTick)** **Features:** * 24-bit timer built into the Cortex-M core * Typically used for OS tick or simple timekeeping * High-priority interrupt **Applications:** * RTOS tick timer (e.g., FreeRTOS) * Simple millisecond or microsecond counters * Delay functions (HAL_Delay()) **5. Low-Power Timers (LPTIM1, LPTIM2)** **Features:** * Designed for operation in low-power modes (e.g., STOP/SLEEP) * 16-bit with wake-up capability * Can be clocked by LSE, LSI **Applications:** * RTC-style low-power timekeeping * Wake-up timers in sleep mode * Capacitive touch sensing (via external pulses) * Energy-efficient PWM **6. Real-Time Clock (RTC)** **Features:** * Counts real time (seconds, minutes, etc.) * Powered by LSE or LSI (low-frequency clock) * Calendar, alarms, timestamping * Runs in backup domain **Applications:** * Calendar and timekeeping * Time-stamped data logging * Wakeup events from standby or shutdown * Battery-powered devices **Typical STM32 Timer Use Case Matrix** ![企业微信截图_20250528165858](https://hackmd.io/_uploads/By4vgUEMlg.png) **Bonus: Using TIM for Quadrature Encoders** General-purpose and advanced timers can be configured for encoder mode: ``` c htim2.Instance = TIM2; htim2.Init.EncoderMode = TIM_ENCODERMODE_TI12; ``` Used in: * Rotary encoders for angle/speed detection * Robotics, CNC, motor feedback