I'm trying to read data from a ITG-3200 Gyro. It's configured such that it uses 0xD0 for write and 0xD1 for read. The code that I'm trying to get to read something from it with is as follows:
#include <Wire.h>
#define DEVICE (0xD1) //device address
#define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
char str[512]; //string buffer to transform data before sending it to the serial port
void writeTo(int device, byte address, byte val)
{
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); //end transmission
} // end of writeTo
void readFrom(int device, byte address, int num, byte buff[])
{
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); //sends address to read from
Wire.endTransmission(); //end transmission
Wire.requestFrom(device, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) //device may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
} // end of readFrom
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
int regAddress = 0x27;
int x, y, z;
readFrom(DEVICE, regAddress, TO_READ, buff);
x = (((int)buff[1]) << 8) | buff[0];
y = (((int)buff[3])<< 8) | buff[2];
z = (((int)buff[5]) << 8) | buff[4];
sprintf(str, "%d %d %d", x, y, z);
Serial.write(str);
Serial.write(byte(10));
delay(15);
} // end of loop
This code is the result of chopping and changing others, I must be missing something important, the only result i'm getting on my serial monitor is lines of 0 0 0
The data manual for the sensor is here: http://www.sparkfun.com/datasheets/Sensors/Gyro/PS-ITG-3200-00-01.4.pdf
Thanks in advance (and sorry I'm a bit of a noob :))