interfacing Accelerometer MMA7455L WITH Arduino MEGA ADK ?

i am USING MMA7455L accelerometer , but i can't read from it , i used to do testing using analog one and it is way easier. But now with the digital one i can't read X ,Y,Z . And i tired someone's code in the forum but it just gives 0 0 0

#include <SPI.h> // include the SPI library

const byte mode = 0xAC; // Write Mode Control Register "1"+0x16+"1bit"
const byte measure = 0x05; // 2g sensitive + Measure Mode "00000101"
const byte xout = 0x0C; // Read XOUT register "0"+0x06+"0"
const byte yout = 0x0E; // Read YOUT register "0"+0x07+"0"
const byte zout = 0x10; // Read ZOUT register "0"+0x08+"0"

const byte i2c_add = 0x9A; // Write I2C address "1"+0x0D+"0"
const byte i2cdis = 0x9D; // Disable I2C 0b10011101, bit 6:0 is read only

const int pinReady = 7;
const int pinCS = 5;

void setup() {
Serial.begin(9600); // open serial port
SPI.begin(); // start the SPI library
SPI.setClockDivider(SPI_CLOCK_DIV128); // tune down the speed of SPI
pinMode(pinReady, INPUT); // initalize the pins
pinMode(pinCS, OUTPUT); // initalize the pins

digitalWrite(pinCS, LOW); // low make the chip in SPI mode
delay(1000); // wait for the chip to setup

// Disable I2C, without this code the DataRady pin never go HIGH
digitalWrite(pinCS, HIGH); // turn from HIGH to LOW to give the signal
delay(10); // without this delay, DataReady pin never go HIGH
digitalWrite(pinCS, LOW); // select the sensor
SPI.transfer(i2c_add); // I2C address write command
SPI.transfer(i2cdis); // disable the I2C
digitalWrite(pinCS, HIGH); // de-select the sensor

// Setup the measure mode
digitalWrite(pinCS, HIGH); // turn from HIGH to LOW to give the signal
digitalWrite(pinCS, LOW); // select the sensor
SPI.transfer(mode); // Write to mode address
SPI.transfer(measure); // Send the mode setting value
digitalWrite(pinCS, HIGH); // de-select the sensor
delay(10); // give the sensor time to set up
}

void loop() {
byte valueX, valueY, valueZ;
if (digitalRead(pinReady) == HIGH) { // data ready
digitalWrite(pinCS, LOW); // select the sensor
(void) SPI.transfer(xout); // read value X
valueX = SPI.transfer(0); // '0' is don't care value
digitalWrite(pinCS, HIGH); // de-select the sensor

digitalWrite(pinCS, LOW); // select the sensor
(void) SPI.transfer(yout); // read value Y
valueY = SPI.transfer(0); // '0' is don't care value
digitalWrite(pinCS, HIGH); // de-select the sensor

digitalWrite(pinCS, LOW); // select the sensor
(void) SPI.transfer(zout); // read value Z
valueZ = SPI.transfer(0); // '0' is don't care value
digitalWrite(pinCS, HIGH); // de-select the sensor

// print out the value
Serial.print("X: ");
Serial.print((int) valueX);
Serial.print(" Y: ");
Serial.print((int) valueY);
Serial.print(" Z: ");
Serial.println((int) valueZ);
}
delay(1000);
}

Hi YInk,

I am actually trying out the MMA7455L too at the moment.

And I am having troubles too... I CAN read, but can't seem to be able to write properly. Which means I can't set the mode control register and therefore can't actually get readings since the default mode is standby unfortunately....

What is essential is that you modify the address field that you use in function of what you're doing.

For instance, (according to the MMA7455L.pdf), if you want to read the WHOAMI register, which is 0b1111 :

  1. shift the address by 1 bit to the left : 0b11110
  2. make sure the first bit is 0 (for read action) : 0b0001 1110
  3. you can now send that over the SPI interface, and then as you did send 0 to get the result of the read.

The code for this (it's easier if you factorize your read or write code in methods...) :

int readRegister(byte address)
{
  address = address << 1;
  int result;

  address = address & 0b01111110;  // This tells the MMA7455L we're reading;

  digitalWrite(chipSelect, LOW);
  SPI.transfer(address);
  result = SPI.transfer(0x00);
  digitalWrite(chipSelect, HIGH);

  return result;
}

For the write action, it is similar except that the first bit is supposed to be 1. But as I said, I have failed to make it work as of yet...

If anyone can help... thanks in advance... :slight_smile:

KushankuTom

P.S. : check out the SPI part of the MMA7455L.pdf.

I got it finally!

You must use SPI mode 0, since you have low clock idle and rising edge data shift.

So use SPI.setDataMode(SPI_MODE0) just after the SPI.begin() command.

See SPI - Arduino Reference for details.