MPU-6050 doubt in playgroud code Arduino.cc

arduinogyro

I am studying the MPU-6050 and saw this article. I'm wondering in two lines of code.

http://playground.arduino.cc/Main/MPU-6050

This line from what I understand the shield guard reading the last value in this register and the source code is asking these values. That's right?

Wire.requestFrom(MPU,14,true);

This code I understood that he reads the values, but because moving bits to the left?

AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    

Best Answer

The line

Wire.requestFrom(MPU,14,true);

initiates a read transfer using I2C. The read consists of two 8 bit values, which are then joined together into a single 16 bit value:

AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    

That basically means "Read one byte, shift it left 8 bits, read a second byte, and OR the two values together".

So if the first byte is (in binary) 0b00100100 and the second is 0b10101010 then the result would be 0b0010010010101010.

In hexadecimal that would be taking 0x24, shifting it left to 0x2400, then or-ing 0xAA with it, to form 0x24AA.

In decimal it would be taking the number 36, shifting it left 8 bits to make it 9216, then or-ing 170 to make 9386.