Go Down

Topic: Need help with MMA7455 using SPI (Read 1 time) previous topic - next topic

Dec 08, 2011, 03:59 pm Last Edit: Dec 08, 2011, 10:12 pm by nagibator_vl Reason: 1
Hi! I want to read data from MMA7455 to my Arduino Mega by SPI, but I have some problems.

I connect MMA7455 to arduino mega by 4-wires:


MMA7455       Arduino Mega

  SDO      ->    pin 50 (MISO)
  SDA      ->    pin 51 (MOSI)
  SCL      ->    pin 52 (SCK)  
  CS        ->    pin 53 (SS)

And wrote this code:
Code: [Select]

#include <SPI.h>

char X_data;

#define SELECT_MODE 0x16 //Register for the mode changing
#define MODE_2G 0x09 //2g mode
#define MODE_4G 0x05 //4g mode
#define MODE_8G 0x01 //8g mode

#define x_axis 0x06 //Register for reading the X-Axis
#define y_axis 0x07 //Register for reading the Y-Axis
#define z_axis 0x08 //Register for reading the Z-Axis

#define SS 53   //SS pin

void writeRegister(char Register, char Value) {

 digitalWrite(SS, LOW);

 SPI.transfer(0x80 | (Register << 1));
 SPI.transfer(Value);  

 digitalWrite(SS, HIGH);
}

unsigned char readRegister(char Register) {

 char data;  
 
 digitalWrite(SS, LOW);
 SPI.transfer(Register << 1);
 data = SPI.transfer(0x00);
 digitalWrite(SS, HIGH);
 
 return data;
}

void setup() {
 Serial.begin(9600);
 SPI.begin();

 pinMode(SS, OUTPUT);
 digitalWrite(SS, HIGH);
 delay(100);
 
 writeRegister(SELECT_MODE, MODE_8G);
 delay(1000);
}

void loop() {
 X_data = readRegister(x_axis);
 
  Serial.print(X_data, DEC);
  Serial.print("\n");
  delay(100);
}


...but this program always gives back zero from all axis (

Please help me, where is my mistake? Thanks a lot.


up... I2C is working good, but SPI is not... (

OpenElectronics

Hi, I use the MMA7455 with i2c without problem.
I hope that this page can be helpful for you:
http://www.open-electronics.org/mma7455l-three-axis-digital-output-accelerometer/

Why do you need the SPI connection?

http://www.open-electronics.org

MarkT

Since the CS pin doubles as a I2C enable pin you need to be I2C clean when CS is high.  Thus I'd make sure SCK and MOSI are LOW before releasing CS.  The datasheet says there's a control bit in one of the registers to disable I2C, probably the first thing to check.

You then need to make sure you are using 4-wire and not 3-wire SPI - this may be why you are getting nothing on MISO....
[ I won't respond to messages, use the forum please ]

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy