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 support@ccsinfo.com

how to convert read_adc() value to float?

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



Joined: 02 Mar 2006
Posts: 16

View user's profile Send private message

how to convert read_adc() value to float?
PostPosted: Tue Mar 28, 2006 7:59 pm     Reply with quote

How do you take the digital value returned by the read_adc() function as a float value?
Ttelmah
Guest







PostPosted: Wed Mar 29, 2006 2:32 am     Reply with quote

I depends totally on what float value you want....
In 'C', you can change a data 'type', using what is known as a 'cast', or just by assigning a value to a variable. So:

float value;
value=read_adc();

will put the floating point representation of the value from the ADC, into 'value'. The 'cast', is done like:

int16 value;
float value2;
value=read_adc();
//puts the integer value into value'
value2 = (float)value /4;

Will convert 'value' into a float (this is what the bracketted 'float' does), and then divide this by 4 using floating point arithmetic, and put the result into 'value2'. Now it is important to distinguish this from:

int16 value;
float value2;
value=read_adc();
//puts the integer value into value'
value2 = value/4;

This will take the _integer_ value 'value', and divide it by 4, using _integer_ arithmetic, and then convert the result to a float, and put this into value2.
So if the reading was (say) 127, the first version, would give 31.75, while the second, will just give 31.

Now it is also worth asking 'why' you need float?. Integer arithmetic is a _lot_ faster, and in many cases it is possible to generate results using a 'scaled integer', where you treat an int32 value (for example), as being the required value *10000. CCS has support for outputting such scaled values (using the %w format in printf), and it may be worth seeing if you can work out a way of performing your arithmetic in such a scaled integer, rather than a float.

Best Wishes
Storic



Joined: 03 Dec 2005
Posts: 182
Location: Australia SA

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 29, 2006 6:18 am     Reply with quote

Thanks for your explanation Ttelmah, I have been doing faily much that, however didnot break it down until now.

I found that you need to be aware of the result expected, I have been using the int16 that if the result roll over, I get a different reading. I should of used the int32 instead of dividing down down the result then display via printf %5.2w

Andrew
_________________
What has been learnt if you make the same mistake? Wink
Will Reeve



Joined: 30 Oct 2003
Posts: 209
Location: Norfolk, England

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Thu Mar 30, 2006 3:08 am     Reply with quote

Ttelmah, how long has %w been in the compilier. I had to write my own formatting function a while back to do this! RTFM I guess!

Keep well,

Will
Ttelmah
Guest







PostPosted: Thu Mar 30, 2006 7:00 am     Reply with quote

As with quite a few CCS functions, they are not in the manual!...
%w, has _just_ appeared in the manual, in about the latest release, but has been around for quite a while, in the 'readme.txt', that comes with the compiler download.
So the sequence it:
RTFM.
RT readme....

:-)
Best Wishes
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