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

조회 수: 3 (최근 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에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by