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
it is quite simple:(100 << 8) + 214same on var:
data = i2c.readfrom(addr,2) value = (data[0] << 8) + data[1] -
That works!
What is the "<<8" notation called so I can read up more on how to use it?
Thanks for the rapid reply...
-
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login