2
votes

I am using STM32F103C8Tx and I am trying to create a PID adjustable PWM. In PID I will be giving values to PWM from 0 to 100 to set the duty cycle. That means my counter period needs to be 100 in Cubemx. Because as I have learnt while I am changing the dutch cycle it changes the counter period. However, I need to create PWM with 200 KHz frequency and the clock of the timer is 72 MHz.In order to achieve a value of 100 in counter period I need to have a 3.6 prescaler. Am I following a wrong way or did I get this PWM duty cycle thing wrong in stm32?

Note: I am using htimx.Instance->CCRx =Duty_Cycle; in order to change the duty cycle from code.

1
It might be more meaningful if you use PWM clock ticks as your internal unit, not some artificial "0 to 100" thing. The PID regulator doesn't care if it calculates cycles, milliamps or number of carrots, it's just raw math.Lundin
Yes but without the PID loop. I need a duty cycle with at least 100 sensitivity. ( I need to set duty cycle to %7 or %52 etc.) Which means my counter period needs to be at least 100. And for easy calculation I want to make it 100 so that I won't need to change PID output. ( PID output will be input to timer as duty cycle.) But I guess it looks impossible with 72 MHz timer clockGünkut Ağabeyoğlu
Which means my counter period needs to be at least 100 One percent is 1/100 part of a "whole". It doesn't mean that the "whole" needs to be exactly 100. Can't you set it to for example 18000?KamilCuk
With 72 MHz clock and 200 kHz expected PWM frequency. The max clock period I can set is 360. With prescaler "1".Günkut Ağabeyoğlu

1 Answers

3
votes

To achieve 0-100% duty cycles, you don't have to set the counter period is 100. Instead, you can set it higher and the toggle threshold is higher. Let's use your example, you need 200KHz frequency and your clock of the timer is 72Mhz so the counter period should be 360, and the threshold value should be htimx.Instance->CCRx = (int)(360 * dutyCycle / 100). But if you use PID to control a motor, you must consider it's deadband and the 0-100% will correspond with 60-360 and the value should be htimx.Instance->CCRx = (int)((360 - 60) * dutyCycle / 100)

In the case that you need the exact value, you can adjust the system frequency to 50Mhz or even 100Mhz(yes you can, but I don't encourage that) or you can use another MCU like stm32f401ccu6 with the same cost.

P/S: just my curiosity, what is the target that you want to control. I mean, if it is a motor, I think 200Mhz is quite high. To control a motor, I usually use 20Mhz ( oh my low-cost driver :) ) and the CCRx value will be more beautiful.