Issues generating correct PWM duty cycle using matlab STM32 Coder on NUCLEO-G474RE (TIM1 Advanced Timer)

Hi everyone,
I’m running into a confusing issue while using MATLAB STM32 Coder with a NUCLEO-G474RE and wanted to check if anyone here has faced something similar.
What I’m trying to do:
  • Generate a fixed-frequency PWM with variable duty cycle
  • Using Advanced Timer TIM1
  • Duty cycle driven from Simulink (percentage input)
  • Target hardware: STM32G474RE (NUCLEO board)
  • MATLAB R2025a, ARM GNU Toolchain
Setup details:
  • Using the PWM Output block from STM32 Support Package
  • Timer module: TIM1
  • Duty cycle unit: Percentage
  • Only Channel 1 enabled
  • Timer group set to Advanced Timers 1/8/20
  • Timer configured to start during model initialization
  • No interrupts enabled
  • Pins configured correctly in CubeMX and verified to be in AF (Alternate Function) mode
  • Same pin/timer configuration works as expected when using CubeMX + HAL code directly
The problem:
Although the PWM signal is present on the expected pin, the duty cycle does not match the input value from Simulink.
  • Example: 50% input does not give a clean 50% duty cycle, it instead defaults to the pulse value given in the cubemx file.
  • Changing the input does not scale the duty as expected
  • Behavior feels like either:
  • CCR registers aren’t updating correctly, or
  • MOE / output enable / complementary settings are interfering, or
  • MATLAB’s abstraction of TIM1 differs from CubeMX behavior
Things I’ve already checked:
  • Correct pin mapping for TIM1_CH1
  • AF mode confirmed
  • No dead-time or break inputs enabled
  • No complementary outputs enabled
  • Clock configuration looks fine
  • Timer counter is running
I’ve attached screenshots of my PWM block configuration and Hardware Implementation Timers (TIM1) settings for reference.
Any insights, would be really appreciated
Thanks!
Tim-1 Config

 Accepted Answer

  • Make sure to set the sample time of constant block.
  • Below are the settings in cubeMX file to generate signal with the period of 1ms
  • I was able to generate the expected signal. In the below external mode simulation, I changed duty cycle from 50% to 80%. It worked fine.

1 Comment

Thank you this did work. Additionally this was fixed by creating a new file, another way was configuring a general purpose timer and then confgiruing the advance timer!!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!