Collects 30 samples from ADXL345 (3 axis Accelerometer, Sprakfun SEN-09836 breakout board) based on interrupt and presents the data.
main.cpp@3:f12b8859ecb2, 2017-09-22 (annotated)
- Committer:
- GerritPathuis
- Date:
- Fri Sep 22 20:56:47 2017 +0000
- Revision:
- 3:f12b8859ecb2
- Parent:
- 2:4ed2dd2347f3
update mbed lib version 151
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GerritPathuis | 2:4ed2dd2347f3 | 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 | 2:4ed2dd2347f3 | 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 | InterruptIn event(p9); |
GerritPathuis | 0:a50c8bfe4605 | 32 | Serial pc(USBTX, USBRX); |
GerritPathuis | 0:a50c8bfe4605 | 33 | |
GerritPathuis | 0:a50c8bfe4605 | 34 | void int_service(void); |
GerritPathuis | 0:a50c8bfe4605 | 35 | void init_SPI_ADXL345(void); |
GerritPathuis | 0:a50c8bfe4605 | 36 | int oneByteRead(int address); |
GerritPathuis | 0:a50c8bfe4605 | 37 | void oneByteWrite(int address, char data); |
GerritPathuis | 0:a50c8bfe4605 | 38 | void multiByteRead(int startAddress, char* buffer, int size); |
GerritPathuis | 0:a50c8bfe4605 | 39 | void multiByteWrite(int startAddress, char* buffer, int size); |
GerritPathuis | 0:a50c8bfe4605 | 40 | |
GerritPathuis | 0:a50c8bfe4605 | 41 | Timer timer; |
GerritPathuis | 0:a50c8bfe4605 | 42 | struct ttstamp { |
GerritPathuis | 0:a50c8bfe4605 | 43 | int x; |
GerritPathuis | 0:a50c8bfe4605 | 44 | int y; |
GerritPathuis | 0:a50c8bfe4605 | 45 | int z; |
GerritPathuis | 0:a50c8bfe4605 | 46 | short int time; |
GerritPathuis | 0:a50c8bfe4605 | 47 | } data [NOSAMPLES]; |
GerritPathuis | 0:a50c8bfe4605 | 48 | |
GerritPathuis | 0:a50c8bfe4605 | 49 | int acc[3] = {0, 0, 0}; |
GerritPathuis | 0:a50c8bfe4605 | 50 | int interrupt_counter, tstamp; |
GerritPathuis | 0:a50c8bfe4605 | 51 | |
GerritPathuis | 0:a50c8bfe4605 | 52 | int main() { |
GerritPathuis | 0:a50c8bfe4605 | 53 | int i; |
GerritPathuis | 0:a50c8bfe4605 | 54 | float xx, yy, zz; |
GerritPathuis | 0:a50c8bfe4605 | 55 | |
GerritPathuis | 0:a50c8bfe4605 | 56 | pc.baud(9600); |
GerritPathuis | 0:a50c8bfe4605 | 57 | pc.format(8,Serial::None,1); |
GerritPathuis | 0:a50c8bfe4605 | 58 | |
GerritPathuis | 0:a50c8bfe4605 | 59 | event.rise(&int_service); // set the interrupt handling |
GerritPathuis | 0:a50c8bfe4605 | 60 | init_SPI_ADXL345(); // init the ADXL345 sensor |
GerritPathuis | 0:a50c8bfe4605 | 61 | |
GerritPathuis | 0:a50c8bfe4605 | 62 | ////////////////// get the data ////////////////////// |
GerritPathuis | 0:a50c8bfe4605 | 63 | interrupt_counter= -1; |
GerritPathuis | 0:a50c8bfe4605 | 64 | timer.start(); |
GerritPathuis | 1:7dbdddce7156 | 65 | pc.printf("\n\rBussy collecting data\n\r "); |
GerritPathuis | 2:4ed2dd2347f3 | 66 | while (interrupt_counter < (NOSAMPLES-1)) |
GerritPathuis | 1:7dbdddce7156 | 67 | pc.printf("."); |
GerritPathuis | 0:a50c8bfe4605 | 68 | |
GerritPathuis | 0:a50c8bfe4605 | 69 | ///////////////// present the data //////////////////// |
GerritPathuis | 2:4ed2dd2347f3 | 70 | // Full scale 16g is 1024 positions |
GerritPathuis | 1:7dbdddce7156 | 71 | pc.printf("\r"); |
GerritPathuis | 0:a50c8bfe4605 | 72 | for (i=0; i <NOSAMPLES; i++) { |
GerritPathuis | 2:4ed2dd2347f3 | 73 | xx= data[i].x * 0.004; // Full-res, sign extended values, 4.0 mg/LSB |
GerritPathuis | 2:4ed2dd2347f3 | 74 | yy= data[i].y * 0.004; |
GerritPathuis | 2:4ed2dd2347f3 | 75 | zz= data[i].z * 0.004; |
GerritPathuis | 0:a50c8bfe4605 | 76 | |
GerritPathuis | 2:4ed2dd2347f3 | 77 | 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); |
GerritPathuis | 2:4ed2dd2347f3 | 78 | //pc.printf("Sample= %03d, x= %+06d, y=%+06d, z=%+06d \n\r", i, data[i].x, data[i].y, data[i].z); |
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 | |
GerritPathuis | 0:a50c8bfe4605 | 104 | oneByteRead(0x30); // clear interrupt sources |
GerritPathuis | 0:a50c8bfe4605 | 105 | } |
GerritPathuis | 0:a50c8bfe4605 | 106 | |
GerritPathuis | 0:a50c8bfe4605 | 107 | void init_SPI_ADXL345() { |
GerritPathuis | 0:a50c8bfe4605 | 108 | char readID; |
GerritPathuis | 2:4ed2dd2347f3 | 109 | int ox, oy, oz; |
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 | 2:4ed2dd2347f3 | 122 | ox = 13; // Offset X direction, not yet two's complement, (15.6 mg/LSB) |
GerritPathuis | 2:4ed2dd2347f3 | 123 | oy = 2; // Offset Y direction, not yet two's complement, (15.6 mg/LSB) |
GerritPathuis | 2:4ed2dd2347f3 | 124 | oz = 3; // Offset Z direction, not yet two's complement, (15.6 mg/LSB) |
GerritPathuis | 2:4ed2dd2347f3 | 125 | |
GerritPathuis | 2:4ed2dd2347f3 | 126 | oneByteWrite(0x1E, -ox); // Offset X in two's complement |
GerritPathuis | 2:4ed2dd2347f3 | 127 | oneByteWrite(0x1F, -oy); // Offset Y in two's complement |
GerritPathuis | 2:4ed2dd2347f3 | 128 | oneByteWrite(0x20, -oz); // Offset Z in two's complement |
GerritPathuis | 2:4ed2dd2347f3 | 129 | |
GerritPathuis | 0:a50c8bfe4605 | 130 | oneByteWrite(0x2D, 0x00); // PowerControl, Go into standby mode to configure the device. |
GerritPathuis | 0:a50c8bfe4605 | 131 | oneByteWrite(0x2E, 0x00); // InterruptEnableControl, disable all interrupts |
GerritPathuis | 0:a50c8bfe4605 | 132 | |
GerritPathuis | 0:a50c8bfe4605 | 133 | oneByteWrite(0x1D, 0x01); // TRESH_TAP is set to 1 |
GerritPathuis | 0:a50c8bfe4605 | 134 | oneByteWrite(0x21, 0x00); // DUR disable |
GerritPathuis | 0:a50c8bfe4605 | 135 | oneByteWrite(0x22, 0x00); // LATENT disable |
GerritPathuis | 0:a50c8bfe4605 | 136 | oneByteWrite(0x23, 0x00); // WINDOW disable |
GerritPathuis | 0:a50c8bfe4605 | 137 | oneByteWrite(0x24, 0x01); // TRESH_ACT is set to 1 |
GerritPathuis | 0:a50c8bfe4605 | 138 | oneByteWrite(0x25, 0x01); // TRESH_INACT is set to 1 |
GerritPathuis | 0:a50c8bfe4605 | 139 | oneByteWrite(0x26, 0x01); // TIME_INACT is set to 1 |
GerritPathuis | 0:a50c8bfe4605 | 140 | oneByteWrite(0x27, 0x00); // ACT_INACT_CTL disable |
GerritPathuis | 0:a50c8bfe4605 | 141 | oneByteWrite(0x28, 0x01); // TRESF_FF is set to 1 |
GerritPathuis | 0:a50c8bfe4605 | 142 | oneByteWrite(0x29, 0x01); // TIME_FF is set to 1 |
GerritPathuis | 0:a50c8bfe4605 | 143 | oneByteWrite(0x2A, 0x00); // TAP_AXES disable |
GerritPathuis | 0:a50c8bfe4605 | 144 | |
GerritPathuis | 2:4ed2dd2347f3 | 145 | oneByteWrite(0x31, 0x0B); // setDataFormatControl, 4-wire SPI, Full res, Right justify, +/-16g, 4 mg/LSB. |
GerritPathuis | 0:a50c8bfe4605 | 146 | oneByteWrite(0x2F, 0x00); // setInterruptMappingControl, send ALL interrupt to pin INT1 |
GerritPathuis | 0:a50c8bfe4605 | 147 | oneByteWrite(0x38, 0x01); // setFifoControl, Bypass, Int pin 1, 1 sample |
GerritPathuis | 0:a50c8bfe4605 | 148 | oneByteWrite(0x2C, 0x0F); // setDataRate, 3.2kHz data rate |
GerritPathuis | 0:a50c8bfe4605 | 149 | oneByteWrite(0x2D, 0x00); // setPowerControl, measurement mode (start measuring) (se page 17 sleep bit) |
GerritPathuis | 0:a50c8bfe4605 | 150 | oneByteWrite(0x2D, 0x08); // setPowerControl, measurement mode (start measuring) |
GerritPathuis | 0:a50c8bfe4605 | 151 | oneByteWrite(0x2E, 0x80); // setInterruptEnableControl, enable interrupt DATA_READY |
GerritPathuis | 0:a50c8bfe4605 | 152 | } |
GerritPathuis | 0:a50c8bfe4605 | 153 | |
GerritPathuis | 0:a50c8bfe4605 | 154 | int oneByteRead(int address) { |
GerritPathuis | 0:a50c8bfe4605 | 155 | int tx = (ADXL345_SPI_READ | (address & 0x3F)); |
GerritPathuis | 0:a50c8bfe4605 | 156 | int rx = 0; |
GerritPathuis | 0:a50c8bfe4605 | 157 | |
GerritPathuis | 0:a50c8bfe4605 | 158 | cs = 0; //Send address to read from. |
GerritPathuis | 0:a50c8bfe4605 | 159 | spi.write(tx); //Read back contents of address. |
GerritPathuis | 0:a50c8bfe4605 | 160 | rx = spi.write(0x00); |
GerritPathuis | 0:a50c8bfe4605 | 161 | cs = 1; |
GerritPathuis | 0:a50c8bfe4605 | 162 | return rx; |
GerritPathuis | 0:a50c8bfe4605 | 163 | } |
GerritPathuis | 0:a50c8bfe4605 | 164 | |
GerritPathuis | 0:a50c8bfe4605 | 165 | void oneByteWrite(int address, char data) { |
GerritPathuis | 0:a50c8bfe4605 | 166 | int tx = (ADXL345_SPI_WRITE | (address & 0x3F)); |
GerritPathuis | 0:a50c8bfe4605 | 167 | |
GerritPathuis | 0:a50c8bfe4605 | 168 | cs = 0; //Send address to write to. |
GerritPathuis | 0:a50c8bfe4605 | 169 | spi.write(tx); //Send data to be written. |
GerritPathuis | 0:a50c8bfe4605 | 170 | spi.write(data); |
GerritPathuis | 0:a50c8bfe4605 | 171 | cs = 1; |
GerritPathuis | 0:a50c8bfe4605 | 172 | } |
GerritPathuis | 0:a50c8bfe4605 | 173 | |
GerritPathuis | 0:a50c8bfe4605 | 174 | void multiByteRead(int startAddress, char* buffer, int size) { |
GerritPathuis | 0:a50c8bfe4605 | 175 | int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); |
GerritPathuis | 0:a50c8bfe4605 | 176 | |
GerritPathuis | 0:a50c8bfe4605 | 177 | cs = 0; //Send address to start reading from. |
GerritPathuis | 0:a50c8bfe4605 | 178 | spi.write(tx); |
GerritPathuis | 0:a50c8bfe4605 | 179 | for (int i = 0; i < size; i++) { |
GerritPathuis | 0:a50c8bfe4605 | 180 | buffer[i] = spi.write(0x00); |
GerritPathuis | 0:a50c8bfe4605 | 181 | } |
GerritPathuis | 0:a50c8bfe4605 | 182 | cs = 1; |
GerritPathuis | 0:a50c8bfe4605 | 183 | } |
GerritPathuis | 0:a50c8bfe4605 | 184 | |
GerritPathuis | 0:a50c8bfe4605 | 185 | void multiByteWrite(int startAddress, char* buffer, int size) { |
GerritPathuis | 0:a50c8bfe4605 | 186 | int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); |
GerritPathuis | 0:a50c8bfe4605 | 187 | |
GerritPathuis | 0:a50c8bfe4605 | 188 | cs = 0; //Send address to start reading from. |
GerritPathuis | 0:a50c8bfe4605 | 189 | spi.write(tx); |
GerritPathuis | 0:a50c8bfe4605 | 190 | for (int i = 0; i < size; i++) { |
GerritPathuis | 0:a50c8bfe4605 | 191 | buffer[i] = spi.write(0x00); |
GerritPathuis | 0:a50c8bfe4605 | 192 | } |
GerritPathuis | 0:a50c8bfe4605 | 193 | cs = 1; |
GerritPathuis | 0:a50c8bfe4605 | 194 | } |
GerritPathuis | 0:a50c8bfe4605 | 195 |