Hi guys,
I'm a complete noob to using the arduino and I have a bit of a problem. I have the above accelerometer and I want to control it via SPI.
However I can't seem to get anything back from it.
The code I have currently looks like this:
// define spi bus pins
#define SLAVESELECT 10
#define SPICLOCK 13
#define DATAOUT 11 //MOSI
#define DATAIN 12 //MISO
#define UBLB(a,b) ( ( (a) << 8) | (b) )
#define UBLB19(a,b) ( ( (a) << 16 ) | (b) )
//Addresses
#define REVID 0x00 //ASIC Revision Number
char rev_in_byte;
int temp_in;
unsigned long x;
unsigned long y;
unsigned long z;
void setup()
{
byte clr;
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
SPCR = B01010011; //MPIE=0, SPE=1 (on), DORD=0 (MSB first), MSTR=1 (master), CPOL=0 (clock idle when low), CPHA=0 (samples MOSI on rising edge), SPR1=0 & SPR0=0 (500kHz)
clr=SPSR;
clr=SPDR;
delay(10);
Serial.begin(9600);
delay(500);
Serial.print("Command Register set to ");
Serial.println(SPCR, DEC);
char tmp = read_register(0x31);
Serial.print("Read from reg 0x31 ");
Serial.println(tmp, DEC);
rev_in_byte = read_register(REVID);
Serial.print("Device id ");
Serial.println(rev_in_byte, DEC);
tmp &= 0xEF;
Serial.print("Writing back to reg 0x31 ");
Serial.println(tmp, DEC);
write_register(0x31, tmp);
Serial.println("Setup complete");
}
void loop()
{
// Todo
delay(250);
}
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
{
};
return SPDR; // return the received byte
}
char read_register(char register_name)
{
char in_byte;
register_name <<= 2;
register_name &= B11110100; //Read command
digitalWrite(SLAVESELECT,LOW); //Select SPI Device
delay(10);
spi_transfer(register_name); //Write byte to device
in_byte = spi_transfer(0x00); //Send nothing, but we should get back the register value
digitalWrite(SLAVESELECT,HIGH);
delay(10);
return(in_byte);
}
void write_register(char register_name, char register_value)
{
register_name <<= 2;
register_name |= B00000010; //Write command
digitalWrite(SLAVESELECT,LOW); //Select SPI device
spi_transfer(register_name); //Send register location
spi_transfer(register_value); //Send value to record into register
digitalWrite(SLAVESELECT,HIGH);
}
This code borrows heavily from some other code I found on the web for SPI comms to a barometer.
It doesn't do much at present, just a simple query of the data format register, but even this doesn't work.
I've also tried changing the SPDR values, to what I think the data sheet is telling me, but still no joy - the original settings are above however.
Can anyone tell me if there's something obviously wrong with the code, or whether I should look to wiring as being the problem or the chip being dud.
FYI the wiring is as follows:
adxl345 / Arduino
SCL = 13
SDA = 11
SDO = 12
CS = 10
VCC = 3v3
GND = Gnd
I have tried swapping the MISO and MOSI in the code above, but still no luck.
Thanks in advance guys.