CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Convert float value into int,,,,

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
CCS ADDiT
Guest







Convert float value into int,,,,
PostPosted: Sun Mar 05, 2006 8:48 am     Reply with quote

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????


Smile
Guest








PostPosted: Sun Mar 05, 2006 9:09 am     Reply with quote

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








PostPosted: Sun Mar 05, 2006 9:59 am     Reply with quote

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








PostPosted: Sun Mar 05, 2006 10:00 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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