duty cycle calculation

parisien

Member
hi

im playing doing some calculations with picaxe 08M.i wanted to calculate the duty cycle using the formula D=1-(Vi/Vo).i did

Symbol vInAdc = b0
Symbol Vi = b1
Symbol Vo = b4
Symbol duty = b2
Symbol difference = b3

Vo= 227 ' 12.0V
Vi = vInAdc
difference = Vi*100/Vo
duty =1-difference

pwmout 2, 99 , duty

my problem is: taking for example Vin=3.5V ,gives duty=70.833%
how do we convert this value in a number cunderstable by picaxe which correspond to the same value??
 

BeanieBots

Moderator
That would depend on your scaling.
You need to know what voltage is presented to the ADC pin from your divider.
You also need to know your PICAXE supply voltage.
From that, you can calculate what NUMBER will be returned by the ADC for any given voltage.

I'm guessing from your code that 277 represents 12v.
Hence 3.5v would give 80. (assuming a linear input).
 

hippy

Ex-Staff (retired)
@ parisien : Have a play with the PICAXE PWM Wizard ...

The duty value is between 0 to 4 times plus one of the frequency value, so for "PWMOUT 2,99,duty" duty can range from 0 to 400, equivalent to 0% to 100%, and if my maths is right ...

duty value = ( ( frequency value * 4 ) + 1 ) * ( duty% / 100 )

Choosing 75% at 10kHz, duty value = ( (99*4)+1 ) * (75/100) = 300, which matches with the PWM Wizard.
 

parisien

Member
my picaxe voltage is 4.5V.
i have half divider, so when got 3.5V, i have Vadc =1.75V.. this correspond to value Nadc= 1.75*255/4.5= 99

Symbol vInAdc = b0
Symbol Vi = b1
Symbol Vo = b4
Symbol duty = b2
Symbol difference = b3

Vo= 227 ' 12.0V
Vi = 99 ' Nadc= 1.75*255/4.5
difference = Vi/Vo '
duty =1-difference

pwmout 2, 99 , duty

gives duty=70.833%
lets say i have a duty of 73, how to return this value in a number understable by picaxe which correspond to the same value??
 

boriz

Senior Member
You already have the answer.

Use the PWM wizard.

Try this: Enter your chosen frequency, then set it for 100% duty. The resulting number at the end of the statement represents the duty resolution (the number of possible discrete steps you can set the duty to for that particular period frequency).

EG: The default wizard values are 10000Hz @ 50%. Change the 50% to 100% and click calculate. You get 99,400. The 99 is the period, the 400 is the maximum number of discrete duty steps possible at that frequency. 200 would be 50% duty. 100 would be 25% duty. Each step represents (100/400)% = 0.25% resolution.

Do it again for 4000Hz @ 100%. You get a period of 249 and a number of duty steps of 1000. So 500 would be 50% duty. 250 would be 25% duty etc. Each step is (100/1000)% = 0.1% resolution.

The higher the frequency (smaller period), the lower the duty resolution. At 500kHz, you only get a maximum of 8 steps. 8=100%, 4=50% etc. Each step is (100/8)% = 12.5% resolution.
 
Top