Converting 16 bit signed value to 32 bit floating point value
I'm reading a 16 bit value on an accelerometer (two 8-bit registers) and trying to convert the 16 bit value into a 32 bit floating point value. This is what my routine currently looks like:
PUB Read_Word(): x
x := spi.read(1, 8) << 8
x |= spi.read(1, 8)
x := x << SIGNX 7
return x
If I display the returned variable using DEBUG(SDEC(x)), it is human readable, both positive and negative accelerations. However, if I display the variable using DEBUG(FDEC(x)), the values are incorrect and I get a NAN (not a number) whenever the X variable is negative. I am assuming this is because my Read_Word() routine returns a binary signed 16 bit value instead of a binary signed 32 bit value. Can somebody please help me with updating the routine to return a binary signed 32 bit value? Thanks. I also attached the code below.
Comments
Your routine is not converting to a float -- there is a Spin2 keyword (float()) that will convert an integer to a float.
Is this the line you think is converting your reading to a float?
This gives me a syntax error in Propeller Tool and Spin Tools. If you're wanting to convert a signed 8-bit integer to a float try this:
Seems odd you'd have to retrive two bytes. If it is in fact a signed 16-bit integer, change the last line to
Except for the newly added pointer mechanism, Spin doesn't do data type definitions of the storage. It instead relies on the operators and functions alone to match what the data types are.