HOW PRECISE IS PWM

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

HOW PRECISE IS PWM

Post by siliconchip »

hi all

I've just started to learn how to use PWM with the help of members on this forum, however I've noted that its difficult to obtain certain frequency's and that at best you can get a frequency either side of the one you want from several Hz either way, this was backed up when i checked an online calculator for pwm when you input your clock frequency say 8MHz then ask for 50% duty the frequency range starts at 488Hz and from here increments in 2Hz steps so 488, 490, 492 etc and the drift becomes greater the higher you go until your around 10Hz adrift of the frequency you would like ie 1117Hz but the closest is 1126 or the other way 1106 Hz. i then acquired a signal generator module from eBay to check for accuracy, the module lets you pick a frequency from 1Hz to 150KHz and you can adjust the duty from 0 - 100% all done with buttons and using an lcd but when using a fluke to measure the output it was found on some frequency's again this could be as worse as over or under up to 20Hz difference. i don't know if I've got this concept totally wrong but can you get an exact frequency you want pending on prescaler or would you require more than 10 bits to achieve this accuracy if in deed it does exist, thanks in advance

bob

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: HOW PRECISE IS PWM

Post by mnf »

Yes, PWM has a number of discrete steps - each of a number of Hz...

For example 10 bit resolution will give 1024 steps over the PWM range.

The PIC documentation gives the following on 'PWM 'resolution

IMG_20210829_185338.jpg
IMG_20210829_185338.jpg (240.58 KiB) Viewed 7398 times
(sorry had to photo the page here - for better reading download from microchip.com)

Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HOW PRECISE IS PWM

Post by siliconchip »

Hi martin

Thanks for the reply, ive experimented with 4, 8, 16 and 20 MHz clocks but didnt notice any difference in results even though by the data sheet saying 10 bit on the highest frequency (20 meg) as opposed the 8 MHz in your picture

Bob

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: HOW PRECISE IS PWM

Post by mnf »

I did this: (a while ago)
freq2.fcfx
(16.96 KiB) Downloaded 189 times
That did a frequency sweep (on a pic16lf1513) and the output looked good on a scope... Approx 15Hz steps.

Martin

User avatar
Bachman
Posts: 116
Joined: Sun Sep 07, 2014 11:37 am
Location: Hungary
Has thanked: 9 times
Been thanked: 53 times
Contact:

Re: HOW PRECISE IS PWM

Post by Bachman »

For example, let's see the datasheet of PIC12F1840.

The PWM frequency is based on Timer2 settings (prescaler + PR2 values) and Fosc. The frequency is not is linearly related to the PR2 value. For equation, see datasheet. You can't say, one step in PR2 equals n Hertz.
PWM_PR2.png
PWM_PR2.png (70.87 KiB) Viewed 7361 times
PWM_PR2_2.png
PWM_PR2_2.png (26.92 KiB) Viewed 7361 times
As you see, not every frequencies can be generated by a simple PWM generator and you can't create fixed step values. The precision of the PWM frequency is based on the precision of the oscillator.

Don't mix the frequency with the duty cycle ratio. PWM resolution = Duty cycle ratio resolution. You can get 10 bits if PR2=255. If PR2=63, the resolution is 8 bits. If PR2=0 you get 2 bits resolution and this means, you can generate 0, 25, 50, 75 and 100 % duty cycle ratio, no intermediate steps. For equation, check datasheet or use online PWM calculator.
Duty.png
Duty.png (22.46 KiB) Viewed 7361 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: HOW PRECISE IS PWM

Post by siliconchip »

Hi bachman

Thanks for the reply, thats what i thought as per my question so my next question can anyone tell me whats the answer to finding a way to generate any frequency when using a pic micro thanks in advance

Bob

User avatar
Bachman
Posts: 116
Joined: Sun Sep 07, 2014 11:37 am
Location: Hungary
Has thanked: 9 times
Been thanked: 53 times
Contact:

Re: HOW PRECISE IS PWM

Post by Bachman »

Check IC (module) AD9833.

With 25 MHz clock, you can get output frequency (for example) 19,999.96602535247802734375 Hz. This close enough to 20 kHz. I think, for home projects, it's more than enough. One step is 0.0931322574615478515625 Hz (25 MHz/2^28). In this chip, one step (bit) is always the same frequency step up or down.

Post Reply