You are here

Sending floats across SPI | Cypress Semiconductor

Sending floats across SPI

Summary: 5 Replies, Latest post by Evan D on 29 Sep 2016 12:26 AM PDT
Verified Answers: 3
Last post
Log in to post new comments.
Evan D's picture
User
24 posts

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 

user_1377889's picture
User
10112 posts

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

Evan D's picture
User
24 posts

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?

user_1377889's picture
User
10112 posts

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

user_342122993's picture
User
675 posts

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.

Evan D's picture
User
24 posts

Ahh I didn't realise that. Thanks!

Log in to post new comments.