CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

A math formula in AD

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sf



Joined: 02 Jul 2007
Posts: 9

View user's profile Send private message

A math formula in AD
PostPosted: Tue Sep 28, 2010 9:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 28, 2010 9:35 am     Reply with quote

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: 19549

View user's profile Send private message

PostPosted: Tue Sep 28, 2010 9:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 28, 2010 10:17 am     Reply with quote

Smile Thank you both for this great help.

@Ttelmah
The adc is from 0 to 1023 Embarassed 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

View user's profile Send private message

PostPosted: Tue Sep 28, 2010 10:26 am     Reply with quote

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: 19549

View user's profile Send private message

PostPosted: Tue Sep 28, 2010 2:25 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Sep 28, 2010 2:59 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group