|
|
View previous topic :: View next topic |
Author |
Message |
sachin
Joined: 02 Oct 2004 Posts: 1 Location: pune
|
floatin to byte using union |
Posted: Sat Oct 02, 2004 6:32 am |
|
|
Hi,
I am doing serial communication between VC++ code and CCS code
using USART.
for float to byte conversion have used union.
union
{
unsigned char tmp_char[4];
float tmp_float;
} un_tmp_float;
VC++ code send value 12.5000 as 0,0,72,65
but for same value ccs code requires 0,0,72,130
where could be proble, and how to overcom?
thnaks,
sachin |
|
|
Ttelmah Guest
|
Re: floatin to byte using union |
Posted: Sat Oct 02, 2004 8:18 am |
|
|
sachin wrote: | Hi,
I am doing serial communication between VC++ code and CCS code
using USART.
for float to byte conversion have used union.
union
{
unsigned char tmp_char[4];
float tmp_float;
} un_tmp_float;
VC++ code send value 12.5000 as 0,0,72,65
but for same value ccs code requires 0,0,72,130
where could be proble, and how to overcom?
thnaks,
sachin |
The problem is that the float format is not the same in VC, as in CCS. Unfortunately, the sign bit is stored in a different location on the two formats, which then displaces the exponent. There have been routines posted here in the past, to move the required bits around. It is not hard to do in C, with a mask, and a shift. On the CCS format, the sign bit sits as the top bit of the third byte (in the order you show them), while in VC, the sign sits as the top bit of the last byte. On CCS, the 8bit exponent sits entirely in the last byte, while in VC, the top seven bits of this sit in the last byte, and then the bottom bit occupies the tp bit of the third byte. The VC format, is the IEEE tandard layout, but the CCS format, would be slightly quicker to code for, removing the need to extract the exponent from two bytes. I'd suspect this is why CCS went this way...
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
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
|