Difficulty with i2c Read Register. Not enough information in the doc
7 views (last 30 days)
Show older comments
Robert
on 24 Jan 2017
Commented: Walter Roberson
on 7 Apr 2020
So i am working with an I2C device and reading in 8 bit register data
The problem is the following. The doc does not allow for data to be read in a specific format.
The register is currently housing data in a 2,s complement format
When i read from the register its giving decimal answers.
This is a problem because i have no idea what conversion its doing in the background. It sure doesn't know that what its reading is 2s complement so I'm assuming the decimal number I'm getting back is wrong also.
How can i just read the register in pure binary?
There is nothing in the doc
0 Comments
Accepted Answer
Walter Roberson
on 24 Jan 2017
If you are using arduino and read() of an i2cdev object, then the transfer always binary. As is always the case, by default MATLAB displays binary numbers in decimal . You can use dec2bin() to display it as binary.
I suspect that want you want is to pass 'int8' to the read() routine.
8 Comments
Fawwaz Hosein
on 7 Apr 2020
Thank you Walter for such a lengthy response. However, consider the case when the register returns the 2's complement value as '00110000'. Now, reading this value as a signed value is correct. However even changing the type to unsigned then converting using dec2bin could still end up in error. Since with 2's comp we essentially are looking for the MSB bit (i.e. the sign bit). Referring to the previous 8 bit number, we would be unsure if the MSB is a 0 or a 1. Therefore performing dec2bin would automatically make the assumption that the MSB is 1 which would be wrong.
Walter Roberson
on 7 Apr 2020
Understanding the I2C Bus
Data is transferred Most Significant Bit (MSB) first.
As the bit order for I2C is fixed, there is no possibility that the bit order for any given device should be the other way around. This is not a negotiable property of the device.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!