Notes About Working with Various Arduino & Netduino Microcontroller Boards

Sunday, November 4, 2012

Converting a float to an Array of bytes

When passing a float value from the Arduino to the uM-FPU v 3.1 floating point co-processor, it is necessary to convert it to an array of bytes.

Each Arduino float is 32 bit (4 bytes).

Note that the co-processor expects the bytes in the opposite order compared to the Arduino.

Here is a sample code snippet showing how to do this. This code takes the specified float value and loads it into the co-processor's register 0:

float x = 0.004882814;
byte* b = (byte*) &x;
Fpu.write(FWRITE0, b[3], b[2], b[1], b[0]);

No comments:

Post a Comment