Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* This program collects 30 samples from a Triple Axis 00002 * Accelerometer Breakout ADXL345 sensor (Sparkfun.com SEN-09836) 00003 * The program is connected to a mbed via 4-wire spi. 00004 * The data is collected via DATA_READY interrupt at maximum speed 00005 * of the sensor. 00006 */ 00007 00008 #include "mbed.h" 00009 #define NOSAMPLES 30 00010 #define DEVID 0x00 00011 #define ADXL345_SPI_READ 0x80 00012 #define ADXL345_SPI_WRITE 0x00 00013 #define ADXL345_MULTI_BYTE 0x60 00014 #define ADXL345_DATAX0_REG 0x32 00015 00016 /////////4-wire SPI/////////// 00017 //// Hardware connections //// 00018 // ADXL345 00019 // Signal ---------- mbed pin 00020 // Vcc ------------ mbed Vout 00021 // Gnd ------------ mbed Gnd 00022 // SDA ------------ mbed p5 00023 // SDO ------------ mbed p6 00024 // SCL ------------ mbed p7 00025 // CS ------------ mbed p8 00026 // INTI ------------ mbed p9 // ADXL345 gives a interrupt when ready 00027 ////////////////////////////// 00028 00029 SPI spi(p5,p6,p7); // mosi, miso, sclk 00030 DigitalOut cs(p8); // cs 00031 InterruptIn event(p9); 00032 Serial pc(USBTX, USBRX); 00033 00034 void int_service(void); 00035 void init_SPI_ADXL345(void); 00036 int oneByteRead(int address); 00037 void oneByteWrite(int address, char data); 00038 void multiByteRead(int startAddress, char* buffer, int size); 00039 void multiByteWrite(int startAddress, char* buffer, int size); 00040 00041 Timer timer; 00042 struct ttstamp { 00043 int x; 00044 int y; 00045 int z; 00046 short int time; 00047 } data [NOSAMPLES]; 00048 00049 int acc[3] = {0, 0, 0}; 00050 int interrupt_counter, tstamp; 00051 00052 int main() { 00053 int i; 00054 float xx, yy, zz; 00055 00056 pc.baud(9600); 00057 pc.format(8,Serial::None,1); 00058 00059 event.rise(&int_service); // set the interrupt handling 00060 init_SPI_ADXL345(); // init the ADXL345 sensor 00061 00062 ////////////////// get the data ////////////////////// 00063 interrupt_counter= -1; 00064 timer.start(); 00065 pc.printf("\n\rBussy collecting data\n\r "); 00066 while (interrupt_counter < (NOSAMPLES-1)) 00067 pc.printf("."); 00068 00069 ///////////////// present the data //////////////////// 00070 // Full scale 16g is 1024 positions 00071 pc.printf("\r"); 00072 for (i=0; i <NOSAMPLES; i++) { 00073 xx= data[i].x * 0.004; // Full-res, sign extended values, 4.0 mg/LSB 00074 yy= data[i].y * 0.004; 00075 zz= data[i].z * 0.004; 00076 00077 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); 00078 //pc.printf("Sample= %03d, x= %+06d, y=%+06d, z=%+06d \n\r", i, data[i].x, data[i].y, data[i].z); 00079 } 00080 } 00081 00082 ///////////////////// Interrupt service ///////////////////// 00083 ///////////////////// store data in array /////////////////// 00084 ///////////////////// clear interrups /////////////////////// 00085 void int_service(void) { 00086 char buffer[6]; 00087 00088 interrupt_counter += 1; 00089 if (interrupt_counter == (NOSAMPLES -1)) 00090 oneByteWrite(0x2E, 0x00); // InterruptEnableControl, disable all interrupts 00091 00092 multiByteRead(ADXL345_DATAX0_REG, buffer, 6); // read the DATAX,DATZ,DATAY register 00093 acc[0] = (int)buffer[1] << 8 | (int)buffer[0]; 00094 acc[1] = (int)buffer[3] << 8 | (int)buffer[2]; 00095 acc[2] = (int)buffer[5] << 8 | (int)buffer[4]; 00096 00097 tstamp= timer.read_us(); 00098 // stora data in array 00099 data[interrupt_counter].x = (int16_t) acc[0]; 00100 data[interrupt_counter].y = (int16_t) acc[1]; 00101 data[interrupt_counter].z = (int16_t) acc[2]; 00102 data[interrupt_counter].time = tstamp; 00103 00104 oneByteRead(0x30); // clear interrupt sources 00105 } 00106 00107 void init_SPI_ADXL345() { 00108 char readID; 00109 int ox, oy, oz; 00110 00111 spi.frequency(5000000); // max 5 mHz page 8 of the ADXL345 datasheet 00112 spi.format(8,3); 00113 wait(0.1); 00114 00115 readID = oneByteRead(DEVID); 00116 if (readID == 0xE5) 00117 pc.printf("\n\rConnected to ADXL345\n\r"); 00118 else 00119 pc.printf("Sorry not connected to ADXL345 !!!(readID= %d)\n\r", readID); 00120 00121 // set up ADXL345 00122 ox = 13; // Offset X direction, not yet two's complement, (15.6 mg/LSB) 00123 oy = 2; // Offset Y direction, not yet two's complement, (15.6 mg/LSB) 00124 oz = 3; // Offset Z direction, not yet two's complement, (15.6 mg/LSB) 00125 00126 oneByteWrite(0x1E, -ox); // Offset X in two's complement 00127 oneByteWrite(0x1F, -oy); // Offset Y in two's complement 00128 oneByteWrite(0x20, -oz); // Offset Z in two's complement 00129 00130 oneByteWrite(0x2D, 0x00); // PowerControl, Go into standby mode to configure the device. 00131 oneByteWrite(0x2E, 0x00); // InterruptEnableControl, disable all interrupts 00132 00133 oneByteWrite(0x1D, 0x01); // TRESH_TAP is set to 1 00134 oneByteWrite(0x21, 0x00); // DUR disable 00135 oneByteWrite(0x22, 0x00); // LATENT disable 00136 oneByteWrite(0x23, 0x00); // WINDOW disable 00137 oneByteWrite(0x24, 0x01); // TRESH_ACT is set to 1 00138 oneByteWrite(0x25, 0x01); // TRESH_INACT is set to 1 00139 oneByteWrite(0x26, 0x01); // TIME_INACT is set to 1 00140 oneByteWrite(0x27, 0x00); // ACT_INACT_CTL disable 00141 oneByteWrite(0x28, 0x01); // TRESF_FF is set to 1 00142 oneByteWrite(0x29, 0x01); // TIME_FF is set to 1 00143 oneByteWrite(0x2A, 0x00); // TAP_AXES disable 00144 00145 oneByteWrite(0x31, 0x0B); // setDataFormatControl, 4-wire SPI, Full res, Right justify, +/-16g, 4 mg/LSB. 00146 oneByteWrite(0x2F, 0x00); // setInterruptMappingControl, send ALL interrupt to pin INT1 00147 oneByteWrite(0x38, 0x01); // setFifoControl, Bypass, Int pin 1, 1 sample 00148 oneByteWrite(0x2C, 0x0F); // setDataRate, 3.2kHz data rate 00149 oneByteWrite(0x2D, 0x00); // setPowerControl, measurement mode (start measuring) (se page 17 sleep bit) 00150 oneByteWrite(0x2D, 0x08); // setPowerControl, measurement mode (start measuring) 00151 oneByteWrite(0x2E, 0x80); // setInterruptEnableControl, enable interrupt DATA_READY 00152 } 00153 00154 int oneByteRead(int address) { 00155 int tx = (ADXL345_SPI_READ | (address & 0x3F)); 00156 int rx = 0; 00157 00158 cs = 0; //Send address to read from. 00159 spi.write(tx); //Read back contents of address. 00160 rx = spi.write(0x00); 00161 cs = 1; 00162 return rx; 00163 } 00164 00165 void oneByteWrite(int address, char data) { 00166 int tx = (ADXL345_SPI_WRITE | (address & 0x3F)); 00167 00168 cs = 0; //Send address to write to. 00169 spi.write(tx); //Send data to be written. 00170 spi.write(data); 00171 cs = 1; 00172 } 00173 00174 void multiByteRead(int startAddress, char* buffer, int size) { 00175 int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); 00176 00177 cs = 0; //Send address to start reading from. 00178 spi.write(tx); 00179 for (int i = 0; i < size; i++) { 00180 buffer[i] = spi.write(0x00); 00181 } 00182 cs = 1; 00183 } 00184 00185 void multiByteWrite(int startAddress, char* buffer, int size) { 00186 int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); 00187 00188 cs = 0; //Send address to start reading from. 00189 spi.write(tx); 00190 for (int i = 0; i < size; i++) { 00191 buffer[i] = spi.write(0x00); 00192 } 00193 cs = 1; 00194 } 00195
Generated on Mon Jul 25 2022 04:02:21 by
1.7.2