MMA4851 Library? What is this code doing? SOLVED

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
r_o_m
 
Posts: 2
Joined: Sun Apr 10, 2016 11:38 am

MMA4851 Library? What is this code doing? SOLVED

Post by r_o_m »

Hello,

So I am usually good at understanding what code is doing but this particular snippet of code has me stumped. I understand the initial function call but not the purpose of the three statements after the Wire.read() statement. Can someone explain what this is doing?

file: Adafruit_MMA8451.cpp
in function read():

x = Wire.read(); x <<= 8; x |= Wire.read(); x >>= 2;
y = Wire.read(); y <<= 8; y |= Wire.read(); y >>= 2;
z = Wire.read(); z <<= 8; z |= Wire.read(); z >>= 2;

I'm hoping to determine the true maximum frequency at which I can read from this board (when taking into account the processing delays, communication delays, and sensor ADC delays) so I need to understand what code is necessary and what code can be removed or optimized.

Thanks!

Rebecca
Last edited by r_o_m on Mon Apr 11, 2016 9:48 pm, edited 1 time in total.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: MMA4851 Library? What is this code doing?

Post by adafruit_support_mike »

They're reading 14-bit integers into memory.

It's a little easier to understand if you unpack the code:

Code: Select all

    x = Wire.read();
    x <<= 8; 
    x |= Wire.read(); 
    x >>= 2;
The first line reads 8 bits from the I2C bus and stores them in the lowest 8 bits of the variable 'x'.

The second line moves those bits 8 places to the left, putting them in the variable's upper 8 bits.

The next line reads another 8 bits from the I2C bus, then copies them onto the lowest 8 bits of variable 'x' without disturbing the upper 8 bits.

The last line shifts all the bits in 'x' two places to the right.

Code: Select all

sensor registers:   [aaaa bbbb]
                    [cccc dd--]

//  the 'a', 'b', 'c', and 'd' bits are data
//  the '-' bits are unused

x = 0000 0000 0000 0000

//  x = Wire.read();
//  first I2C read = [aaaa bbbb]
//  expand to 16 bits: 0000 0000 aaaa bbbb
x = 0000 0000 aaaa bbbb

// x <<= 8
x = aaaa bbbb 0000 0000

//  x |= Wire.read();
//  second I2C read = [cccc dd--]
//  expand to 16 bits: 0000 0000 cccc dd--

    aaaa bbbb 0000 0000 
  | 0000 0000 cccc dd--
    -------------------
x = aaaa bbbb cccc dd--

//  x >>= 2;
x = 00aa aabb bbcc ccdd


User avatar
r_o_m
 
Posts: 2
Joined: Sun Apr 10, 2016 11:38 am

Re: MMA4851 Library? What is this code doing?

Post by r_o_m »

Wow, thank you so much. That is very helpful!! I didn't know the <<=, |=, >>= operators but they now make sense. Appreciate it!

Rebecca

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: MMA4851 Library? What is this code doing? SOLVED

Post by adafruit_support_mike »

Yep, they're called 'in place' operators. C has them for all the basic math and boolean logic operations.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Products from Adafruit”