|
|
View previous topic :: View next topic |
Author |
Message |
Guest_X44 Guest
|
Printf a negative float number |
Posted: Mon Feb 02, 2004 11:25 pm |
|
|
I can't get to send the correct value to the terminal. I'm expecting a value of -25.0625. This is based on the value from a DS1631 of 0xE6F0. Instead, I'm getting a value of 24.9999. Debugging it seems to indicate that this value does not include the fractional part. The other problem is how I can get to print a negative without resorting to concatenating the value with a negative symbol.
I can't also get the fractional part to be output.
I've used a constant value of 0xE6F0 for demonstration. Also, the code has been expanded just to make it clear to me whilst debugging so I can see what happens to each register when stepping through.
What have I done wrong?
#include <16f877.h>
#device ICD=TRUE
#fuses HS, NOLVP, NOWDT, PUT
#use delay(clock=20000000)
#use rs232(DEBUGGER)
signed long test_temp()
{
int datal, datah, x, y;
signed long data = 0;
float z = 0.00;
datah=0xE6;
datal=0xF0;
//process the two's complement...it's a negative
data = datah;
data = (data<<8) | datal;
data = ~data;
data += 1;
//get the whole portion
x = data>>12;
x *= 16;
y = data>>8 & 0b00001111;
//get the fractional part
z = (float)(data>>4 & 0b000000001111);
z /= 16.;
//get the result
data = x + y + z;
return(data);
}
main()
{
signed long value;
do{
delay_ms(1000);
value = test_temp();
printf("%f\r\n", (float)value);
}while(1);
} |
|
|
Charlie U
Joined: 09 Sep 2003 Posts: 183 Location: Somewhere under water in the Great Lakes
|
|
Posted: Tue Feb 03, 2004 7:46 am |
|
|
By the time you are done with your function, you are returning a signed long int. This does not have a fractional part.
I haven't had a chance to verify this, but my first thought is that you loose your sign when you shift a signed number. As a test of the printf function, couldn't you first divide your signed long data by 16, which should preserve the sign:
data = data/16;
then cast the result to a float and multiply by 0.0625 and print the result?
return ((float)data*0.0625);
This would require returning a float from your function, but it would allow you to verify the printf question. |
|
|
|
|
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
|