View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
How to convert a float in two int8 |
Posted: Thu Sep 10, 2009 11:45 am |
|
|
Hi, I have a result of reading a sensor expressed in float with 2 decimal (TEM), I want to convert this number float in two int8 to TX to a PC (buffer_tx[0] and buffer_tx[1]), how I can do it?
Code: | int buffer_tx[2];
printf("%f\r\n",TEMP); // print 27.50
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 10, 2009 12:21 pm |
|
|
Look at the floor() function in the CCS manual. It shows an example
of how to get the fractional part of the floating point number.
Then with some math and the atoi() function, you can convert each part
into an 8-bit integer value. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Sep 10, 2009 2:37 pm |
|
|
Maybe you are looking for the make8() function? It is well documented in the manual once you know what it is called. A float is not going to fit in 2 bytes without some work. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Sep 10, 2009 3:19 pm |
|
|
pilar wants to take a floating point number in this format xx.yy, and
convert the xx and the yy into 8-bit integers, and transmit them as two
bytes. I didn't write a routine, I just pointed out the floor() function,
because I think pilar should try to do it first. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Thu Sep 10, 2009 9:39 pm |
|
|
Hi PCM programmer, thank you, I did not know this function. |
|
|
|