I have I2C compass sensor, which I can't get to work properly.
Compass automatically sends data, when you connect to it, which means that I just have to send something to it and then I retreive 10-bit number (split into 2 bytes).
Here is the code I have:
- Code: Select all
I2CPort i2cport = (I2CPort) SensorPort.S3;
byte[] input = new byte[2];
i2cport.i2cTransaction(0x42, new byte[] {0x00}, 0, 0, input, 0, 2);
System.out.println(new DataInputStream(new ByteArrayInputStream(input)).readUnsignedShort());
However, value seems to be a bit jumpy. I printed some more info on screen and figured out that second byte won't go over 127. It will go to 127 and then skip to 0. But it also won't return any negative numbers, so signed/unsigned is not a problem. It's like first bit is completely ignored. Here is RobotC code that works without problems on same sensor:
- Code: Select all
ubyte received[2];
byte message[2] =
{
1,
0x42
};
while(nI2CStatus[I2Cport] == STAT_COMM_PENDING){}
sendI2CMsg(I2Cport, message[0], 2);
while(nI2CStatus[I2Cport] == STAT_COMM_PENDING){}
readI2CReply(I2Cport, received[0], 2);
int result = 256*message[0] + message[1]; //Here is the proper result that is not jumpy
Any idea what could be wrong?
