How to change Duty cycle of PWM using API

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi,

I want to change the duty cycle of an PWM  frequenctly  as per my requirement.

Can you tell me how to do it and also  tell me the how to calculate the duty cycle based on input clk frequency

Thanks in advance

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The PWM duty cycle is independent of the input frequency.

Duty Cycle in % = 100 * Period / Compare value

Both values can be changed by APIs, see the PWM datasheet.

Bob

View solution in original post

9 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The PWM duty cycle is independent of the input frequency.

Duty Cycle in % = 100 * Period / Compare value

Both values can be changed by APIs, see the PWM datasheet.

Bob

Anonymous
Not applicable

Thanks Bob for reply

0 Likes
Anonymous
Not applicable

hI,

i want to generate a pwm dutycycle 20% to 90%  but these below apis

   PWM_WritePeriod(3);

PWM_WriteCompare(10); 

given the same waveform output  that is it's not working.

according to your formula  Duty Cycle in % = 100 * Period / Compare value 

but it's not working.

Please help me to resolve the issue.

Thnbaks in advance.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

hI ,

i want to generate pwm for 10% duty cycle to 90% dutycycle .For every 10 seconds i am changing the period and compare value.

But no pwm output is coming i mean pwm getting stopped

please help me to generate duty cycle.

Thanks in adavance

0 Likes
Anonymous
Not applicable

in my code i removed the while(1);   it's not working

0 Likes
lock attach
Attachments are accessible only for community members.

Sorry, reziprok error:

Duty Cycle in % = 100 / Period * Compare value

or

Compare Value = Period * Duty cycle  in % / 100

See attached project

Bob

0 Likes
Anonymous
Not applicable

Thanks sir

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi bob,

I attached  an snapshot of the pwm dutycycle.

According to your formula  ,the duty_cycle should  be 50% but  it shows the waveform in the configuration as a 70% dutycycle waveform.

compare value=1,period=2ms  ,ip frequency is 1000HZ .

please help me to resolve the issue.

Thanks in advance.

0 Likes

Hi

It is better to use the PWM block with higher counts(high period value) for higher accuracy. Other wise one clock cycle for toggle state can introduce this much error.

0 Likes