Collects 30 samples from ADXL345 (3 axis Accelerometer, Sprakfun SEN-09836 breakout board) based on interrupt and presents the data.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
GerritPathuis
Date:
Fri Aug 05 17:58:19 2011 +0000
Parent:
1:7dbdddce7156
Child:
3:f12b8859ecb2
Commit message:
Offset improved

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Jul 25 19:13:06 2011 +0000
+++ b/main.cpp	Fri Aug 05 17:58:19 2011 +0000
@@ -1,8 +1,8 @@
-/* This program collects 30 samples from a Triple Axis 
+/* This program collects 30 samples from a Triple Axis
 *  Accelerometer Breakout ADXL345 sensor (Sparkfun.com SEN-09836)
 *  The program is connected to a mbed via 4-wire spi.
 *  The data is collected via DATA_READY interrupt at maximum speed
-*  of the sensor. 
+*  of the sensor.
 */
 
 #include "mbed.h"
@@ -28,7 +28,6 @@
 
 SPI spi(p5,p6,p7);      // mosi, miso, sclk
 DigitalOut cs(p8);      // cs
-DigitalIn flag(p15);    // 
 InterruptIn event(p9);
 Serial pc(USBTX, USBRX);
 
@@ -45,7 +44,6 @@
     int y;
     int z;
     short int time;
-    short int flag;
 } data [NOSAMPLES];
 
 int acc[3] = {0, 0, 0};
@@ -65,17 +63,19 @@
     interrupt_counter= -1;
     timer.start();
     pc.printf("\n\rBussy collecting data\n\r ");
-    while (interrupt_counter < (NOSAMPLES-1)) 
+    while (interrupt_counter < (NOSAMPLES-1))
         pc.printf(".");
 
     ///////////////// present the data ////////////////////
+    // Full scale 16g is 1024 positions
     pc.printf("\r");
     for (i=0; i <NOSAMPLES; i++) {
-        xx= data[i].x *0.00312;  // 13-bit, sign extended values,   31.2 mg/LSB
-        yy= data[i].y *0.00312;
-        zz= data[i].z *0.00312;
+        xx= data[i].x * 0.004;  // Full-res, sign extended values,   4.0 mg/LSB
+        yy= data[i].y * 0.004;
+        zz= data[i].z * 0.004;
 
-        pc.printf("Sample= %03d, x= %+06.3f, y=%+06.3f, z=%+06.3f, t= %5d us, flag = %2d\n\r", i, xx, yy, zz, data[i].time, data[i].flag );
+        pc.printf("Sample= %03d, x= %+06.3f, y=%+06.3f, z=%+06.3f, t= %5d us\n\r", i, xx, yy, zz, data[i].time);
+        //pc.printf("Sample= %03d, x= %+06d, y=%+06d, z=%+06d \n\r", i, data[i].x, data[i].y, data[i].z);
     }
 }
 
@@ -100,13 +100,13 @@
     data[interrupt_counter].y = (int16_t) acc[1];
     data[interrupt_counter].z = (int16_t) acc[2];
     data[interrupt_counter].time = tstamp;
-    data[interrupt_counter].flag = flag.read();
 
     oneByteRead(0x30);    // clear interrupt sources
 }
 
 void init_SPI_ADXL345() {
     char readID;
+    int ox, oy, oz;     
 
     spi.frequency(5000000);        // max 5 mHz page 8 of the ADXL345 datasheet
     spi.format(8,3);
@@ -119,6 +119,14 @@
         pc.printf("Sorry not connected to ADXL345 !!!(readID= %d)\n\r", readID);
 
     // set up ADXL345
+    ox = 13;    // Offset X direction, not yet two's complement, (15.6 mg/LSB)
+    oy = 2;     // Offset Y direction, not yet two's complement, (15.6 mg/LSB)
+    oz = 3;     // Offset Z direction, not yet two's complement, (15.6 mg/LSB)
+      
+    oneByteWrite(0x1E, -ox);   // Offset X in two's complement 
+    oneByteWrite(0x1F, -oy);   // Offset Y in two's complement
+    oneByteWrite(0x20, -oz);   // Offset Z in two's complement
+
     oneByteWrite(0x2D, 0x00);   // PowerControl, Go into standby mode to configure the device.
     oneByteWrite(0x2E, 0x00);   // InterruptEnableControl, disable all interrupts
 
@@ -134,7 +142,7 @@
     oneByteWrite(0x29, 0x01);   // TIME_FF is set to 1
     oneByteWrite(0x2A, 0x00);   // TAP_AXES disable
 
-    oneByteWrite(0x31, 0x0B);   // setDataFormatControl, 4-wire SPI, Full_RES, Right justify, +/-16g, 4mg/LSB.
+    oneByteWrite(0x31, 0x0B);   // setDataFormatControl, 4-wire SPI, Full res, Right justify, +/-16g, 4 mg/LSB.
     oneByteWrite(0x2F, 0x00);   // setInterruptMappingControl, send ALL interrupt to pin INT1
     oneByteWrite(0x38, 0x01);   // setFifoControl, Bypass, Int pin 1, 1 sample
     oneByteWrite(0x2C, 0x0F);   // setDataRate, 3.2kHz data rate