Receiving Data over i2c

Hello,

I am trying to get data from a sensor to an Uno over i2c. The device’s address is 56, and we need data from register 0x00 and 0x01. The i2c scanner code picks up the device at address 56, but the only data I’m getting on the serial monitor is alternating 0 and -1.

Code:

#include <Wire.h>
int p;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  delay(10);
}

void loop()
{
  Wire.beginTransmission(56);
  Wire.requestFrom(56,8);
  while(Wire.available() >0)
  {
    p =  Wire.read();
    Wire.endTransmission();
  }
  Serial.println(p);
}

and here is some sample code from the sensor manufacturer (written in C)

Void main_loop(unsigned char Sensitivity_Mode)
{
unsigned char Data1, Data2;
short value; // 10-bit output value (0~1023)
// The value 511 is the middle point @ 0mT
float magnetic_field; // Value of the magnetic field in mT
Data_LSB = I2C_Read8(I2C_ADDR, 0x00); // Read D7..0
Data_MSB = I2C_Read8(I2C_ADDR, 0x01); // Read D9..8 + OCF + Parity
value = ((Data_MSB & 0x03)<<8) + Data_LSB;

and links to the sensor & breakout board spec sheets:
http://www.ams.com/eng/content/download/207875/885647/118685
http://www.ams.com/eng/content/download/62568/562619/41334

Usually you would send a command:

 Wire.beginTransmission(56);
 Wire.write(command);  //  Command to read register
 Wire.endTransmission();

and then read the data returned:

  Wire.requestFrom(56,2);
  byte highByte = Wire.read();
  byte lowByte = Wire.read();
  int value = ((highByte&0x03) << 8) | lowByte;

while(Wire.available() >0) { p = Wire.read(); Wire.endTransmission(); } Serial.println(p);

This bit of code is rubbish. If there is more than one piece of data available, you will read each piece into p ( overwriting what is already in p ), and then you will only print the last piece.