# Fractional Duty Cycles

Discussion in 'Microcontrollers' started by elecLear78, Jun 22, 2017.

1. ### elecLear78Member

Joined:
Jul 16, 2013
Messages:
226
Likes:
1
Location:
india
Is it possible to have fractional duty cycles in PWM like 2.1, 23.5 etc? Does it have any significance?

2. ### PommieWell-Known MemberMost Helpful Member

Joined:
Mar 18, 2005
Messages:
10,022
Likes:
317
Location:
Brisbane Australia
ONLINE
The pic pwm module can have 1024 steps from 0 to 1023. So if you are talking percentages then 23.5% is doable. If you're not talking percentages then we need to know the units.

Mike.

3. ### elecLear78Member

Joined:
Jul 16, 2013
Messages:
226
Likes:
1
Location:
india
Thanks for the reply. I am talking about percentages. But why only 1024 steps ?

Joined:
Jan 12, 1997
Messages:
-
Likes:
0

5. ### PommieWell-Known MemberMost Helpful Member

Joined:
Mar 18, 2005
Messages:
10,022
Likes:
317
Location:
Brisbane Australia
ONLINE

It's a 10 bit PWM module. 2^10 = 1024.

Mike.

6. ### MisterbennActive Member

Joined:
May 30, 2005
Messages:
502
Likes:
28
Location:
Birmingham, UK
the number of steps is a function of the number of bits in the register to control the duty cycle.

so an 8 bit register would give 256 steps 2^8

7. ### elecLear78Member

Joined:
Jul 16, 2013
Messages:
226
Likes:
1
Location:
india
But every time will it be 8 bit or 10 bit? Because as per the data sheet information the resolution is changing, even though the pdc register is 16 bit for duty cycle. It keeps varying based on the pwm frequency I think. Can you please help me understand. I am not sure if i am following or i am deviating from the actual question. Does fractional duty cycle in percentages and the resolution have any relation?

8. ### PommieWell-Known MemberMost Helpful Member

Joined:
Mar 18, 2005
Messages:
10,022
Likes:
317
Location:
Brisbane Australia
ONLINE
If you look at the PDCx value, you will notice that the number of bits set is the same as the resolution. I.E. 0x3fff = 11 1111 1111 = 10 bits set. This is the value that the counter resets at and so the max PWM value is the same as this max value. However, because the counter is being clocked at a certain frequency then the frequency drops as the value of PDC increases. E.G. at 10MHz and PDC value of 0x1ff (512 steps) the frequency is 10,000,000/512 = 19.5kHz - at 20MHz = 39.1 etc.

Mike.

9. ### MisterbennActive Member

Joined:
May 30, 2005
Messages:
502
Likes:
28
Location:
Birmingham, UK
so the faster the PWM frequency the fewer possible steps in your duty cycle.

In your application you need to select a duty cycle depending on what your doing. So for an LED dimmer 915Hz will be more than fine and this gives you 16bit duty cycle register.

10. ### elecLear78Member

Joined:
Jul 16, 2013
Messages:
226
Likes:
1
Location:
india
One final question take the example of 1st one. Max my PDC is 0xFFFF. So if i load 65535 i will get 100% duty. Now let us say i load 65534 into PDC register then what will be my duty? Will it be (65534/65535)*100%? And 1 bit corresponds to (1/65535)*100%. Please advise.

11. ### MisterbennActive Member

Joined:
May 30, 2005
Messages:
502
Likes:
28
Location:
Birmingham, UK
Almost, you need to use 65535-1, as there are 65535 states and the lowest state is equal to zero duty cycle.
so 1 bit corresponds to 100/(65535-1)

• Like x 1
12. ### elecLear78Member

Joined:
Jul 16, 2013
Messages:
226
Likes:
1
Location:
india
Ok thank you very much.

13. ### PommieWell-Known MemberMost Helpful Member

Joined:
Mar 18, 2005
Messages:
10,022
Likes:
317
Location:
Brisbane Australia
ONLINE
Nope, definitely 65536 states - 0 to 65535. So 1 bit equals 1/65536 or 0.0015%

Mike.

14. ### MisterbennActive Member

Joined:
May 30, 2005
Messages:
502
Likes:
28
Location:
Birmingham, UK
My bad 2^16 is 65536 not 65535.
but i stand by my logic that you need to do 1/(65536-1), so 1 divide by one less than the number of states

Perhaps i'm being dumb, but I've talked through it below.

if i simplify it to just 2 bits for example.
2^2 gives me four states:
00
01
10
11

00 is state 1 (not state 0) and would be 0% duty cycle
11 is state 4 and would give 100% duty cycle.

if i just divide 1 by the number of states (1/4) as Pommie has done then i get each bit equal to 25%
But that's not right. In state 1 (00) i have 0% duty but the above suggests i should have 25%

instead if i say 1 divided by the number of state less 1 [ 1 / (4-1) OR (1/3) ]. that gives me 33% per bit
so state 1 is (1-1 * 33) = 0% duty which is correct
2 is (2-1 * 33) = 33.3%
3 (3-1 * 33) = 66.6%
4 (4-1*33) = 99.9%