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
MMA4851 Library? What is this code doing? SOLVED
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- r_o_m
- Posts: 2
- Joined: Sun Apr 10, 2016 11:38 am
MMA4851 Library? What is this code doing? SOLVED
Last edited by r_o_m on Mon Apr 11, 2016 9:48 pm, edited 1 time in total.
- adafruit_support_mike
- Posts: 67446
- Joined: Thu Feb 11, 2010 2:51 pm
Re: MMA4851 Library? What is this code doing?
They're reading 14-bit integers into memory.
It's a little easier to understand if you unpack the code:
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.
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 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
- r_o_m
- Posts: 2
- Joined: Sun Apr 10, 2016 11:38 am
Re: MMA4851 Library? What is this code doing?
Wow, thank you so much. That is very helpful!! I didn't know the <<=, |=, >>= operators but they now make sense. Appreciate it!
Rebecca
Rebecca
- adafruit_support_mike
- Posts: 67446
- Joined: Thu Feb 11, 2010 2:51 pm
Re: MMA4851 Library? What is this code doing? SOLVED
Yep, they're called 'in place' operators. C has them for all the basic math and boolean logic operations.
Please be positive and constructive with your questions and comments.