Difficulty with i2c Read Register. Not enough information in the doc

5 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2017-1-24
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 个评论
Fawwaz Hosein
Fawwaz Hosein 2020-4-7
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
Walter Roberson 2020-4-7
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by