Changing the duty cycle of PWM

Question: How do I change the Duty Cycle of a PWM?

 

Answer:

Duty Cycle of PWM can be modified by writing the desired value to the Period Register or the Compare Register. The duty cycle in % is given by the following formula.

Duty Cycle = (Compare Value / Period Value) * 100

To prevent glitches on the PWM output, the compare value register should be updated only when the PWM is stopped or when the PWM has reached the zero count.