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

Dependencies:   mbed

Committer:
GerritPathuis
Date:
Mon Jul 25 19:13:06 2011 +0000
Revision:
1:7dbdddce7156
Parent:
0:a50c8bfe4605
Child:
2:4ed2dd2347f3
Minor changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GerritPathuis 0:a50c8bfe4605 1 /* This program collects 30 samples from a Triple Axis
GerritPathuis 0:a50c8bfe4605 2 * Accelerometer Breakout ADXL345 sensor (Sparkfun.com SEN-09836)
GerritPathuis 0:a50c8bfe4605 3 * The program is connected to a mbed via 4-wire spi.
GerritPathuis 0:a50c8bfe4605 4 * The data is collected via DATA_READY interrupt at maximum speed
GerritPathuis 0:a50c8bfe4605 5 * of the sensor.
GerritPathuis 0:a50c8bfe4605 6 */
GerritPathuis 0:a50c8bfe4605 7
GerritPathuis 0:a50c8bfe4605 8 #include "mbed.h"
GerritPathuis 0:a50c8bfe4605 9 #define NOSAMPLES 30
GerritPathuis 0:a50c8bfe4605 10 #define DEVID 0x00
GerritPathuis 0:a50c8bfe4605 11 #define ADXL345_SPI_READ 0x80
GerritPathuis 0:a50c8bfe4605 12 #define ADXL345_SPI_WRITE 0x00
GerritPathuis 0:a50c8bfe4605 13 #define ADXL345_MULTI_BYTE 0x60
GerritPathuis 0:a50c8bfe4605 14 #define ADXL345_DATAX0_REG 0x32
GerritPathuis 0:a50c8bfe4605 15
GerritPathuis 0:a50c8bfe4605 16 /////////4-wire SPI///////////
GerritPathuis 0:a50c8bfe4605 17 //// Hardware connections ////
GerritPathuis 0:a50c8bfe4605 18 // ADXL345
GerritPathuis 0:a50c8bfe4605 19 // Signal ---------- mbed pin
GerritPathuis 0:a50c8bfe4605 20 // Vcc ------------ mbed Vout
GerritPathuis 0:a50c8bfe4605 21 // Gnd ------------ mbed Gnd
GerritPathuis 0:a50c8bfe4605 22 // SDA ------------ mbed p5
GerritPathuis 0:a50c8bfe4605 23 // SDO ------------ mbed p6
GerritPathuis 0:a50c8bfe4605 24 // SCL ------------ mbed p7
GerritPathuis 0:a50c8bfe4605 25 // CS ------------ mbed p8
GerritPathuis 0:a50c8bfe4605 26 // INTI ------------ mbed p9 // ADXL345 gives a interrupt when ready
GerritPathuis 0:a50c8bfe4605 27 //////////////////////////////
GerritPathuis 0:a50c8bfe4605 28
GerritPathuis 0:a50c8bfe4605 29 SPI spi(p5,p6,p7); // mosi, miso, sclk
GerritPathuis 0:a50c8bfe4605 30 DigitalOut cs(p8); // cs
GerritPathuis 0:a50c8bfe4605 31 DigitalIn flag(p15); //
GerritPathuis 0:a50c8bfe4605 32 InterruptIn event(p9);
GerritPathuis 0:a50c8bfe4605 33 Serial pc(USBTX, USBRX);
GerritPathuis 0:a50c8bfe4605 34
GerritPathuis 0:a50c8bfe4605 35 void int_service(void);
GerritPathuis 0:a50c8bfe4605 36 void init_SPI_ADXL345(void);
GerritPathuis 0:a50c8bfe4605 37 int oneByteRead(int address);
GerritPathuis 0:a50c8bfe4605 38 void oneByteWrite(int address, char data);
GerritPathuis 0:a50c8bfe4605 39 void multiByteRead(int startAddress, char* buffer, int size);
GerritPathuis 0:a50c8bfe4605 40 void multiByteWrite(int startAddress, char* buffer, int size);
GerritPathuis 0:a50c8bfe4605 41
GerritPathuis 0:a50c8bfe4605 42 Timer timer;
GerritPathuis 0:a50c8bfe4605 43 struct ttstamp {
GerritPathuis 0:a50c8bfe4605 44 int x;
GerritPathuis 0:a50c8bfe4605 45 int y;
GerritPathuis 0:a50c8bfe4605 46 int z;
GerritPathuis 0:a50c8bfe4605 47 short int time;
GerritPathuis 0:a50c8bfe4605 48 short int flag;
GerritPathuis 0:a50c8bfe4605 49 } data [NOSAMPLES];
GerritPathuis 0:a50c8bfe4605 50
GerritPathuis 0:a50c8bfe4605 51 int acc[3] = {0, 0, 0};
GerritPathuis 0:a50c8bfe4605 52 int interrupt_counter, tstamp;
GerritPathuis 0:a50c8bfe4605 53
GerritPathuis 0:a50c8bfe4605 54 int main() {
GerritPathuis 0:a50c8bfe4605 55 int i;
GerritPathuis 0:a50c8bfe4605 56 float xx, yy, zz;
GerritPathuis 0:a50c8bfe4605 57
GerritPathuis 0:a50c8bfe4605 58 pc.baud(9600);
GerritPathuis 0:a50c8bfe4605 59 pc.format(8,Serial::None,1);
GerritPathuis 0:a50c8bfe4605 60
GerritPathuis 0:a50c8bfe4605 61 event.rise(&int_service); // set the interrupt handling
GerritPathuis 0:a50c8bfe4605 62 init_SPI_ADXL345(); // init the ADXL345 sensor
GerritPathuis 0:a50c8bfe4605 63
GerritPathuis 0:a50c8bfe4605 64 ////////////////// get the data //////////////////////
GerritPathuis 0:a50c8bfe4605 65 interrupt_counter= -1;
GerritPathuis 0:a50c8bfe4605 66 timer.start();
GerritPathuis 1:7dbdddce7156 67 pc.printf("\n\rBussy collecting data\n\r ");
GerritPathuis 0:a50c8bfe4605 68 while (interrupt_counter < (NOSAMPLES-1))
GerritPathuis 1:7dbdddce7156 69 pc.printf(".");
GerritPathuis 0:a50c8bfe4605 70
GerritPathuis 0:a50c8bfe4605 71 ///////////////// present the data ////////////////////
GerritPathuis 1:7dbdddce7156 72 pc.printf("\r");
GerritPathuis 0:a50c8bfe4605 73 for (i=0; i <NOSAMPLES; i++) {
GerritPathuis 0:a50c8bfe4605 74 xx= data[i].x *0.00312; // 13-bit, sign extended values, 31.2 mg/LSB
GerritPathuis 0:a50c8bfe4605 75 yy= data[i].y *0.00312;
GerritPathuis 0:a50c8bfe4605 76 zz= data[i].z *0.00312;
GerritPathuis 0:a50c8bfe4605 77
GerritPathuis 0:a50c8bfe4605 78 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 );
GerritPathuis 0:a50c8bfe4605 79 }
GerritPathuis 0:a50c8bfe4605 80 }
GerritPathuis 0:a50c8bfe4605 81
GerritPathuis 0:a50c8bfe4605 82 ///////////////////// Interrupt service /////////////////////
GerritPathuis 0:a50c8bfe4605 83 ///////////////////// store data in array ///////////////////
GerritPathuis 0:a50c8bfe4605 84 ///////////////////// clear interrups ///////////////////////
GerritPathuis 0:a50c8bfe4605 85 void int_service(void) {
GerritPathuis 0:a50c8bfe4605 86 char buffer[6];
GerritPathuis 0:a50c8bfe4605 87
GerritPathuis 0:a50c8bfe4605 88 interrupt_counter += 1;
GerritPathuis 0:a50c8bfe4605 89 if (interrupt_counter == (NOSAMPLES -1))
GerritPathuis 0:a50c8bfe4605 90 oneByteWrite(0x2E, 0x00); // InterruptEnableControl, disable all interrupts
GerritPathuis 0:a50c8bfe4605 91
GerritPathuis 0:a50c8bfe4605 92 multiByteRead(ADXL345_DATAX0_REG, buffer, 6); // read the DATAX,DATZ,DATAY register
GerritPathuis 0:a50c8bfe4605 93 acc[0] = (int)buffer[1] << 8 | (int)buffer[0];
GerritPathuis 0:a50c8bfe4605 94 acc[1] = (int)buffer[3] << 8 | (int)buffer[2];
GerritPathuis 0:a50c8bfe4605 95 acc[2] = (int)buffer[5] << 8 | (int)buffer[4];
GerritPathuis 0:a50c8bfe4605 96
GerritPathuis 0:a50c8bfe4605 97 tstamp= timer.read_us();
GerritPathuis 0:a50c8bfe4605 98 // stora data in array
GerritPathuis 0:a50c8bfe4605 99 data[interrupt_counter].x = (int16_t) acc[0];
GerritPathuis 0:a50c8bfe4605 100 data[interrupt_counter].y = (int16_t) acc[1];
GerritPathuis 0:a50c8bfe4605 101 data[interrupt_counter].z = (int16_t) acc[2];
GerritPathuis 0:a50c8bfe4605 102 data[interrupt_counter].time = tstamp;
GerritPathuis 0:a50c8bfe4605 103 data[interrupt_counter].flag = flag.read();
GerritPathuis 0:a50c8bfe4605 104
GerritPathuis 0:a50c8bfe4605 105 oneByteRead(0x30); // clear interrupt sources
GerritPathuis 0:a50c8bfe4605 106 }
GerritPathuis 0:a50c8bfe4605 107
GerritPathuis 0:a50c8bfe4605 108 void init_SPI_ADXL345() {
GerritPathuis 0:a50c8bfe4605 109 char readID;
GerritPathuis 0:a50c8bfe4605 110
GerritPathuis 0:a50c8bfe4605 111 spi.frequency(5000000); // max 5 mHz page 8 of the ADXL345 datasheet
GerritPathuis 0:a50c8bfe4605 112 spi.format(8,3);
GerritPathuis 0:a50c8bfe4605 113 wait(0.1);
GerritPathuis 0:a50c8bfe4605 114
GerritPathuis 0:a50c8bfe4605 115 readID = oneByteRead(DEVID);
GerritPathuis 0:a50c8bfe4605 116 if (readID == 0xE5)
GerritPathuis 0:a50c8bfe4605 117 pc.printf("\n\rConnected to ADXL345\n\r");
GerritPathuis 0:a50c8bfe4605 118 else
GerritPathuis 0:a50c8bfe4605 119 pc.printf("Sorry not connected to ADXL345 !!!(readID= %d)\n\r", readID);
GerritPathuis 0:a50c8bfe4605 120
GerritPathuis 0:a50c8bfe4605 121 // set up ADXL345
GerritPathuis 0:a50c8bfe4605 122 oneByteWrite(0x2D, 0x00); // PowerControl, Go into standby mode to configure the device.
GerritPathuis 0:a50c8bfe4605 123 oneByteWrite(0x2E, 0x00); // InterruptEnableControl, disable all interrupts
GerritPathuis 0:a50c8bfe4605 124
GerritPathuis 0:a50c8bfe4605 125 oneByteWrite(0x1D, 0x01); // TRESH_TAP is set to 1
GerritPathuis 0:a50c8bfe4605 126 oneByteWrite(0x21, 0x00); // DUR disable
GerritPathuis 0:a50c8bfe4605 127 oneByteWrite(0x22, 0x00); // LATENT disable
GerritPathuis 0:a50c8bfe4605 128 oneByteWrite(0x23, 0x00); // WINDOW disable
GerritPathuis 0:a50c8bfe4605 129 oneByteWrite(0x24, 0x01); // TRESH_ACT is set to 1
GerritPathuis 0:a50c8bfe4605 130 oneByteWrite(0x25, 0x01); // TRESH_INACT is set to 1
GerritPathuis 0:a50c8bfe4605 131 oneByteWrite(0x26, 0x01); // TIME_INACT is set to 1
GerritPathuis 0:a50c8bfe4605 132 oneByteWrite(0x27, 0x00); // ACT_INACT_CTL disable
GerritPathuis 0:a50c8bfe4605 133 oneByteWrite(0x28, 0x01); // TRESF_FF is set to 1
GerritPathuis 0:a50c8bfe4605 134 oneByteWrite(0x29, 0x01); // TIME_FF is set to 1
GerritPathuis 0:a50c8bfe4605 135 oneByteWrite(0x2A, 0x00); // TAP_AXES disable
GerritPathuis 0:a50c8bfe4605 136
GerritPathuis 0:a50c8bfe4605 137 oneByteWrite(0x31, 0x0B); // setDataFormatControl, 4-wire SPI, Full_RES, Right justify, +/-16g, 4mg/LSB.
GerritPathuis 0:a50c8bfe4605 138 oneByteWrite(0x2F, 0x00); // setInterruptMappingControl, send ALL interrupt to pin INT1
GerritPathuis 0:a50c8bfe4605 139 oneByteWrite(0x38, 0x01); // setFifoControl, Bypass, Int pin 1, 1 sample
GerritPathuis 0:a50c8bfe4605 140 oneByteWrite(0x2C, 0x0F); // setDataRate, 3.2kHz data rate
GerritPathuis 0:a50c8bfe4605 141 oneByteWrite(0x2D, 0x00); // setPowerControl, measurement mode (start measuring) (se page 17 sleep bit)
GerritPathuis 0:a50c8bfe4605 142 oneByteWrite(0x2D, 0x08); // setPowerControl, measurement mode (start measuring)
GerritPathuis 0:a50c8bfe4605 143 oneByteWrite(0x2E, 0x80); // setInterruptEnableControl, enable interrupt DATA_READY
GerritPathuis 0:a50c8bfe4605 144 }
GerritPathuis 0:a50c8bfe4605 145
GerritPathuis 0:a50c8bfe4605 146 int oneByteRead(int address) {
GerritPathuis 0:a50c8bfe4605 147 int tx = (ADXL345_SPI_READ | (address & 0x3F));
GerritPathuis 0:a50c8bfe4605 148 int rx = 0;
GerritPathuis 0:a50c8bfe4605 149
GerritPathuis 0:a50c8bfe4605 150 cs = 0; //Send address to read from.
GerritPathuis 0:a50c8bfe4605 151 spi.write(tx); //Read back contents of address.
GerritPathuis 0:a50c8bfe4605 152 rx = spi.write(0x00);
GerritPathuis 0:a50c8bfe4605 153 cs = 1;
GerritPathuis 0:a50c8bfe4605 154 return rx;
GerritPathuis 0:a50c8bfe4605 155 }
GerritPathuis 0:a50c8bfe4605 156
GerritPathuis 0:a50c8bfe4605 157 void oneByteWrite(int address, char data) {
GerritPathuis 0:a50c8bfe4605 158 int tx = (ADXL345_SPI_WRITE | (address & 0x3F));
GerritPathuis 0:a50c8bfe4605 159
GerritPathuis 0:a50c8bfe4605 160 cs = 0; //Send address to write to.
GerritPathuis 0:a50c8bfe4605 161 spi.write(tx); //Send data to be written.
GerritPathuis 0:a50c8bfe4605 162 spi.write(data);
GerritPathuis 0:a50c8bfe4605 163 cs = 1;
GerritPathuis 0:a50c8bfe4605 164 }
GerritPathuis 0:a50c8bfe4605 165
GerritPathuis 0:a50c8bfe4605 166 void multiByteRead(int startAddress, char* buffer, int size) {
GerritPathuis 0:a50c8bfe4605 167 int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
GerritPathuis 0:a50c8bfe4605 168
GerritPathuis 0:a50c8bfe4605 169 cs = 0; //Send address to start reading from.
GerritPathuis 0:a50c8bfe4605 170 spi.write(tx);
GerritPathuis 0:a50c8bfe4605 171 for (int i = 0; i < size; i++) {
GerritPathuis 0:a50c8bfe4605 172 buffer[i] = spi.write(0x00);
GerritPathuis 0:a50c8bfe4605 173 }
GerritPathuis 0:a50c8bfe4605 174 cs = 1;
GerritPathuis 0:a50c8bfe4605 175 }
GerritPathuis 0:a50c8bfe4605 176
GerritPathuis 0:a50c8bfe4605 177 void multiByteWrite(int startAddress, char* buffer, int size) {
GerritPathuis 0:a50c8bfe4605 178 int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
GerritPathuis 0:a50c8bfe4605 179
GerritPathuis 0:a50c8bfe4605 180 cs = 0; //Send address to start reading from.
GerritPathuis 0:a50c8bfe4605 181 spi.write(tx);
GerritPathuis 0:a50c8bfe4605 182 for (int i = 0; i < size; i++) {
GerritPathuis 0:a50c8bfe4605 183 buffer[i] = spi.write(0x00);
GerritPathuis 0:a50c8bfe4605 184 }
GerritPathuis 0:a50c8bfe4605 185 cs = 1;
GerritPathuis 0:a50c8bfe4605 186 }
GerritPathuis 0:a50c8bfe4605 187