Page 1 of 1

PWM duty cycle not adjusting

Posted: Fri Aug 01, 2014 12:04 pm
by singerinst_ben
Hi,

I think I've found a problem with the PWM component. On our project I have the period set to 255, the prescale 16 and the duty cycle 128. This gives me a frequency of 2.92Hz, a period of 3.41us and an on and off of around 171us, so a 50/50 duty cycle.

When we scope the output on the MIAC that the PWM is running through (A) we have found the duty is vastly different - actual amount 90/10 (30us off, 300us on). Adjusting the duty to 64, you would expect the on duty to be around 25%, and again setting it to 32 you would expect 12.5% but when scoped it doesn't change noticeably. It updates correctly on the simulation though.

Any help is much appreciated!

Regards,

Ben

Re: PWM duty cycle not adjusting

Posted: Fri Aug 01, 2014 3:55 pm
by LeighM
Hi Ben,
I cannot see any fault. I’ve attached a test program.
Regards,
Leigh

Re: PWM duty cycle not adjusting

Posted: Wed Aug 06, 2014 11:49 am
by singerinst_ben
Apologies - I wasn't setting the duty and period after enabling the PWM, it appears you have to set these each time you enable. Is there any way of reducing the frequency further, we are using a prescale of 16 to reduce the frequency, but could do with dividing this by 2 again to further reduce the frequency. Is this possible? The maximum prescale appears to be 16.

Regards,

Ben

Re: PWM duty cycle not adjusting

Posted: Wed Aug 06, 2014 1:47 pm
by Benj
Hello,

It looks like the device only has a max prescaler of 16 for the PWM so the hardware PWM cannot be run any slower.

You could use a timer based software PWM and this should give you much more scope for setting the frequency.

An example of software based PWM using a timer interrupt can be found here.
http://www.flowcode.co.uk/article.php?a=52

The count variable sets the period in multiples of the timer interrupt timeout and the PWMx variables set the various channel dutys.

Let me know how you get on.

Re: PWM duty cycle not adjusting

Posted: Thu Aug 07, 2014 11:03 am
by singerinst_ben
Thanks, great idea, I hadn't thought of that. Our stepper river board should arrive later so once we have given it a go I'll let you know if we need to go the software route! Thanks again for your help.

Ben