|
|
View previous topic :: View next topic |
Author |
Message |
CCS ADDiT Guest
|
Convert float value into int,,,, |
Posted: Sun Mar 05, 2006 8:48 am |
|
|
I am using i2c MAX517(DAC) and i2C only accept 8 bits integer....
now I am trying to pass a float value...f=34.009
In fact..it is ok for to pass '34' as well....
how can I conver from float to int????
|
|
|
Guest
|
|
Posted: Sun Mar 05, 2006 9:09 am |
|
|
Code: |
float head,temp0;
int wrhead;
head=65.68;
temp0=(head*990)/255; // Rescale
printf("\n\rHead is %2.2f",head);
printf("\n\rTemp0 is %2.2f",temp0);
wrhead=(int)temp0;
printf("\n\r Wr head is %2.2d",wrhead);
|
the output is
Head is 65.67
Temp0 is 254.99
Wr head is -2
I was expecting wrhead to be 254...
How can I do that?? |
|
|
Guest
|
|
Posted: Sun Mar 05, 2006 9:59 am |
|
|
Code: | int wrhead;
head=65.68;
[color=green]temp0=(head*245)/127; // Rescale [/color]
printf("\n\rHead is %2.2f",head);
printf("\n\rTemp0 is %2.2f",temp0);
wrhead=(int)temp0;
printf("\n\r Wr head is %2.2d",wrhead);
|
the output is
Head is 65.67
Temp0 is 126.70
Wr head is 126 |
|
|
Guest
|
|
Posted: Sun Mar 05, 2006 10:00 am |
|
|
Well, it is -2 in two's complement. Try this:
printf("\n\r Wr head is %u",wrhead);
It should print it as an unsigned int. |
|
|
|
|
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
|