Pages: [1]   Go Down
Author Topic: Need help with MMA7455 using SPI  (Read 1416 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.

« Last Edit: December 08, 2011, 04:12:48 pm by nagibator_vl » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 170
http://www.open-electronics.org
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged


0
Online Online
Shannon Member
****
Karma: 223
Posts: 12747
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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....
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: