STM32 Timer Operations: PWM Generation and Input Capture

Pulse Width Modulation (PWM) Overview

PWM is a technique used to simulate analog signal levels by modulating the width of digital pulses. In control applications, such as motor speed regulation, the duty cycle dictates the average power delivered to the load. By rapidly swithcing the output state, the system effectively manages the average voltage output.

  • Frequency (Freq): Defined as 1/Tcycle.
  • Duty Cycle: The ratio of high-level duration to the total cycle time (Ton / Tcycle).
  • Resolution: The smallest achievable step of duty cycle change.

The mathematical relationships are given by:

  • Freq = CK_PSC / (PSC + 1) / (ARR + 1)
  • Duty = CCR / (ARR + 1)

PWM Configuration Example

The following routine configures TIM2 for PWM output on PA0:


void Setup_PWM_Output(void) {
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_TimeBaseStructure.TIM_Period = 99;      // ARR
    TIM_TimeBaseStructure.TIM_Prescaler = 719;  // PSC
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_OCInitTypeDef TIM_OCStructure;
    TIM_OCStructInit(&TIM_OCStructure);
    TIM_OCStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCStructure.TIM_Pulse = 0; // Initial Duty Cycle
    TIM_OC1Init(TIM2, &TIM_OCStructure);

    TIM_Cmd(TIM2, ENABLE);
}

Input Capture (IC)

Enput Capture is used to measure external signal characteristics such as frequency, pulse width, or period. When a specified edge is detected on an input channel, the current Counter (CNT) value is captured into the Capture/Compare Register (CCR).

Input Capture Configuration Example

The following setup configures TIM3 to capture signals on PA6:


void Setup_Input_Capture(void) {
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    TIM_TimeBaseInitTypeDef TIM_Base;
    TIM_Base.TIM_Period = 0xFFFF;
    TIM_Base.TIM_Prescaler = 71;
    TIM_TimeBaseInit(TIM3, &TIM_Base);

    TIM_ICInitTypeDef TIM_IC_Structure;
    TIM_IC_Structure.TIM_Channel = TIM_Channel_1;
    TIM_IC_Structure.TIM_ICPolarity = TIM_ICPolarity_Rising;
    TIM_IC_Structure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_IC_Structure.TIM_ICFilter = 0x0F;
    TIM_ICInit(TIM3, &TIM_IC_Structure);

    TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);
    TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
    TIM_Cmd(TIM3, ENABLE);
}

Timer Slave Modes

STM32 timers support various slave modes to synchronize with external signals:

  • Reset Mode: Triggering the input clears the counter and prescaler, useful for precise period measurement.
  • Gated Mode: The counter runs only when the trigger signal is in a specific state (e.g., high or low).
  • Trigger Mode: Enables the timer start based on an external trigger event.
  • External Clock Mode 2: Allows an externla pin (like ETR) to act as the primary clock source for the timer.

Tags: STM32 Timer PWM InputCapture EmbeddedSystems

Posted on Sat, 16 May 2026 22:57:28 +0000 by Pezmc