johnwasser:
Those are probably not doing what you think they are doing.
digitalWrite and digitalRead will set an Arduino pin HIGH or LOW.
0x15 is a decimal value of 21 and your basic Arduino doesn't have a Pin 21.
I suspect you are trying to set a register but you will need SPI transfers for that.
In your accelerometer, register 0x15 is the upper three bits of the "Offset drift Z value" and has nothing to do with an "SPI mode".
You right, about digitalWrite and 0x15, nothing to do with what I want.
johnwasser:
Your results don't change because you have no code in loop() to change x, y, or z. You just send them out, wait 10 milliseconds, and repeat. You probably want to read the data registers from the accelerometer to get values for X, Y and Z. You can just read the 8-bit values to start (registers 0x07, 0x08, and 0x09). Once that works you can try reading the 10-bit versions: 0x00/0x01, 0x02/0x03, 0x04/0x05.
I added a code for the loop, still not working, but no quick sure why. Is the loop code the bug or are there more bugs in the rest?
//Add the SPI library so we can communicate with the MMA7455L sensor
#include <SPI.h>
//Assign the Chip Select signal to pin 10.
int CS=10;
//This buffer will hold values read from the MMA7455L registers.
char values[6];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;
//This is a list of some of the registers available on the MMA7455L.
char DATAX0 = 0x00; //X-Axis 10 bits output Data LSB
char DATAX1 = 0x01; //X-Axis 10 bits output Data MSB
char DATAY0 = 0x02; //Y-Axis 10 bits output Data LSB
char DATAY1 = 0x03; //Y-Axis 10 bits output Data MSB
char DATAZ0 = 0x04; //Z-Axis 10 bits output Data LSB
char DATAZ1 = 0x05; //Z-Axis 10 bits output Data MSB
char XOUT8 = 0x06; //X-Axis 8 bits output Data
char YOUT8 = 0x07; //Y-Axis 8 bits output Data
char ZOUT8 = 0x08; //Z-Axis 8 bits output Data
void setup()
{
//Initiate an SPI communication instance.
SPI.begin();
//Create a serial connection to display the data on the terminal.
Serial.begin(9600);
//Set up the Chip Select pin to be an output from the Arduino.
pinMode(CS, OUTPUT);
//Before communication starts, the Chip Select pin needs to be set high.
digitalWrite(CS, HIGH);
writeRegister(0x16, 0x01); //to set the accelerometer into 2g measurement mode.
}
void loop(){
//The results of the read operation will get stored to the values[] buffer.
readRegister(XOUT8, 3, values);
//The X value is stored in values[6].
x = ((int)values[6] << 1);
//The Y value is stored in values[7].
y = ((int)values[7] << 1);
//The Z value is stored in values[8].
z = ((int)values[8] << 1);
//Print the results to the terminal.
Serial.print(x, DEC);
Serial.print(',');
Serial.print(y, DEC);
Serial.print(',');
Serial.println(z, DEC);
delay(10);
}
// To write a register:
void writeRegister(char registerAddress, char value){
digitalWrite(CS, LOW);
SPI.transfer(registerAddress << 1);
SPI.transfer(value);
digitalWrite(CS, HIGH);
}
//To read a register:
void readRegister(char registerAddress, int numBytes, char * values){
//Since we're performing a read operation, the most significant bit of the register address should be set.
char address = (0x80 | registerAddress << 1);
digitalWrite(CS, LOW);
SPI.transfer(address);
//Continue to read registers until we've read the number specified, storing the results to the input buffer.
for(int i=0; i<numBytes; i++){
values[i] = SPI.transfer(0);
}
digitalWrite(CS, HIGH);
}