DEV Community

Hedy
Hedy

Posted on

How to enable the PWM of TIM16 and 17 of STM32F030C8T6 to start simultaneously?

To start TIM16 and TIM17 PWM outputs simultaneously on the STM32F030C8T6, you can use the Master-Slave Timer synchronization mechanism. However, TIM16 and TIM17 are independent advanced-control timers, and on STM32F030, they do not support direct hardware master/slave synchronization between each other like TIM1 and TIM3 do on higher-end chips.

Image description

But you can still synchronize their start using either of these methods:

Option 1: Start Both Timers in Software (Software Sync)
This is the simplest approach for STM32F0 series without inter-timer triggers.

Steps:

  1. Configure both TIM16 and TIM17 for PWM mode.
  2. Set CR1.CEN = 0 initially (timers disabled).
  3. After full configuration, start both timers in quick succession in code.

Code Example (HAL):

c

HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
Enter fullscreen mode Exit fullscreen mode

Or more precise (with register-level control):

c

// Configure everything first, then:
TIM16->CR1 |= TIM_CR1_CEN;
TIM17->CR1 |= TIM_CR1_CEN;
Enter fullscreen mode Exit fullscreen mode

This works well if a few CPU cycles of delay between start is acceptable.

Option 2: Use a Shared Trigger (Advanced Sync – Optional for STM32F0)
On STM32F030, TIM16 and TIM17 do not support slave mode (SMS) or external triggers directly. However:

  • You can use an external event (like EXTI or a GPIO edge) to start both timers if you're okay with using external hardware or code-generated triggers.
  • Or, in some use cases, you could generate a shared software event (like writing to UG - Update Generation) to preload and sync both counters.

Preloading + UG Example:

c

TIM16->EGR |= TIM_EGR_UG; // Generate update event (preload registers)
TIM17->EGR |= TIM_EGR_UG;

TIM16->CR1 |= TIM_CR1_CEN;
TIM17->CR1 |= TIM_CR1_CEN;
Enter fullscreen mode Exit fullscreen mode

This ensures both timers start counting from their initial preload values.

Option 3: Use DMA or SysTick Trigger (More Complex)
For very tight sync, you could:

  • Use a DMA request or SysTick interrupt to enable both timers at the same time.
  • But this is overkill unless you're doing precision waveform generation.

Notes and Tips

Image description

Recommendation
For STM32F030C8T6, the best practical method is:

c

// Set both timers' configurations, preload values, PWM modes
TIM16->EGR |= TIM_EGR_UG;
TIM17->EGR |= TIM_EGR_UG;

// Then start both timers
TIM16->CR1 |= TIM_CR1_CEN;
TIM17->CR1 |= TIM_CR1_CEN;
Enter fullscreen mode Exit fullscreen mode

This achieves near-simultaneous PWM start, good enough for most embedded applications.

Top comments (0)