1

The datasheet for the accelerometer states that:

enter image description here

Here is the data output from the accelerometer:

enter image description here

I have tried to mask the first two bits using a bitwise AND command with B0011111111111111.

I tried to use this code, but unfortunately the first 2 bits are still "11" so the value has not changed:

int bresult=0b0011111111111111&result;
Serial.print(bresult,BIN);

How would I bits mask the first two bits and will arduino automatically know how to convert to twos compliment?

The variable result is of data type "long"

According to an answer, I tried:

int bresult= result<<2;
Serial.print(bresult,BIN);

I got this result which is great but is it possible to remove the last two zeros because this is making my value larger? The image below shows result then bresult.

enter image description here

  • 1
    That's odd, as 0b0011111111111111&result; works as expected on my Arduino. I just tested it as I didn't know about the 0b... notation (I only knew B...) – Gerben Jul 23 '14 at 17:14
2

Since it won't convert to two's complement if the sign bit is the 14 bit instead of the 16th, it would be easier to just shift the value left by 2. So

int bresult= result<<2;

Since <<2 is the same as *4 the result will be 4 times as high. So you need to divide the result by 4. Since you already have to multiply by 0.025 (or divide by 40), you end up having to multiply by 0.00625 (or divide by 160).

  • I tried to do this: int bresult= result<<2; Serial.print(bresult,BIN); However i still get 15 bits – Hamoudy Jul 23 '14 at 12:53
  • Here is a screenshot : i.imgur.com/Ck35p4V.png – Hamoudy Jul 23 '14 at 12:54
  • However, I am left with two zeros at the end of the value, making my value bigger. Is it possible to remove these two zeros? Here is a screenshot of result and bresult: prntscr.com/45j01c – Hamoudy Jul 23 '14 at 13:58
  • 1
    Please re-read my answer, as already told you that would happen. Two extra 0s at the end means the result is 4 times too big. – Gerben Jul 23 '14 at 16:52
0

For the arduino, long is 32 bits, int is 16 and char is 8.

Save the result in an int not a long. Then Gerben's method int bresult = (result << 2)/4 should work. This is because << 2 moves everything left two bits, but / 4 move every thing right two bits with new bytes the same as the sign bit. So you get a 16 bit signed variable with the correct number. If you have it as a long you would have to do int bresult = (result << 18)/(2^18).

BTW Serial.print(bresult,BIN); is always going to give you up to 16 bits. There is no 14 bit type.

  • However, I am left with two zeros at the end of the value, making my value bigger. Is it possible to remove these two zeros? Here is a screenshot of result and bresult: prntscr.com/45j01c – Hamoudy Jul 23 '14 at 13:57
  • As geo said, printing a result as binary will always show 16 bits. You will ALWAYS see 16 binary digits, no matter what. What are you trying to do with the output? If you need 14 bits of 1 and zero characters written to the serial output, you might need to write a custom function for that. It would be trivial to do. – Duncan C Jul 23 '14 at 15:25
  • @Hamoudy in the screen shot you haven't done the /4 yet. – geometrikal Jul 24 '14 at 1:01

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.