- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I have used some sample code to send a float across an SPI connection. To reconstruct the float from a buffer I have used a union.
union{
float floatx;
int8 b[4];
} x;
x.b[0] = rxBuffer[4];
x.b[1] = rxBuffer[3];
x.b[2] = rxBuffer[2];
x.b[3] = rxBuffer[1];
but when I compare the value sent to the value of x (x.floatx == -9.9999) they aren't the same even thought the bytes printed on the LCD look correct (in hex). Have I done this correctly?
Thanks
- Labels:
-
PSoC 5LP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Array indices in C-language start at 0. rxBuffer[4] does not exist.
Sender and receiver machines may have different endianess. This might cause differences.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Opps I realise the index was wrong in the code, my bad but the endianess I didn't. I thought if I specified MSB for both the arduino and the SPI slave (shift direction MSB) they would both be little endian?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You cannot influence endianess, but when both systems run on ARM it will probably the same.
When all fails, consider converting the floats to ASCII using sprintf(), send them and convert them back. Increase the heap size to at least 0x0200 when using sprintf()
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ahh I didn't realise that. Thanks!