Page 3 of 3

Re: How to dynamically change duty cycle with HardwareTimer library?

Posted: Sun Mar 13, 2022 8:20 am
by Bakisha
To my understanding (as a hobby user) :
setCaptureCompare is 0 indexed, CounterCompare Register (CCRx) is 0 indexed
setOverflow is 1 indexed, Hardware AutoReload Register (ARR) is 0 indexed.
setPrescaleFactor is 1 indexed, Prescaler register (PSC) is 0 indexed.

To set duty cycle to 100% CounterCompare Register must be greater than ARR (or same in setOveflow)

In example above, my error is only in comment, and should be:

Code: Select all

  PWMtimer ->setOverflow( 255 , TICK_FORMAT); // Overflow in 255 ticks (same for all 4 channels) // PWM base frequency: 72MHz/255=282.35KHz
Actaul hardware AutoReloaRegister (ARR) is 254, so 255 in CounterCompareRegister (CCR) (same as setCaptureCompare ) will be 100% duty cycle.

Again, this is just my opinion, based on information i found in source code/datasheet/internet.
Here is some links i found that helped me :
https://github.com/stm32duino/Arduino_C ... issues/897
https://github.com/stm32duino/Arduino_C ... #L852-L853

Re: How to dynamically change duty cycle with HardwareTimer library?

Posted: Sun Mar 13, 2022 12:26 pm
by Laserjones
To me, both the documentation at https://github.com/stm32duino/wiki/wiki ... er-library and the discussion you referred to at https://github.com/stm32duino/Arduino_C ... issues/897 sound like setCaptureCompare is indeed 1-indexed, same as setOverflow. But since I don't fully understand that discussion, I have posted a comment and asked whether I understood it correctly (because it's a bit counterintuitive to set CaptureCompare to 1 for a duty cyle of 0). Let's see whether he replies.

Re: How to dynamically change duty cycle with HardwareTimer library?

Posted: Mon Mar 14, 2022 8:27 am
by ABOSTM
With Arduino API setCaptureCompare() and setOverflow() I tried to abstract the complexity of Registers
(where sometimes -1 is applies sometime not, depending on register),
so for those API, when using TICK_FORMAT, the parameter is the number of tick (and not register value).
* setOverflow(): if you want 256 Tick, set parameter to 256 (and underlying ARR register will get 255)
* setCaptureCompare():
--> if you want 0 % duty which means 0 TICK, set parameter to 0 (and underlying register CCRx will get 0)
--> if you want 100 % duty which means 256 TICK, set parameter to 256 (and underlying register CCRxwill get 256)

Re: How to dynamically change duty cycle with HardwareTimer library?

Posted: Mon Mar 14, 2022 8:48 am
by ABOSTM
I just figure out that there was an error in documentation (wiki).
probably a wrong copy/past. It is now fixed
The right information is:

Code: Select all

CaptureCompare range: [0.. 0xFFFF]

Re: How to dynamically change duty cycle with HardwareTimer library?

Posted: Mon Mar 14, 2022 1:32 pm
by Laserjones
Thanks, ABOSTM. It all makes sense now. :)