Advertisement
Guest User

Sparkfun ADXL345 Eval Board Arduino Example Code

a guest
Feb 29th, 2012
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.10 KB | None | 0 0
  1. /************************************************************************************
  2.  
  3.   Stefan Dzisiewski-Smith, March 2012
  4.  
  5.   Basic accelerometer value reading code for the Sparkfun ADXL345 Eval Board
  6.  
  7.   Based on the code at http://www.sparkfun.com/tutorials/240 but modified for use
  8.   with shiftOut() instead of hardware SPI, due to the pin mapping on the eval board
  9.  
  10.   I'm not responsible for anything this code does to ANYTHING you own - use it
  11.   at your own risk.
  12.  
  13. ************************************************************************************/
  14.  
  15. // ADXL345 register addresses
  16. const char ID = 0x00;
  17. const char THRESH_TAP = 0x1D;
  18. const char OFSX = 0x1E;
  19. const char OFSY = 0x1F;
  20. const char OFSZ = 0x20;
  21. const char DUR = 0x21;
  22. const char Latent = 0x22;
  23. const char Window = 0x23;
  24. const char THRESH_ACT = 0x24;
  25. const char THRESH_INACT = 0x25;
  26. const char TIME_INACT = 0x26;
  27. const char ACT_INACT_CTL = 0x27;
  28. const char THRESH_FF = 0x28;
  29. const char TIME_FF = 0x29;
  30. const char TAP_AXES = 0x2A;
  31. const char BW_RATE = 0x2C;
  32. const char POWER_CTL = 0x2D;    //Power Control Register
  33. const char INT_ENABLE = 0x2E;
  34. const char INT_MAP = 0x2F;
  35. const char DATA_FORMAT = 0x31;
  36. const char DATAX0 = 0x32;   //X-Axis Data 0
  37. const char DATAX1 = 0x33;   //X-Axis Data 1
  38. const char DATAY0 = 0x34;   //Y-Axis Data 0
  39. const char DATAY1 = 0x35;   //Y-Axis Data 1
  40. const char DATAZ0 = 0x36;   //Z-Axis Data 0
  41. const char DATAZ1 = 0x37;   //Z-Axis Data 1
  42. const char FIFO_CTL = 0x28;
  43.  
  44. unsigned char values[10];
  45. int x, y, z;
  46.  
  47. // pins from the eval board schematic
  48. // we're in 4-wire mode
  49. #define CS 14
  50. #define SCK 15
  51. #define DI 17
  52. #define DO 16
  53.  
  54. // LED pins - defined, although I do nothing with them in this code
  55. #define RED 7
  56. #define YELLOW 5
  57. #define GREEN 6
  58.  
  59. void setup() {
  60.  
  61.   initAccelerometer();
  62.  
  63.   Serial.begin(9600);
  64.   Serial.println("");
  65. }
  66.  
  67. void loop() {
  68.  
  69.   //Reading 6 bytes of data starting at register DATAX0 will retrieve the x,y and z acceleration values from the ADXL345.
  70.   //The results of the read operation will get stored to the values[] buffer.
  71.  
  72.   rAxxReg(DATAX0, 6, (char*)values);
  73.  
  74.   //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.
  75.   //The X value is stored in values[0] and values[1].
  76.   x = ((int)values[1]<<8)|(int)values[0];
  77.   //The Y value is stored in values[2] and values[3].
  78.   y = ((int)values[3]<<8)|(int)values[2];
  79.   //The Z value is stored in values[4] and values[5].
  80.   z = ((int)values[5]<<8)|(int)values[4];
  81.  
  82.   //Print the results to the terminal.
  83.   Serial.print(x, DEC);
  84.   Serial.print(',');
  85.   Serial.print(y, DEC);
  86.   Serial.print(',');
  87.   Serial.println(z, DEC);
  88.  
  89.   delay(10);
  90.  
  91. }
  92.  
  93. void wAxxReg(char address, unsigned char value){
  94.  
  95.   digitalWrite(CS, LOW);
  96.  
  97.   // we're rising edge clocked (see the ADXL345 datasheet) so we have to set SCK low before
  98.   // calling shiftOut
  99.   digitalWrite(SCK, LOW);
  100.   shiftOut(DO, SCK, MSBFIRST, address);
  101.   shiftOut(DO, SCK, MSBFIRST, (char)value);
  102.  
  103.   // reset SCK to its idle HIGH value
  104.   digitalWrite(SCK, HIGH);
  105.   digitalWrite(CS, HIGH);  
  106.  
  107. }
  108.  
  109. void rAxxReg(char address, int numBytes, char * values){
  110.  
  111.   // since we're performing a read operation, the most significant bit of the register address should be set
  112.   char scratchAddress = 0x80 | address;
  113.  
  114.   // if we're doing a multi-byte read, bit 6 needs to be set as well
  115.   if(numBytes > 1)scratchAddress |= 0x40;
  116.  
  117.   digitalWrite(CS, LOW);
  118.  
  119.   // we're rising edge clocked (see the ADXL345 datasheet) so we have to set SCK low before
  120.   // calling shiftOut
  121.   digitalWrite(SCK, LOW);
  122.  
  123.   shiftOut(DO, SCK, MSBFIRST, scratchAddress);
  124.   for(int i=0; i<numBytes; i++){
  125.     values[i] = shiftIn(DI, SCK, MSBFIRST);
  126.   }
  127.  
  128.   // reset SCK to its idle HIGH value
  129.   digitalWrite(SCK, HIGH);
  130.   digitalWrite(CS, HIGH);
  131. }
  132.  
  133. void initAccelerometer(){
  134.  
  135.   // setup ADXL345 pins
  136.   pinMode(CS, OUTPUT);
  137.   pinMode(SCK, OUTPUT);
  138.   pinMode(DI, INPUT);
  139.   pinMode(DO, OUTPUT);
  140.  
  141.   digitalWrite(CS, HIGH);
  142.   digitalWrite(SCK, HIGH);
  143.  
  144.   // Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  145.   wAxxReg(DATA_FORMAT, 0x01);
  146.  
  147.   // Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  148.   wAxxReg(POWER_CTL, 0x08);
  149.  
  150.   // Initialise the rest of the registers to their correct reboot values
  151.   // This isn't strictly necessary, but I managed to somehow end up with an offset on my
  152.   // x-axis after an arduino crash one time - better safe than sorry
  153.   wAxxReg(THRESH_TAP, 0x00);
  154.   wAxxReg(OFSX, 0x00);
  155.   wAxxReg(OFSY, 0x00);
  156.   wAxxReg(OFSZ, 0x00);
  157.   wAxxReg(DUR, 0x00);
  158.   wAxxReg(Latent, 0x00);
  159.   wAxxReg(Window, 0x00);
  160.   wAxxReg(THRESH_ACT, 0x00);
  161.   wAxxReg(THRESH_INACT, 0x00);
  162.   wAxxReg(TIME_INACT, 0x00);
  163.   wAxxReg(ACT_INACT_CTL, 0x00);
  164.   wAxxReg(THRESH_FF, 0x00);
  165.   wAxxReg(TIME_FF, 0x00);
  166.   wAxxReg(TAP_AXES, 0x00);
  167.   wAxxReg(BW_RATE, 0x0A); // note that this register DOESN'T get set to 0x00
  168.   wAxxReg(INT_ENABLE, 0x00);
  169.   wAxxReg(INT_MAP, 0x00);
  170.   wAxxReg(FIFO_CTL, 0x00);
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement