简体   繁体   English

STM32 中 PWM 的预分频器和周期值

[英]Prescaler and period values for PWM in STM32

I am using STM32F103C8Tx and I am trying to create a PID adjustable PWM.我正在使用 STM32F103C8Tx,我正在尝试创建一个 PID 可调 PWM。 In PID I will be giving values to PWM from 0 to 100 to set the duty cycle.在 PID 中,我将为 PWM 提供 0 到 100 的值以设置占空比。 That means my counter period needs to be 100 in Cubemx.这意味着我的计数器周期需要在 Cubemx 中为 100。 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.但是,我需要创建频率为 200 KHz 的 PWM,定时器的时钟为 72 MHz。为了在计数器周期内达到 100 的值,我需要一个 3.6 的预分频器。 Am I following a wrong way or did I get this PWM duty cycle thing wrong in stm32?我是按照错误的方式还是我在 stm32 中弄错了这个 PWM 占空比?

Note: I am using htimx.Instance->CCRx =Duty_Cycle;注意:我正在使用htimx.Instance->CCRx =Duty_Cycle; in order to change the duty cycle from code.为了从代码中更改占空比。

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.要实现 0-100% 占空比,您不必将计数器周期设置为 100。相反,您可以将其设置得更高,并且切换阈值更高。 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) .让我们以您的示例为例,您需要 200KHz 频率,并且您的定时器时钟为 72Mhz,因此计数器周期应为 360,阈值应为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)但是如果你用PID控制电机,你必须考虑它的死区,0-100%对应60-360,值应该是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.如果您需要确切的值,您可以将系统频率调整为 50Mhz 甚至 100Mhz(可以,但我不鼓励这样做),或者您可以使用其他 MCU,例如 stm32f401ccu6,成本相同。

P/S: just my curiosity, what is the target that you want to control. P/S:只是我的好奇,你想控制的目标是什么。 I mean, if it is a motor, I think 200Mhz is quite high.我的意思是,如果是电机,我认为 200Mhz 相当高。 To control a motor, I usually use 20Mhz ( oh my low-cost driver:) ) and the CCRx value will be more beautiful.为了控制电机,我通常使用 20Mhz(哦,我的低成本驱动器:)),CCRx 值会更漂亮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM