View previous topic :: View next topic |
Author |
Message |
sf
Joined: 02 Jul 2007 Posts: 9
|
A math formula in AD |
Posted: Tue Sep 28, 2010 9:19 am |
|
|
Hi,
Today isn´t a real good day for me, i have been stucked in a math formula, that i can´t realise, well vacations is need........
But if anybody can help me.....thanks in advance.
The formula i need is the next :
Based on an ADC read at 10 bits.
Having the values "MAX" and "MIN".
I need to "translate" the AD read´s to value of "MIN" to "MAX".
For example:
The "MAX" and "MIN" are values that i change any time.
ADC values from 1 to 1024.
Min=50 and Max=300
My result sould be:
for adc=1 -> result=50
for adc=1024 -> result=300
thanks in advance.
sf |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Sep 28, 2010 9:35 am |
|
|
Code: | input min;
input max;
diff=max-min;
step=diff/1024;
result=adc_value*step;
result+=min; |
this is very off the top of my head... i believe that should doit...
use interger math to make it quicker.... its going to take some ram...
and 16-32 bit vars.
hope that helps... or at least puts you on the right path...
sorry for the crude Pseudo code...
Gabriel. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Tue Sep 28, 2010 9:42 am |
|
|
Are you sure about your ADC values?.
The normal ADC return for a 10bit ADC, would be 0 to 1023, not 1 to 1024....
Range though is 1023 counts in each case.
Code: |
int32 adcval; //note int32 to avoid arithmetic wrap
//code to get the value here
if (adcval<MIN) adcval=MIN;
else {
adcval=((adcval*(MAX-MIN)/1023)+MIN;
}
|
So for half way (512), 512*250 (MAX-MIN) = 128000
Divided by 1023 (in integer)=125
Plus 50 = 175
Half way between MAX and MIN as required. Same applies for the whole range.
Best Wishes |
|
|
sf
Joined: 02 Jul 2007 Posts: 9
|
|
Posted: Tue Sep 28, 2010 10:17 am |
|
|
Thank you both for this great help.
@Ttelmah
The adc is from 0 to 1023 I´m thinking in another value.
My mistake is when i put 1 to 1024, i´m was thinking to add 1 to the adc reading´s for make easier other values.
It´s another way to see that i need vacations very fast.........
Thank you all.
SF |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Sep 28, 2010 10:26 am |
|
|
I believe with the exception of the correction of the min value, we suggested the same idea....(logicwise)
I'll keep that in mind (the min value correction) if I ever need to do something like this.....
nice...
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19513
|
|
Posted: Tue Sep 28, 2010 2:25 pm |
|
|
Absolutely.
I started typing my reply before your's was posted, so "wasn't aware of it".
Logic identical.
Best Wishes |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Sep 28, 2010 2:59 pm |
|
|
yeah it happens...(simultaneous posting)
yours is the " true answer " though... i didnt take in account the min value correction.... because i didnt consider "limit tests" on my inputs for my solution....
cheers
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|