Why the duty cycle change when I just want to change period

Forum for problems or queries regarding other Flowcode Components. Eg LEDs, Switches, LCD, Gfx LCD etc

Moderators: Benj, Mods

Post Reply
WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Why the duty cycle change when I just want to change period

Post by WalkOver »

Hello !

I want to use a trimmer to generate a PWM with a 50% duty cycle but with a variable frequency.

In Flowcode, i set at the beginning of the program duty cycle = 127 and for the nPeriodVal, a variable between 0 and 255 from the ADC component.

When i turn the trimmer, frequency AND the Duty cycle change.....Why ?

Is it possible to do what i want ?

Thank you for your help !

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times
Contact:

Re: Why the duty cycle change when I just want to change period

Post by Sean »

The duty and period values used for PWM are both referenced to a common counter register. The output is turned off when the counter reaches the duty value. The counter is reset, and the output turned back on, when the counter reaches the period value.
The duty value of 127 will only produce a 50% duty cycle when the period value is 255. If the period value is reduced, the duty period will remain constant and become a higher percentage of the cycle.

To maintain a 50% duty cycle when the period value is changed, the duty value must also be changed to (period - 1) / 2

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: Why the duty cycle change when I just want to change period

Post by WalkOver »

Oh thank you so much. It works very well now !

WalkOver
Posts: 65
Joined: Fri Nov 23, 2007 11:24 pm
Contact:

Re: Why the duty cycle change when I just want to change period

Post by WalkOver »

Hello !

I have an other question....

I use a joystick which gives me a variable between 0 and 255 and setup my PWM duty cycle. What I want is add a trimmer which setup my PWM frequency without make any change on my PWM duty cycle. I don't understand how PWM works, all my tests change my PWM frequency AND my PWM duty cycle. Someone knows something about that ?

Thanks !

Post Reply