Really thanks a lot for your help, I guess I have better understanding on the code.
But unfortunately, the code still doesn't work, here is my revised code:
#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);
}
I still get only "0" from serial monitor
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 had followed datasheet's instruction to disable the I2C, otherwise the data ready pin never goes HIGH. My previous code failed to check the data ready pin.
Since the chip is I2C / SPI selectable, and pulling CS pin down will make it SPI; high for I2C. I am somewhat fuzzy about this, since it is unavoidable to set the CS pin high before I can pull it low to select it using SPI. Will this make the sensor into I2C mode?
Really sorry to have so many questions. Thank you.