How to dynamically change duty cycle with HardwareTimer library?

Post here first, or if you can't find a relevant section!
User avatar
Bakisha
Posts: 140
Joined: Fri Dec 20, 2019 6:50 pm
Answers: 5
Contact:

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

Post 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
Laserjones
Posts: 4
Joined: Sat Mar 12, 2022 12:03 am

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

Post 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.
ABOSTM
Posts: 60
Joined: Wed Jan 08, 2020 8:40 am
Answers: 7

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

Post 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)
ABOSTM
Posts: 60
Joined: Wed Jan 08, 2020 8:40 am
Answers: 7

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

Post 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]
Laserjones
Posts: 4
Joined: Sat Mar 12, 2022 12:03 am

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

Post by Laserjones »

Thanks, ABOSTM. It all makes sense now. :)
Post Reply

Return to “General discussion”