Issues generating correct PWM duty cycle using matlab STM32 Coder on NUCLEO-G474RE (TIM1 Advanced Timer)
Show older comments
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!


Accepted Answer
More Answers (0)
Categories
Find more on C2000 Microcontroller Blockset in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


