Pages: 1 [2]   Go Down
Author Topic: How to read register value from ADXL345 Eval board?  (Read 3104 times)
0 Members and 1 Guest are viewing this topic.
Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8081
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Show your sketch and I'll see if I can figure out what is wrong.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Show your sketch and I'll see if I can figure out what is wrong.
Thanks so much!!! smiley-grin
ok now i am referring here: http://www.sparkfun.com/tutorials/240

make my sketch to become like this:
Setup:
Code:
//Add the SPI library so we can communicate with the ADXL345 sensor
#include <SPI.h>
int CS=10;
#define ACCEL_CS A0
#define ACCEL_SCK A1
#define ACCEL_DI A2
#define ACCEL_DO A3
int ledPin = 2;
//This is a list of some of the registers available on the ADXL345.
//To learn more about these and the rest of the registers on the ADXL345, read the datasheet!
char POWER_CTL = 0x2D; //Power Control Register
char DATA_FORMAT = 0x31;
char DATAX0 = 0x32; //X-Axis Data 0
char DATAX1 = 0x33; //X-Axis Data 1
char DATAY0 = 0x34; //Y-Axis Data 0
char DATAY1 = 0x35; //Y-Axis Data 1
char DATAZ0 = 0x36; //Z-Axis Data 0
char DATAZ1 = 0x37; //Z-Axis Data 1

//This buffer will hold values read from the ADXL345 registers.
char values[10];
//These variables will be used to hold the x,y and z axis accelerometer values.
int x,y,z;

void setup(){
  //Initiate an SPI communication instance.
  SPI.begin();
  //Configure the SPI connection for the ADXL345.
  SPI.setDataMode(SPI_MODE3);
  //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);
  pinMode(ledPin, OUTPUT);
  pinMode(ACCEL_CS, OUTPUT);
  pinMode(ACCEL_SCK, OUTPUT);
  pinMode(ACCEL_DI, OUTPUT);
  pinMode(ACCEL_DO, INPUT);
 
  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  writeRegister(DATA_FORMAT, 0x01);
  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  writeRegister(POWER_CTL, 0x08);  //Measurement mode 
}

loop:
Code:
void loop(){
  //Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
  //The results of the read operation will get stored to the values[] buffer.
  readRegister(DATAX0, 6, values);
  int temp;
  //The ADXL345 gives 10-bit acceleration values, but they are stored as bytes (8-bits). To get the full value, two bytes must be combined for each axis.
  //The X value is stored in values[0] and values[1].
  x = ((int)values[1]<<8)|(int)values[0];
  //The Y value is stored in values[2] and values[3].
  y = ((int)values[3]<<8)|(int)values[2];
  //The Z value is stored in values[4] and values[5].
  z = ((int)values[5]<<8)|(int)values[4];
  if (EnsureConnected()){
       digitalWrite(ledPin, HIGH);   // sets the LED on
       delay(1000);}
        else{
        digitalWrite(ledPin, LOW);    // sets the LED off
        delay(1000); }                 // waits for a second
  //Print the results to the terminal.
 /* Serial.print(x, DEC);
  Serial.print(',');
  Serial.print(y, DEC);
  Serial.print(',');
  Serial.println(z, DEC);     
  delay(10); */
  temp = x;
}
write and read:
Code:
void writeRegister(char registerAddress, char value){
  //Set Chip Select pin low to signal the beginning of an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the register address over SPI.
  SPI.transfer(registerAddress);
  //Transfer the desired register value over SPI.
  SPI.transfer(value);
  //Set the Chip Select pin high to signal the end of an SPI packet.
  digitalWrite(CS, HIGH);
}

//This function will read a certain number of registers starting from a specified address and store their values in a buffer.
//Parameters:
//  char registerAddress - The register addresse to start the read sequence from.
//  int numBytes - The number of registers that should be read.
//  char * values - A pointer to a buffer where the results of the operation should be stored.
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;
  //If we're doing a multi-byte read, bit 6 needs to be set as well.
  if(numBytes > 1)address = address | 0x40;
 
  //Set the Chip select pin low to start an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the starting register address that needs to be read.
  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(0x00);
  }
  //Set the Chips Select pin high to end the SPI packet.
  digitalWrite(CS, HIGH);
}

