MAX6965 / MMA7456L 2's complement - Word to Int

Hello again,

So I'm having a problem converting between binary 8 or 10 bit data and applying the 2's complement to get out meaningful accelerometer data.

Looking at my 2's complement function and the way I'm casting to int from a word is there anything obviously wrong? I've been struggling with this for a while. The 8bit data is reasonable, but the 10 bit is garbage of about 1/5th the amplitude I'm expecting.

//  Two's Complement function, shifts 8 bit binary to signed integers (-127 to 127)
int SignedNumber8bit(byte twoscomp){
  //Let's see if the byte is negative
  if (twoscomp & B10000000){
    //Invert
    twoscomp = ~twoscomp + 1;
    //Cast as int and multiply by negative one
    int value = int(twoscomp)*(-1);
    return value;
  }
  else{
    //Byte is non-negative, therefore convert to decimal and display
    //Make sure we are never over 1279
    twoscomp = (twoscomp & B01111111);
    //Cast as int and return
    int value = int(twoscomp);
    return value;
  }
}

//  Two's Complement function, shifts 10 bit binary to signed integers (-512 to 512)
int SignedNumber10bit(word twoscomp){
  //Let's see if the byte is negative
  if (twoscomp & 0x200){
    //Invert
    twoscomp = ~twoscomp + 1;
    twoscomp = (twoscomp & 0x3FF);
    //Cast as int and multiply by negative one
    int value = int(twoscomp)*(-1);
    return value;
  }
  else{
    //Serial.println("We entered the positive loop");
    //Byte is non-negative, therefore convert to decimal and display
    twoscomp = (twoscomp & 0x3FF);
    //Cast as int and return
    //Serial.println(twoscomp);
    int value = int(twoscomp);
    return value;
  }
}

Here's the full code if you really want to dig, all it's doing now is blinking some LED's and delaying based on the acceleration value in the x direction.

http://chrismeyer.org/temporary/Accelerometer_Lights.pde

I think you may be making it more complicated than it needs to be. "Two's complement" is usually just the way the data is interpreted. If you have two bytes of data in a word variable named twoscomp you should be able to cast it to a short (or int) to get the data interpreted as two's complement...

word twoscomp;

twoscomp = ReadDataFromMMA7456L();

Serial.print( (short) twoscomp, DEC );

It should work the same for byte data with a cast to char...

byte twoscomp;

twoscomp = ReadDataFromMMA7456L();

Serial.print( (char) twoscomp, DEC );

If casting works, Merry Christmas! If it doesn't work, a doppelganger has posted this!

Hi, I'm trying to use a freescale MMA7455L accelerometer.
I'm totally new in this type of hardware connections.

I used the code indicated in this post, but what I read are a lot of zeros...
Could you please show me how to physically connect the wires between the accelerometer and the arduino board?
Checking the code, I do not understand what pins are conected :frowning:

Thanks a lot!

Hello,

I made quite a few changes to that code after I uploaded it last. If you click that link again you'll get a new file that should work a lot better.

Unfortunately I've since gone a different direction on that project, but I'll see if I can lend a hand:

First thing to be aware of is that this accelerometer runs on 3.3VDC, do not hook it up to 5V or it will likely die.

The chip can communicate either of SPI or I2C, both of which are serial buses that use specific pins on the Arduino:
Arduino SPI
Arduino I2C

You'll want to read the datasheet, looking at that figure 1 for locations and table 1 for functions of all of the pins. Note especially that pin 7 controls which communication protocol you're wanting to use, in our case I used SPI.

Lastly, reading through my code there are quite a few large chunks of code commented out, these are routines occasionally run once to calibrate things or test outputs from things, please read through the code to figure out what's going on in these sections, it's all thoroughly commented and hopefully is readable. It's been about a year since I've looked at any of it so I won't be much help - if you have questions about a specific section note line numbers in a reply and I can try taking a look.

Hopefully that's helpful :slight_smile:

And now I remember why I'm no longer using this accelerometer - I had misread the spec's initially when looking for a chip, please note carefully that this device outputs 8bit data in either +/-2g, +/-4g, or +/-8g OR 10 bit data in +/-8g

it does not and can not output 10 bit data in the lower ranges, which would give you high resolution readings for low accelerations (which is what I had wanted). Otherwise it works very well :slight_smile:

Hi, I thank you for your help, but unfortunately it is useless to me. I actual fact, I do not understand much of the code.
Could you tell me what could I read to learn a bit about this type of programming?

I connected

  • 3.3 V of the arduino board to the pins 1 and 6
  • the GND of the arduino board to the pin 11.
  • the pin 13 of the arduino board to the pin 14 (clock)
    could you tell me if that is correct, and how to do the other connections?
    I'm sorry but I can not identify the code lines in which you assign the arduino pins address.

Thanks again for your help

Accelerometer Pin -> Arduino Pin
1 -> 3.3V
2 -> GND
3 -> GND
4 -> GND
5 -> GND
6 -> 3.3V
7 -> GND
8 -> NO CONNECTION
9 -> NO CONNECTION
10 -> GND
11 -> GND
12 -> SPO Pin 12
13 ->SPI Pin 11
14 -> SPC Pin 13

SPI is a standard interface, meaning that no matter whose sketch you're running you always use the Arduino SPI data pins that I linked to above in the Arduino playground. (Others - yes, I'm sure you can change those data lines, but there are certain capabilities needed of those pins so using the standard pins is a good idea, YMMV)

Try those pins and see what you come up with, there's a chance you'll have to switch the accelerometer pins 13 and 14 oppositely, meaning if things aren't working try swapping what's hooked to pin 13 -> 14 and pin 14 -> 13

Hi, thanks again.
I tryed to connect the pins as you said, but I realized that I do not have the accelerometer pin 12 available.
I bought the sensor mounted un a PCB. You can see it at:

In the scheme is clear that the pin 12 is not connected to the 7 PCB pins.
How can I do?

P.S.
when I run the code, it is displayed:
X10 0.00
Y10 0.00
Z10 0.00

I'm pretty sure you're going to need all 3 data pins to make it work:

http://www.totalphase.com/support/kb/10045/

I would suggest posting on the Arduino forum specifically asking if you can use SPI with only 2 wires. If you configure the accelerometer for I2C communication you could then use 13, 14 without pin 12, but then you will no longer be able to use my code. You might consider purchasing a different accelerometer that does analog output like one of these:

I'm trying to ask to the Arduino furom as you suggested.

I didn't know that there are analog accelerometers of this type. I'm pretty sure I'll stat using them now.

Thanks.
I'll update this topic if I find a solution.

Cristian