14 bit 2s complement
CannibalRobotics
Posts: 535
On the Freescale Xtrinsic FXOS8700CQ the accelerometer data is presented in the form of a 14 bit 2s complement. Following the "invert and + 1" technique they recommend I can not get SPIN to clear off the bits above the 13th on a negitive number. My feeling is that it's something obvious Im just not picking up.
Some fresh eyes on this would be great.
For Negitive numbers I get:
Why is the routine returning all 1's above the 13th bit after I've ANDed them all off with
Thanks in advance.
Some fresh eyes on this would be great.
Pri FixA(d)| bits '' Returns Signed Long of G force on axis for data d bits := d & $0000_3FFF ' Reduce to 14 bits to work with if bits > $0000_1FFF ' Is sign bit (14th) set ? bits &= $0000_0000_1FFF ' Clear sign off base number bits :=(! bits) + 1 ' Invert the remainder and add 1 bits := bits & $0000_0000_0000_1FFF ' Clear off upper ones from ! operation bits := -1 * bits ' Make it negitive (set sign bit) Return bits
For Negitive numbers I get:
AxC Raw: 00000000000000000011101110111111 14 bits: 00000000000000000011111111111111 FixA out: 11111111111111111111101110111111 = -1089For Positive I get:
AxC Raw: 00000000000000000000010001110100 14 bits: 00000000000000000011111111111111 FixA out: 00000000000000000000010001110100 = 1140
Why is the routine returning all 1's above the 13th bit after I've ANDed them all off with
bits := bits & $0000_0000_0000_1FFF ?
Thanks in advance.
Comments
The << gets the sign bit into bit 31, and the ~> does a signed right shift back, preserving the sign bit.
-Phil
Then you should end up with something ranging from 0 to 37267.
-Phil
So shift left 18 bits, then right 18 bits, to get rid of 'stray' bits on the end, then add 16384 to get rid of the sign.
- edit: Just noticed Phil's response above. I'll shut up.