0
\$\begingroup\$

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)    
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ The operator | is not the OR operator? Reference: arduino.cc/en/Reference/BitwiseAnd \$\endgroup\$ – Tiedt Tech Dec 3 '14 at 13:21
  • \$\begingroup\$ That is correct. | = or, & = and, << = left shift, >> = right shift \$\endgroup\$ – Majenko Dec 3 '14 at 13:32
  • \$\begingroup\$ Therefore do not understand the code. Because it reads the first value and moves 8 bits and then does the OR. Trying to find out the reason for this. \$\endgroup\$ – Tiedt Tech Dec 3 '14 at 13:37
  • 1
    \$\begingroup\$ But I have told you what it does. What don't you understand about it? \$\endgroup\$ – Majenko Dec 3 '14 at 13:44
  • 1
    \$\begingroup\$ Because that is how you join two 8 bit values into a 16 bit value. \$\endgroup\$ – Majenko Dec 3 '14 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.