ps.twi_ = ioio_.openTwiMaster(0, TwiMaster.Rate.RATE_400KHz, false);
in my IOIO loop:
Boolean success;
byte [] acc = new byte [6];
success = ps.twi_.writeRead(0x1E, false, new byte[]{0x3C,0x02,0x01}, 2, acc,0); // single measurement mode
success = ps.twi_.writeRead(0x1E, false, new byte[]{0x3C,0x03}, 2, acc, 0); //register pointer to 03
success = ps.twi_.writeRead(0x1E, false, new byte[]{0x3D,0x06}, 2, acc, 6); // read 6 registers
if (success) {
int dataX = (acc[0]) + ((acc[1]) << 8);
int dataY = (acc[2]) + ((acc[3]) << 8);
int dataZ = (acc[4]) + ((acc[5]) << 8);
DisplayInfo(260, null, null, "X:" + String.valueOf(dataX) + " Y:" + String.valueOf(dataY) + " Z:" + String.valueOf(dataZ));
}
This info taken from the datasheet: http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Magneto/HMC5883L-FDS.pdf
The problem is that data registers acc always have 6 zeros (0). So no data is never read.
What I am doing wrong here, have tried to find a working example using IOIO, but all seems to be with Arduino.
I have tried to use continous measure and single read mode, with no success. writeRead always returns success=true, but no data.
Can anybody help me, thanks !
Is there anywhere documentation, what the parameters in writeRead really mean ? Especially the lengths, is the first number of bytes to write, and the last length:
number of bytes will be read
Hello,
Tryout the code changes below.
...
ps.twi_.writeRead(0x1E, false, new byte[]{0x3C,0x02,0x01}, 2, acc, 6); // Remove this line (it's set by default)
ps.twi_.writeRead(0x1E, false, new byte[]{0x3C,0x03}, 2, acc, 6); // Use this line
ps.twi_.writeRead(0x1E, false, new byte[]{0x3D,0x06}, 2, acc, 6); // Delete this line
...
As I understand the IOIO's implementation of I2C, one writeRead() is sufficient to perform a read, and a write operation to a single slave device on the I2C bus. By sending the above, you are requesting to read from register 0x03. Simply specifying a byte[] and a length is sufficient for writeRead() to automatically wait for a response from the slave and put the reply into the byte[] (acc).
Your first writeRead() is unnesessary because single read mode is set by default on the HMC5883L.
Your second writeRead() is about right. Modify as above.
Your third writeRead() attempt looks close, but based on what I read, that tells the slave that you would like to write to register 0x06.
Hopefully that all makes sense,
Tyler
success = ps.twi_.writeRead(0x1E, false, new byte[]{ 0x3C, 0x0A}, 2, acc,3); //trying to read deviceId from register 10
and get 3 zeros. I would expect some nonzero value.
If I initialize acc byte array with something else before calling writeRead the IOIO writeRead reads zeros.
Could it be that the sensor is damaged ?
If I unplug the power 3.3v from the sensor, I get zeros. The same thing if I remove the pull-au resistors.
But if I unplug SDA or SCL, writeRead returns false (otherwise true).
How could I verify that sensor works ? Can I "factory reset" it ?
I went back and re-read the data sheet more thoroughly, and it appears I was wrong. I just found the example on page 18 that details how to take a single measurement. Looking at your first post, I see that's where your code comes from. I now think you did everything right, except for waiting 6 ms for the data to be placed into the data registers.
So, I would revert back to your first code attempt, but place the line:
Thread.sleep(6);
after writeRead() command #2, to have the IOIO wait 6 ms for the data to be available. Then you should be able to properly read the data.
It can't hurt to write to the configuration registers before the measurement (as in the example), so I would recommend that as well. Sorry to lead you down the wrong trail.
Tyler
Did you remember pull up resistors on the bus?
--
You received this message because you are subscribed to the Google Groups "ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ioio-users+...@googlegroups.com.
To post to this group, send email to ioio-...@googlegroups.com.
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.
TwiMaster.Rate.RATE_100KHz
when opening twi-line, but the same result