![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
zonemikel
Joined: 13 Oct 2007 Posts: 53 Location: Texas
|
Cant cast or print float in printf |
Posted: Thu Jul 29, 2010 2:14 pm |
|
|
I'm trying to take the adc value and make it a float but every time i try and cast it It wont print. I've tried it several different ways but this is one, any idea why ?
Code: | // global variables for this .h
int8 battVolts = 0;
int8 chgVolts = 0;
unsigned ChgSec = 0;
void chargeBattery(){
set_adc_channel(13); // vdivider hooked to battery (39/(47+39))*vin
// reset vars, if not it will hold the last value
battVolts = 0;
chgVolts = 0;
// turn off charge input and read battery
output_high(CHGPIN);
delay_ms( 900 );
// if negative delta v or one hour stop charging and go to idle state
if(battVolts < read_adc() || ChgSec > 3600){
printf("Battery Charged!, NDV detected in %u Seconds", ChgSec);
state = '0'; // return to idle state
}else{ // continue charging
battVolts = read_adc(); // latest battery voltage
// turn on charge input and read charge voltage
output_low(CHGPIN);
delay_ms(50);
chgVolts = read_adc();
printf("\f Chg: %u Batt: %u %f ",chgVolts,battVolts,(float)battVolts/255*5); // <<<-- this line !!!!
}
} |
I'm on a '887 and using #adc=8
Thanks for your time _________________ Smart people know how stupid they are. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 29, 2010 2:25 pm |
|
|
Some versions of the compiler require that you put in a width and a
precision field for "%f". Also, the width field must be at least one
greater than the precision field. Try this:
|
|
![](templates/subSilver/images/spacer.gif) |
zonemikel
Joined: 13 Oct 2007 Posts: 53 Location: Texas
|
|
Posted: Thu Jul 29, 2010 2:36 pm |
|
|
Nope that didnt work ... it just prints nothing where it should print the float
Chg: 183 Batt: 0 time:69
Chg: 183 Batt: 0 time:70
Chg: 183 Batt: 0 time:71
Chg: 183 Batt: 0 time:72
Chg: 183 Batt: 0 time:73
with
Code: | printf("\f Chg: %u Batt: %7.3u time:%Lu ",chgVolts,(float)(battVolts/255)*5/.453,ChgSec); |
never mind just saw the 'u' ...
ok now it just prints like it did before cutting off everything after the float
Chg: 182 Batt:
Chg: 182 Batt:
Chg: 182 Batt:
Code: |
printf("\f Chg: %u Batt: %7.3f time:%Lu ",chgVolts,(float)(battVolts/255)*5/.453,ChgSec); |
_________________ Smart people know how stupid they are. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 29, 2010 2:55 pm |
|
|
Code: | (float)(battVolts/255)*5/.453 |
This is going to give you 0, most of the time. battVolts is an 'int8'.
So, say it's 100. Then 100/255 in integer math is 0. Then you
cast it to a float. But you're just casting a 0 to 0.0, and then multiplying
0.0 * 5 and doing the division still gives 0.
My advice is to make a little test program that only does the math and
displays it. Play around with it. Get it right, and then integrate it into
the rest of your program. Example:
Code: |
#include <16F887.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//===========================
void main()
{
int8 battVolts;
float result;
battVolts = 100;
result = (float)battVolts/255*5;
printf("result = %7.3f \r", result);
while(1);
}
|
If this doesn't help, then post your compiler version. |
|
![](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
|