How do I read int16 data using the Arduino I2C Support Package?

6 views (last 30 days)
How do I read int16 data using the Arduino I2C Support Package?
Output data from the accelerometer I'm using is 16-bit Two's Complement according to the data sheet (attached). When I set the data size to N=6 (6 bytes of data from registers 0x32 - 0x37), I see 5 lines rather than 3, 1 for each axis. Also, only 1 line actually seems to be showing acceleration data. If I up the data size to 8, there are 3 lines that appear to be showing acceleration data along with extra lines.
So it seems there is an issue with packaging the data from the accelerometer in two's complement format into a format that would be readable on my end. How do I figure this out?
The I2C block

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 28 Mar 2019
If the data is twos complement, it means that it is signed data. Since the I2C Read block only supports "int8" and "uint16" data types, you will just want to read the bytes using the I2C Read block and then use the Byte Pack and Byte Unpack blocks to reformat it into the "int16" data type. The attached model provides an example of how to do this.

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!