|
|
View previous topic :: View next topic |
Author |
Message |
xinyh
Joined: 23 Mar 2004 Posts: 11
|
A question of C programming |
Posted: Mon Jun 28, 2004 6:39 am |
|
|
I would like to convert a float value to whole number values individually, e.g. float X0 = 4.63, convert to int x1 = 4, x2 = 6, x3 = 3. Appreciate for telling me how to do that!
With Best Regards,
Xinyh |
|
|
jds-pic
Joined: 17 Sep 2003 Posts: 205
|
Re: A question of C programming |
Posted: Mon Jun 28, 2004 8:12 am |
|
|
xinyh wrote: | I would like to convert a float value to whole number values individually, e.g. float X0 = 4.63, convert to int x1 = 4, x2 = 6, x3 = 3. Appreciate for telling me how to do that!
With Best Regards,
Xinyh |
one way, convert it to a string and then iteratively step through the string array.
jds-pic |
|
|
xinyh
Joined: 23 Mar 2004 Posts: 11
|
|
Posted: Mon Jun 28, 2004 8:20 am |
|
|
OK. Then I can use sprintf() to convert it first, thanks! |
|
|
Guest
|
|
Posted: Tue Jun 29, 2004 1:37 am |
|
|
It stiil doesn't work, can anyone give me a example for how to convert it? or can I change my question like this: I want to display the avlue of Read_ADC() in four LEDs, anyone has this programming? Thanks a lot! |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 29, 2004 3:30 am |
|
|
Anonymous wrote: | It stiil doesn't work, can anyone give me a example for how to convert it? or can I change my question like this: I want to display the avlue of Read_ADC() in four LEDs, anyone has this programming? Thanks a lot! |
Lets deal with different answers and routes. First, the sprintf solution will work, but requires that you specify the field width. You need to force the output to generate four characters (and remember you must have space to store _five_). However the best solution, is really to go away from using a 'float' in the first place!. Assume for instance, that you have the AD reading 0 to 5v, and you want to display this as 0.000 to 5.000. Then instead of converting the incoming AD count (0-1023), using something like:
float volts;
volts = read_adc()*.0048828;
Which takes a suprising amount of work, and leaves you with a floating point number needing scaling to output, instead use:
int16 volts;
volts = ((int32)read_adc()*320000L)/65536L;
Here the compiler will optimise the division by 65536 to just taking the upper 16bits of the number, and the int32 oprations, are much quicker than float arithmetic. The result is an int32, containing a value from 0 to 5000.
Then the digits from this can be extracted with (you must include 'stdlib.h'):
Code: |
int8 ctr;
ldiv_t divval;
for (ctr=0;ctr<4;ctr++) {
divval=ldiv(volts,10);
volts=divval.quot;
//Here divval.rem, will contain the digits in turn
printf("%1Ld", divval.rem);
}
|
Obviously, in your case, you will have to put the digits in turn to the LED's, rather than just printing them as I show.
Best Wishes |
|
|
xinyh
Joined: 23 Mar 2004 Posts: 11
|
|
Posted: Tue Jun 29, 2004 4:15 am |
|
|
Hi Ttelmah, my CCS is PCB Version 2.722, it dosen't support int8, int16, and int32, it only has long int, in this way, how to do? Thanks very much! |
|
|
|
|
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
|