Sending floats across SPI

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

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 

0 Likes
5 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Anonymous
Not applicable

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?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
lock attach
Attachments are accessible only for community members.
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Evan D,

   

I suspect it related to "normalized representation" of the floating numbers (from the paper attached: "The floating-point number 1.00 × 10^-1 is normalized, while 0.01 × 10^1 is not."). Try to change floating point format.

0 Likes
Anonymous
Not applicable

Ahh I didn't realise that. Thanks!

0 Likes