Shop OBEX P1 Docs P2 Docs Learn Events
Spin Language Qeustion — Parallax Forums

Spin Language Qeustion

hylee101001hylee101001 Posts: 48
edited 2015-03-07 10:47 in Propeller 1
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..)

Comments

  • Cluso99Cluso99 Posts: 18,069
    edited 2015-03-06 23:43
    (I2C.read(mpuAdd, mpuXAccH)<< 8)
    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.
  • JonnyMacJonnyMac Posts: 9,105
    edited 2015-03-07 10:47
    Does someone know what the second line do? Is it somehow related to 2's complement conversion?

    That second line converts a 16-bit signed number into a signed long (the native type for Spin). See page 157 in the manual.

    Q2. What does "&$ff" do form the first line?( it looks somehow attaching a 0 at the end of line. though..)

    The & $FF is unnecessary because an I2C read returns 8 bits hence is already limited to 0..255
Sign In or Register to comment.