Hi, I am just a newbie and not very good at English, please excuse me.
I have trouble in reading a MMA7455L sensor board using my Arduino UNO (SPI).
My wire connection:
Clk -> Pin 13
MOSI -> Pin 11
MISO -> Pin 12
Data Ready -> Pin7
CS or SS? -> Pin 10
My Code:
#include <SPI.h> // include the SPI library
const byte mode = 0x16; // Mode Control Register is $16
const byte measure = 0b00000101; // 1st 01 set to 2g, 2nd 01 set to measure mode
const byte xout = 0x06; // X-Axis value register is $06
const byte yout = 0x07; // Y-Axis value register is $07
const byte zout = 0x08; // Z-Axis value register is $08const int pinReady = 7;
const int pinCS = 10;void setup() {
Serial.begin(9600); // open serial port
SPI.begin(); // start the SPI library
pinMode(pinReady, INPUT); // initalize the pins
pinMode(pinCS, OUTPUT); // initalize the pins// Configure MMA7455 into 2g sensitivies and measure mode
digitalWrite(pinCS, LOW); // select the sensor
SPI.transfer(mode); // Send register location
SPI.transfer(measure); // Send value into that register
digitalWrite(pinCS, HIGH); // de-select the sensor
delay(100); // give the sensor time to set up
}void loop() {
if (digitalRead(pinReady == HIGH)) {
digitalWrite(pinCS, LOW); // select the sensor
byte valueX = SPI.transfer(xout);
byte valueY = SPI.transfer(yout);
byte valueZ = SPI.transfer(zout);
digitalWrite(pinCS, HIGH); // de-select the sensorSerial.print("X: ");
Serial.print((int) valueX);
Serial.print(" Y: ");
Serial.print((int) valueY);
Serial.print(" Z: ");
Serial.println((int) valueZ);
}
delay(1000);
}
The Serial Monitor print out only:
X: 0 Y: 0 Z: 0
X: 0 Y: 0 Z: 0
X: 0 Y: 0 Z: 0
X: 0 Y: 0 Z: 0
....
I have read from other post and the MMA7455L data sheet that:
A SPI read transfer consists of a 1-bit Read/Write signal, a 6-bit address, and 1-bit don’t care bit. (1-bit R/W=0 + 6-bits address
- 1-bit don’t care). The data to read is sent by the SPI interface during the next transfer.
In order to write to one of the 8-bit registers, an 8-bit write command must be sent to the MMA7455L. The write command consists
of an MSB (0=read, 1=write) to indicate writing to the MMA7455L register, followed by a 6-bit address and 1 don’t care bit[/li]
This is my first attempt to use SPI and MMA7455L sensor. I don't know what the above mean, can anyone suggest me how to write the code to read /write from the sensor? Thanks a lot.
Andy