100 kHz PWM for STM32L476 in Simulink Coder
9 views (last 30 days)
Show older comments
Hi
I would like to use the PWM block from "Simulink Coder Support for STM Nucleo Boards" to control a buck converter MOSFET. The desired frequency is 100 kHz, which can be set in the block properties and is also delivered by the Nucleo board. However, the resolution of the duty cycle is very low (just 5 steps from 0%-100%). When setting the frequncy of the PWM to 10 kHz, the resolution is fine with around 100 steps.
Is there a work-around for this problem?
Thanks a lot!
1 Comment
Answers (1)
Oliver
on 14 Aug 2023
Edited: Oliver
on 14 Aug 2023
I faced the same issue. I recompiled the relevant mbed source file (which is contained in the "Simulink Coder Support Package Using STMicroelectronics Nucleo Boards") and replaced the resp. object file from the Support Package to be able to increase the PWM resolution.
More detailed:
- Download mbed compile environment (I used mbed CLI 1 from here: Installing Mbed CLI - Quick start | Mbed OS 6 Documentation)
- Setup a new mbed project: in Windows CMD change to the desired path "cd xy" and use "mbed new <yourProjectName>" as described here Create - Build tools | Mbed OS 6 Documentation which will create a folder containing the new mbed project
- I named my project "mbedtest". It is located here C:\Users\<UserName>\mbed_tests\mbedtest. Then adapt C:\Users\<UserName>\mbed_tests\mbedtest\mbed-os\targets\TARGET_STM\pwmout_api.c. You have to adapt the function pwmout_period_us(), especially the lines where the prescaler is calculated. E.g. in the line "TimHandle.Init.Prescaler = (((PclkFreq) / 1000000)) - 1; // 1 us tick" divide by 10000000 instead of 1000000.
- Open Windows CMD in the project folder (in my case "C:\Users\<UserName>\mbed_tests\mbedtest"). Compile mbed using e.g. "mbed compile -m NUCLEO_F103RB -t gcc_arm" (when you compile it for the first time, this may take few minutes). Maybe you have to adapt the command for your board...
- Copy the created object file C:\Users\<UserName>\mbed_tests\mbedtest\BUILD\NUCLEO_F103RB\GCC_ARM\mbed-os\targets\TARGET_STM\pwmout_api.o to C:\ProgramData\MATLAB\SupportPackages\R2022b\toolbox\target\supportpackages\stmnucleo\mbed\NUCLEO_F103RB\TOOLCHAIN_GCC_ARM (it may be the case that your support package files are located in a slightly different path).
- Before building your model in Simulink, delete the output folder (e.g. nucleo_ert_rtw) which is created when the Simulink model is compiled.
For me this allowed an increase of the PWM resolution. However, be in mind that I did not do comprehensive tests. E.g. at the moment, when I specify the PWM frequency in the Simulink "PWM Output" block, I have to divide the frequency by 10 (due to the change above) to correctly set the frequency, e.g. for 1000 Hz I write 1000/10 to the frequency field inside the "PWM Output" block.
0 Comments
Communities
More Answers in the Power Electronics Control
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!