Spin Language Qeustion
hylee101001
Posts: 48
I'm working with mpu. From an I2C library, I have experienced an over-flow-looking-like numbers when I had this:
mpuRawData[0] := (I2C.read(mpuAdd, mpuXAccH)<< 8) | (I2C.read(mpuAdd, mpuXAccL) & $ff)
But I have added this second line (because I saw this from another I2C library)
~~mpuRawData[0]
And numbers are clean!
Q1. Does someone know what the second line do? Is it somehow related to 2's complement conversion?
Q2. What does "&$ff" do form the first line?( it looks somehow attaching a 0 at the end of line. though..)
mpuRawData[0] := (I2C.read(mpuAdd, mpuXAccH)<< 8) | (I2C.read(mpuAdd, mpuXAccL) & $ff)
But I have added this second line (because I saw this from another I2C library)
~~mpuRawData[0]
And numbers are clean!
Q1. Does someone know what the second line do? Is it somehow related to 2's complement conversion?
Q2. What does "&$ff" do form the first line?( it looks somehow attaching a 0 at the end of line. though..)
Comments
This takes the value returned from the call I2C.read(mpuAdd, mpuXAccH) and shifts it left 1 byte (8 bits)
(I2C.read(mpuAdd, mpuXAccL) & $ff)
This takes the low byte (8 bits) of the call I@C.read(mpuAdd, mpuXAccL) by ANDing the result with $FF.
Then the two results are ORed together.
That second line converts a 16-bit signed number into a signed long (the native type for Spin). See page 157 in the manual.
The & $FF is unnecessary because an I2C read returns 8 bits hence is already limited to 0..255