Guest User

LeafLabs Maple adxl345 with hardware i2c library

a guest
Sep 13th, 2011
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. #include "Wire.h"
  2.  
  3. /* the c++ frontend libraries, dubbed Wirish, are always included in sketches.
  4. However, low level libraries, dubbed libmaple, must be included manually */
  5. #include "wirish.h"
  6. #include "i2c.h"
  7.  
  8. #define TIMEOUT 30
  9.  
  10. #define XL345_ADDR    0x53 // use the 7 bit I2C address of the xl345
  11. #define XL345_DEVID   0xE5
  12. #define ITG3200_ADDR  0x69
  13. #define ITG3200_DEVID 0x69
  14.  
  15. // ADXL Control Registers
  16. #define ADXLREG_BW_RATE      0x2C
  17. #define ADXLREG_POWER_CTL    0x2D
  18. #define ADXLREG_DATA_FORMAT  0x31
  19. #define ADXLREG_FIFO_CTL     0x38
  20. #define ADXLREG_BW_RATE      0x2C
  21. #define ADXLREG_TAP_AXES     0x2A
  22. #define ADXLREG_DUR          0x21
  23.  
  24. //ADXL Data Registers
  25. #define ADXLREG_DEVID        0x00
  26. #define ADXLREG_DATAX0       0x32
  27. #define ADXLREG_DATAX1       0x33
  28. #define ADXLREG_DATAY0       0x34
  29. #define ADXLREG_DATAY1       0x35
  30. #define ADXLREG_DATAZ0       0x36
  31. #define ADXLREG_DATAZ1       0x37
  32.  
  33. void writeADXL(uint8 reg, uint8 data) {
  34.   /* all i2c transactions send and receive arrays of i2c_msg objects */
  35.   i2c_msg msgs[1]; // we dont do any bursting here, so we only need one i2c_msg object
  36.   uint8 msg_data[2];
  37.  
  38.   msg_data = {reg,data};
  39.   msgs[0].addr = XL345_ADDR;
  40.   msgs[0].flags = 0; // write
  41.   msgs[0].length = 2;
  42.   msgs[0].data = msg_data;
  43.   i2c_master_xfer(I2C2, msgs, 1,0);
  44. }
  45.  
  46. void xl345_init() {
  47.   /* all i2c transactions send and receive arrays of i2c_msg objects */
  48.   i2c_msg msgs[1]; // we dont do any bursting here, so we only need one i2c_msg object
  49.   uint8 msg_data[2];
  50.  
  51.   /* first make sure everything is wired up properly, by reading the device ID
  52.     The device ID is located in address 0x00 and should always read back 0xE5
  53.    
  54.     To read this device ID, we must first WRITE "0x00" to the device to set the
  55.     address to read on the next transaction. Then we must read 1 byte.
  56.    */
  57.  
  58.   /* i2c_msg objects have fields:
  59.      addr - the 7 bit i2c address of the receiving device
  60.      flags - could be I2C_MSG_READ and/or I2C_MSG_10BIT_ADDR, or 0 for regular write
  61.      length - the number of bytes to transact
  62.      data - pointer to data array being xmitted/received
  63.    */
  64.   msg_data = {0x00,0x00};
  65.   msgs[0].addr = XL345_ADDR;
  66.   msgs[0].flags = 0;
  67.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  68.   msgs[0].data = msg_data;
  69.   i2c_master_xfer(I2C2, msgs, 1,0);
  70.  
  71.   msgs[0].addr = XL345_ADDR;
  72.   msgs[0].flags = I2C_MSG_READ;
  73.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  74.   msgs[0].data = msg_data;
  75.   i2c_master_xfer(I2C2, msgs, 1,0);
  76.  
  77.   /* now we check msg_data for our 0xE5 magic number */
  78.   uint8 dev_id = msg_data[0];
  79.   SerialUSB.print("Read device ID from xl345: ");
  80.   SerialUSB.println(dev_id,HEX);
  81.  
  82.   if (dev_id != XL345_DEVID) {
  83.     SerialUSB.println("Error, incorrect xl345 devid!");
  84.     SerialUSB.println("Halting program, hit reset...");
  85.     waitForButtonPress(0);
  86.   }
  87.  
  88.   /* to enable the xl345 into read mode we need to send three i2c messages
  89.     each messages represents a write into a particular configuration register
  90.     of the xl345.
  91.    
  92.       register 0x2C is "BW_RATE" register, which we set to 0x0A (
  93.       register 0x2D is the "POWER_CTRL" register, which we set to 0x08 (
  94.       register 0x31 is the "DATa_FORMAT" register, which we set to 0x04 (
  95.   */
  96.  
  97.   writeADXL(ADXLREG_POWER_CTL, 0x00);
  98.   writeADXL(ADXLREG_POWER_CTL, 0x08);
  99.  
  100. }
  101.  
  102. void readADXL_burst(uint8 reg, uint8 len, uint8 *msg_data) {
  103.   i2c_msg msgs[1];
  104.   msg_data[0] = reg;
  105.  
  106.   msgs[0].addr = XL345_ADDR;
  107.   msgs[0].flags = 0;
  108.   msgs[0].length = 1; // just one byte for the address to read, 0x00
  109.   msgs[0].data = msg_data;
  110.   i2c_master_xfer(I2C2, msgs, 1,0);
  111.  
  112.   msgs[0].addr = XL345_ADDR;
  113.   msgs[0].flags = I2C_MSG_READ;
  114.   msgs[0].length = len; // just one byte for the address to read, 0x00
  115.   msgs[0].data = msg_data;
  116.   i2c_master_xfer(I2C2, msgs, 1,0);
  117. }
  118.  
  119. int xl345_read_axis(uint8 AXIS_REG) {
  120.   int axis = 0;
  121.   uint8 temp[2]={0};
  122.  
  123.   readADXL_burst(AXIS_REG,2,temp);
  124.  
  125.   axis=((temp[1]<<8)|temp[0]);
  126.   return axis;
  127. }
  128.  
  129. void setup() {
  130.   /* start the sketch when we hit the button, timeout=0 (wait forever) */
  131.   waitForButtonPress(0);
  132.   SerialUSB.println("Initializing sensors");
  133.  
  134.   /* configure I2C port 2 (pins 0, 1) with no special option flags (second argument)*/
  135.   i2c_master_enable(I2C2, 0);
  136.   delay(100); // give time for the peripheral to come up
  137.  
  138.   xl345_init();  
  139.   waitForButtonPress();
  140. }
  141.  
  142. void loop() {
  143.   delay(250);
  144.   int x,y,z;
  145.   x = xl345_read_axis(ADXLREG_DATAX0);
  146.   y = xl345_read_axis(ADXLREG_DATAY0);
  147.   z = xl345_read_axis(ADXLREG_DATAZ0);
  148.   SerialUSB.print(x);
  149.   SerialUSB.print(", ");
  150.   SerialUSB.print(y);
  151.   SerialUSB.print(", ");
  152.   SerialUSB.println(z);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment