PDA

View Full Version : ADCIN > HPWM but with min/max limits - i failed maths



jamie_s
- 27th November 2009, 10:21
Im just working on a small project, but my maths skills are not 100% so I'll ask here and see what you guys reckon:

I have an ADC input, that receives an 0-5v level based on a potentiometer, but in this application, the ADC input is never 0-255 (lets assume 8 bits)
For example the min ADC reading may be 12, and the max 221.
The min and max readings will be stored on the eeprom.

So assuming the 12 and 221 as min and max, gives a range of 209 steps

Now, I need to output a PWM (HPWM) signal, whose duty cycle varies inversely proportional to the ADC, but there is also a min and max PWM % that needs to be set.
Lets use 12% and 88% as the limits here, so min duty is 38 and max is 224 with a range of 186 steps. These max and mins are also in EEPROM

Therefore, what i want to achieve -
ADC = 12, PWM = 224
.
.
.
ADC = 221, PWM = 38

(and everything in between)

Whats the best way of performing this calculation?

My initial thoughts are to divide the no of ADC steps by the no of PWM steps and multiply this result, by the ADC reading then inverting the bits to give me the PWM duty cycle, does this make sense??

I told you i failed my maths classes :rolleyes:

Gusse
- 27th November 2009, 18:35
Hi Jamie_s,

With HPWM you cannot do this if I understood right. Somebody correct if I'm wrong.
Did you mean that with ADC = 12 you will get one pulse high/low in every 224 and ADC = 221 one pulse high/low in every 38?

If yes, then try something like this.


<code><font color="#000000">adval <b>VAR BYTE

</b>Loop:
<b>GOSUB </b>Read_ADC
<b>HIGH </b>PWM_Out <font color="#000080"><i>'Change to match your pin name for PWM output!
</i></font><b>PAUSE </b>1
<b>LOW </b>PWM_Out
<b>PAUSE </b>adval
<b>GOTO </b>Loop

Read_ADC:
<b>ADCIN </b>0, adval
adval = 235 - ((89 * adval)/100)
<b>RETURN</b></code>

Funtion that is used here is y = 235 - 0.89*x.

x = 221 -> y = 38.21
x = 12 -> y =224.32
It should give you sufficient accuracy, I hope so.

BR,
-Gusse-

Melanie
- 27th November 2009, 20:35
ADCMin is the Minimum ADC Value we can have. ADCMax is the Maximum ADC Value we can have. Every routine must know it's limitations so first lets make sure we stay in range...


If ADC < ADCMin then ADC=ADCMin
If ADC > ADCMax then ADC=ADCMax
Now let's convert our ADC value... which varies between ADCMin (0%) and ADCMax (100%) but to an INVERSE Percentage


ADC=100-(((ADC-ADCMin)*100)/(ADCMax-ADCMin))
Our PWM varies between PWMMin (0%) and PWMMax(100%), so here let's calculate the PWMOut Value we need by Applying our previously calculated Percentage and not forgetting to add-in our minimum offset


PWMOut=(((PWMMax-PWMMin)*ADC)/100)+PWMMin
Four lines... and unless I've dropped a clanger - that should do the job.

Put some figures into the variables, pencil, paper and calculator, and see if it works for you...

mackrackit
- 27th November 2009, 21:21
Melanie,
I have a question about your post...
What does
b*****k
mean???

Melanie
- 27th November 2009, 22:22
hmmm... consider it an an English engineering technical term... which can be used in the plural... forgot we have an international audience...

mackrackit
- 27th November 2009, 22:40
hmmm... consider it an an English engineering technical term... which can be used in the plural... forgot we have an international audience...
Well I am just an old hillbilly that never did so good in English :)
I even looked in the manual. Seeing that you changed the previous post I guess I was looking in the wrong manual :D

Keep having fun!!!

jamie_s
- 29th November 2009, 02:02
Melanie,

Seems your code works spot on :)

Thankyou ;)