I am tring to understand and learn I2C better and in doing so have come accross the following question.
I am using part of the I2C example code on the Arduino Learning website for the WIre Library SFR_Ranger reader.
My particular I2C device has a 14-bit reolution and the data is stored in Registers 254 and 255. In 255 the bits 6 and 7 are not used, hence the 14 bits.
Question 1)
I am confused about why I read register 255 in the code with 2 bytes, I would have thought it should read register 254 first but that just gives garbage.
Question 2)
In the statement reading = reading << 6; // shift high byte to be high 8 bits
I read the correct 14 bit data with range of 0 - 16383.
If I change the 6 to an 8 as in original code, which i understand is 16 bit, the output range is 0 - 65535. While I assume this is just the 14 bit data interpolated into the 16 bit range so resolution would not be any greater, My question is how does it do this? It is now reading 8 bits from each register but 2 of the bits are unused in the first register.
The answer is probably staring me in the face but i dont get it.
here is the working code.
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial communication at 9600bps
}
unsigned int reading = 0;
void loop()
{
Wire.beginTransmission(66); // transmit to device #112
Wire.write(byte(255)); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting
// step 4: request reading from sensor
Wire.requestFrom(66, 2); // request 2 bytes from slave device #112
// step 5: receive reading from sensor
if(2 <= Wire.available()) // if two bytes were received
{
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 6; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
Serial.println(reading); // print the reading
}
delay(300); // wait a bit since people have to read the output :)
}