i2c Return Data Conversion



  • I have been successful in bringing back data from an i2c analog input device.

    Data comes back as two bytes. To convert the data to engineering units, I need to combine the two bytes to a 16 bit word and then convert to decimal. Example data coming back for the two bytes in the return array from a i2c.readfrom(addr,2) instruction:

    Decimal: 100 214
    Hex: b'd\xd6'

    This is 0110010011010110 in combined 16 bit form which then can be converted to 25814 decimal. This decimal number is proportional to the incoming signal and can be used to convert to engineering units.

    Have spun my wheels for awhile trying to figure out how to do combine [0] and [1] and convert to decimal in code. Throwing myself on the collective braintrust of the forum for assistance.

    Thanks!



  • @dchappel
    bit left shift

    <<
    

    same for bit right shift

    >>
    


  • @livius

    That works!

    What is the "<<8" notation called so I can read up more on how to use it?

    Thanks for the rapid reply...



  • @dchappel
    it is quite simple:

    (100 << 8) + 214
    

    same on var:

    data = i2c.readfrom(addr,2)
    value = (data[0] << 8) + data[1]
    


Pycom on Twitter