Go Down

Topic: ADXL345 (Read 747 times) previous topic - next topic

accel

May 17, 2010, 12:23 am Last Edit: May 17, 2010, 12:23 am by baronep Reason: 1
I tried to hook up my 3 axis accelerometer which has the ADXL345 on it (a similar device is sold from sparkfun.com).
I used the code exactly from http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html

BUT all I get is 0 0 0 as an output.

I have an arduino mega if that makes a difference?

HELP!!!

Groove

#1
May 17, 2010, 08:20 am Last Edit: May 17, 2010, 08:24 am by GrooveFlotilla Reason: 1
Quote
have an arduino mega if that makes a difference


It does.
What pins did you connect the I2C lines to?

Should be pin 20 and 21 on the Mega, not the analogue pins as it is on the Duemilanove.
Per Arduino ad Astra

jpkh

Also check your analogReference. Are you using internal or external. Ok this should not affect on I2C bus but still check it or you might get weird results.

--jp

Groove

@jpkh - I'm curious - how does the source of the analogue reference voltage affect I2C?
Per Arduino ad Astra

accel

no, I had the i2c connected to 20 & 21 as labeled on the board

AWOL

Does the accelerometer have any other i2c registers you could read for a sanity check?
Does it have a sleep mode?
Any alternate addresses that need to be jumpered?
Is the i2c giving any errors or timeouts?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

accel

I don't believe that it has any other i2c registers. I also tried using the spi interface and I kept receiving an output of 255 which is '11111111' in binary so I don't understand whats going on? Is there a way to test and see if the part works?

AWOL

the device does have other registers - see the datasheet.
Do you have access to an oscilloscope?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Groove

Quote
I don't believe that it has any other i2c registers

Looking at the datasheet, the device has about 30 I2C registers, including a device ID at address zero.

Have you tried some debug prints?
Per Arduino ad Astra

accel

Ok, sorry. I haven't tried to do that yet, but will the power settings on register 2d affect the ability for it to send even the device id?

accel

#10
May 18, 2010, 01:49 pm Last Edit: May 18, 2010, 01:51 pm by baronep Reason: 1
Ok, I tried to read from the 0x00 register with a power on setting before it and still nothing. I get a -1 returned for any register I test for.

BTW I think I saw on the data sheet LSB (least significant bit first), but in the i2c/spi libraries its MSB??? If so how do i compensate???

Groove

Are you certain you've got SCL and SDA wired correctly?

Can you post your code?
Per Arduino ad Astra

accel

I have moved on from I2c because I was unsure about the address of my device and that doesn't apply with spi and because SPI has better performance. I am not at home but I will post my code when I get there.

accel

Here is my code:

// define spi bus pins
#define SLAVESELECT 53
#define SPICLOCK 52
#define DATAOUT 51      //MOSI
#define DATAIN 50       //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 = B01011111; //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("Set to wakeup ");
 write_register(0x2d, 0x08);
 
 char tmp2 = read_register(0x31);

 Serial.print("Re - Read from reg 0x31 ");
 Serial.println(tmp2, DEC);

 Serial.println("Setup complete");
}

void loop()
{
 // Todo
 char tmp3 = read_register(0x00);
 Serial.print("Read from reg 0x00 ");
 Serial.println(tmp3, DEC);
 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);
}

Go Up