![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
dacenrie
Joined: 02 Mar 2006 Posts: 16
|
how to convert read_adc() value to float? |
Posted: Tue Mar 28, 2006 7:59 pm |
|
|
How do you take the digital value returned by the read_adc() function as a float value? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Mar 29, 2006 2:32 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
|
Posted: Wed Mar 29, 2006 6:18 am |
|
|
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](images/smiles/icon_wink.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Will Reeve
Joined: 30 Oct 2003 Posts: 209 Location: Norfolk, England
|
|
Posted: Thu Mar 30, 2006 3:08 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Mar 30, 2006 7:00 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|