Reading adxl345 accelerometer with SPI

Hi

I am a total newbie to arduino and microcontrollers. (I’m an engineering student though, I do have theoretical knowledge of electronics.) The first thing I tried on arduino was reading the adxl345 accelerometer using the SPI protocol.
Ultimately I failed at reading the device… Can someone help me with my code?

#include <SPI.h>

const int chipSelectPin = 10; //
const int testLed = 5;
const byte registerX0 = 0x32;
const byte registerX1 = 0x33;
const byte READ = B10000000;
const byte WRITE = B00000000;


void setup() {
  //Initialize Serial communication
  Serial.begin(9600);

  //Initialize SPI
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV8);
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE3);

  // Initialize pins
  pinMode(chipSelectPin,OUTPUT);
  pinMode(testLed,OUTPUT);

  // Initialize x-axis accelerometer : 2g range, 4 gate SPI 
  // B100... indicates Self test
  writeRegister(0x31,B10000111);
  // outputRate
  writeRegister(0x2C,B00001100);
  
  // Allowed setup time
  delay(100);
}

void loop(){
  digitalWrite(testLed,HIGH);
  //unsigned int tempData = readRegister(registerX1,1);
  //unsigned int dataReady = readRegister(0x2E,1);
  //Serial.println(readRegister(0x2E,1));
  Serial.println("registerX0 = ");
  Serial.print(readRegister(registerX0,1));
  Serial.println("");
  Serial.println("registerX1 = "); 
  Serial.print(readRegister(registerX1,1));
  Serial.println("");
  Serial.println("end");
  Serial.println("");
  delay(1000);
}

unsigned int readRegister(byte thisRegister, int bytesToRead) {
  byte inByte = 0; 
  unsigned int result = 0;
  byte adresToRead = thisRegister | READ;
//  Serial.println("readAddres = ");
//  Serial.println(adresToRead,BIN);

  digitalWrite(chipSelectPin, LOW);
  // send the device the register you want to read:
  SPI.transfer(adresToRead);
  // send a value of 0 to read the first byte returned:
  result = SPI.transfer(0x00);
  // decrement the number of bytes left to read:
  bytesToRead--;
  // if you still have another byte to read:
  if (bytesToRead > 0) {
    // shift the first byte left, then get the second byte:
    result = result << 8;
    inByte = SPI.transfer(0x00);
    // combine the byte you just got with the previous one:
    result = result | inByte;
    // decrement the number of bytes left to read:
    bytesToRead--;
  }
  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);
  // return the result:
  return(result);
  delay(1);
}

void writeRegister(byte dataToSend, byte thisValue) {

  // ADXL345 expects 1 byte info, 1 byte adres 
  // Input is Register and value
  // For normal write: two msb are 00, no adjustments needed.;
//  Serial.println("writeAddres = ");
//  Serial.println(dataToSend,BIN);

  // take the chip select low to select the device:
  digitalWrite(chipSelectPin, LOW);

  SPI.transfer(dataToSend); //Send register location
  SPI.transfer(thisValue);  //Send value to record into register

  // take the chip select high to de-select:
  digitalWrite(chipSelectPin, HIGH);
  delay(1);
}

Thank you very much and kind regards

B

Mentioning what results you actually get would have been useful. The SPI library knows whether it is reading or writing, so you are not supposed to append the READ/WRITE bit to the address. Doing so can screw things up.

First I received nothing (as in 0). The ADXL345 datasheet indicates that the maximum input voltage at any of the input pins is 3.3V. The output voltage of arduino is 5V, so I used some Zener diodes (3.3V reverse) in parallel at each input of the accelerometer (except for SCLK) to clip these input voltages. When I did this I always got 256 (B11111111) as a result (whether self-test was on or of).

Doesn't your Arduino have a 3.3V supply? An analogue read of 256 implies an input of around 1.25V (1024/4)