Now i am just wondering..as i am using USB connection between adxl eval board and arduino...above SPI code can works or not?

In the loop function, i add a if else condition e.g :ensure connection() to test whether it is really getting any result or not....can you suggest me any condition that i can make to test whether i can read the register value or not?

Thousand thanks for you. smiley-cry
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8081
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make sure you don't have an SD card in the SD card slot.  The SD card slot uses the hardware SPI pins (10-13) so if you put a card in the slot you can't use Pin 10 for anything else.

If you want to use the sketch from http://www.sparkfun.com/tutorials/240 you will have to re-wire the evaluation board to connect the ADXL345 to pins 10-13 instead of pins 14-17 (A0-A3).  You should not need to make any change to the sketch supplied with that tutorial.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make sure you don't have an SD card in the SD card slot.  The SD card slot uses the hardware SPI pins (10-13) so if you put a card in the slot you can't use Pin 10 for anything else.

If you want to use the sketch from http://www.sparkfun.com/tutorials/240 you will have to re-wire the evaluation board to connect the ADXL345 to pins 10-13 instead of pins 14-17 (A0-A3).  You should not need to make any change to the sketch supplied with that tutorial.

thanks for your guidance..so that's mean instead of declare it by:
#define ACCEL_CS   A0
#define ACCEL_SCK   A1
#define ACCEL_DI   A2
#define ACCEL_DO   A3
i can change it to lets say:
#define ACCEL_CS   10
#define ACCEL_SCK   13
#define ACCEL_DI   11
#define ACCEL_DO   12

??then the rest of code works fine??ya i did read on the user guide of eval board said that SD card will used up the SPI so i didnt insert the SD card. Thanks for that also. Your explanation make my uncertainty clear. thanks so much. and actually with the code above, will it really works for USB connection?

and what do u mean by rewired??since the eval board is a complete module???sorry for any lame question....thanks so much^^

Edit: this is what i am connect for my arduino board. and this is act what i wan...hope u understand...i just draw it using mspaint...sorry for unclear picture.


Led is just use for testing...if really confirm getting the register value then i will replace it with servo motor.
« Last Edit: March 23, 2012, 08:56:26 am by tlslaj0417 » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8081
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The ADXL345  Evaluation Board is NOT a good choice if you want to connect the ADXL345 to an Arduino UNO.  You should buy an ADXL345 on a breakout board:

http://www.sparkfun.com/products/9836
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The ADXL345  Evaluation Board is NOT a good choice if you want to connect the ADXL345 to an Arduino UNO.  You should buy an ADXL345 on a breakout board:

http://www.sparkfun.com/products/9836

this i am understand..because i saw plenty of people using this for that....but i just want to ask that whether this eval board can do that also?because i hope i can done it with this eval board. is this possible?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8081
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i just want to ask that whether this eval board can do that also? because i hope i can done it with this eval board. is this possible?

The Eval Board has an ATmega chip connected to the ADXL chip.  There is no place on the Eval Board designed to bypass the on-board ATmega and connect the ADXL to an Arduino UNO.  To do that properly you need a board designed to be connected to an Arduino UNO, like the breakout board I mentioned.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i just want to ask that whether this eval board can do that also? because i hope i can done it with this eval board. is this possible?

The Eval Board has an ATmega chip connected to the ADXL chip.  There is no place on the Eval Board designed to bypass the on-board ATmega and connect the ADXL to an Arduino UNO.  To do that properly you need a board designed to be connected to an Arduino UNO, like the breakout board I mentioned.

thanks for your information. i will discuss with my partner regarding this...would you mind i contact u in pm onwards if i meet any problem?thanks for you information for all of these smiley
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8081
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you encounter a problem you should post a new question on the forum.  That way it will be searchable by others that might have a similar problem in the future.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you encounter a problem you should post a new question on the forum.  That way it will be searchable by others that might have a similar problem in the future.
is ok thanks. i will repost it if i meet another problem thanks!!
Logged

Pages: 1 [2]   Go Up
Jump to: