在 2014年1月24日星期五UTC-5下午5时55分44秒,Ping li写道:
>
> Hi Ytai
>
> I am using IOIO V2 to read MPU9150, a 9DOF motion sensor: 
> https://www.sparkfun.com/products/11486
>
>  The problem I encountered is that I can not read out the register by 
> I2C.  I modified the code base on https://github.com/twyatt/ioio-adxl345, 
> from SPI to I2C, it works for ADXL345
>
>
>
> I list down the code:
>
> ////////////////////////////////////code from 
> here////////////////////////////////////////////////
>
> *public* *byte* getDeviceId() *throws* ConnectionLostException, 
> InterruptedException {
>
> read(*WHO_AM_I*, 1, readBuffer);
>
> *return* readBuffer[0];
>
> }
>
> *private* *void* setupDevice() *throws* InterruptedException, 
> ConnectionLostException {
>
> write(*PWR_MGMT_1*, (*byte*) 0x80); // reset
>
> Thread.*sleep*(*REGISTER_WRITE_DELAY*);
>
> write(*PWR_MGMT_1*, (*byte*) 0x00);
>
> Thread.*sleep*(*REGISTER_WRITE_DELAY*);
>
> read(*WHO_AM_I*, 1, readBuffer);
>
> System.*out*.println("device ID is "+readBuffer[0]);
>
> *byte* id = readBuffer[0];
>
> Thread.*sleep*(*REGISTER_WRITE_DELAY*);
>
> /*
>
> write(SMPLRT_DIV, (byte) 0x00);
>
> Thread.sleep(REGISTER_WRITE_DELAY);
>
> read(SMPLRT_DIV, 1, readBuffer);
>
> Thread.sleep(REGISTER_WRITE_DELAY);
>
> System.out.println("The first SMPLRT_DIV is "+readBuffer[0]);
>
> */
>
>  write(*SMPLRT_DIV*, (*byte*) 0x07); // *Gyro* sample rate, 0x07=125Hz
>
> Thread.*sleep*(*REGISTER_WRITE_DELAY*);
>
> read(*SMPLRT_DIV*, 1, readBuffer);
>
> Thread.*sleep*(*REGISTER_WRITE_DELAY*);
>
> System.*out*.println("The second SMPLRT_DIV is "+readBuffer[0]);
>
> write(*CONFIG*, (*byte*) 0x06); //0x06=5Hz
>
> Thread.*sleep*(*REGISTER_WRITE_DELAY*);
>
> write(*GYRO_CONFIG*, (*byte*) 0x18); //2000degree/s
>
> Thread.*sleep*(*REGISTER_WRITE_DELAY*);
>
> write(*ACCEL_CONFIG*, (*byte*) 0x01); //  2G, 5Hz
>
> Thread.*sleep*(*REGISTER_WRITE_DELAY*);
>
> *if* (id == *WHO_AM_I_default*) {
>
> deviceId = id;
>
> System.*out*.println("afjskldfjklasdjfklasfjaklsdfjsdklf The id is "+id);
>
> } *else* {
>
> onError("Invalid device ID, expected " + (*WHO_AM_I_default* & 0xFF) + " 
> but got " + (id & 0xFF));
>
> System.*out*.println("afjskldfjklasdjfklasfjaklsdfjsdklf The id is wrong "
> +id);
>
> }
>
> *if* (listener != *null*) {
>
> listener.onDeviceId(deviceId);
>
> }
>
> //setRange(2); // +/- 16 G
>
>
> }
>
> *protected* *void* write(*byte* register, *byte* value) 
> *throws*ConnectionLostException, InterruptedException {
>
> writeBuffer[0] = register;
>
> writeBuffer[1] = value;
>
> flush(2);
>
> }
>
> *protected* *void* write(*byte* register, *byte*[] values) 
> *throws*ConnectionLostException, InterruptedException {
>
> writeBuffer[0] = register;
>
> System.*arraycopy*(values, 0, writeBuffer, 1, values.length);
>
> flush(1 + values.length);
>
> }
>
> /**
>
> * Writes the write buffer to the I2C.
>
> * 
>
> * *@param* length Number of bytes of the buffer to write.
>
> * *@throws* ConnectionLostException
>
> * *@throws* InterruptedException
>
> */
>
> *protected* *void* flush(*int* length) *throws* ConnectionLostException, 
> InterruptedException {
>
> *boolean* tenBitAddr = *false*;
>
> *int* readSize = 0;
>
> i2c.writeRead(*ADDRESS_AD0_LOW*, tenBitAddr, writeBuffer, length, 
> readBuffer, readSize);
>
> *if* (*REGISTER_WRITE_DELAY* > 0)
>
> Thread.*sleep*(*REGISTER_WRITE_DELAY*);
>
> }
>
> *protected* *void* read(*byte* register, *int* length, *byte*[] values) 
> *throws* ConnectionLostException, InterruptedException {
>
> *boolean* tenBitAddr = *false*;
>
> writeBuffer[0] = register;
>
> i2c.writeRead(*ADDRESS_AD0_LOW*, tenBitAddr, writeBuffer, 1, readBuffer, 
> length);
>
> }
>
> *private* *void* onError(String message) {
>
> *if* (listener != *null*) {
>
> listener.onError(message);
>
> }
>
> }
>
> /*
>
> * IOIOLooper interface methods.
>
> */
>
> @Override
>
> *public* *void* setup(IOIO ioio) *throws* ConnectionLostException, 
> InterruptedException {
>
> i2c = ioio.openTwiMaster(twiNum, rate, *false* /* *smbus* */);
>
> setupDevice();
>
> }
>
> @Override
>
> *public* *void* loop() *throws* ConnectionLostException, 
> InterruptedException {
>
> *if* (listener != *null*) {
>
> read(*GYRO_XOUT_H*, 6, readBuffer);
>
> x = (readBuffer[0] << 8) | readBuffer[1];
>
> y = (readBuffer[2] << 8) | readBuffer[3];
>
> z = (readBuffer[4] << 8) | readBuffer[5];
>
> listener.onData(x, y, z);
>
> }
>
> }
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to