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.
ADXL.cpp
00001 #include "ADXL.h" 00002 00003 SPI acc(p11, p12, p13); 00004 DigitalOut cs(p10); 00005 char buffer[6]; 00006 int16_t tempData[3]; 00007 float x, y, z; 00008 00009 void accConfig() { 00010 cs=1; // initially ADXL345 is not activated 00011 acc.format(8,3); // 8 bit data, Mode 3 00012 acc.frequency(2000000); // 2MHz clock rate 00013 cs=0; //select the device 00014 acc.write(0x31); // data format register 00015 acc.write(0x0B); // format +/-16g, 0.004g/LSB 00016 cs=1; //end of transmission 00017 cs=0; //start a new transmission 00018 acc.write(0x2D); // power ctrl register 00019 acc.write(0x08); // measure mode 00020 cs=1; // end of transmission 00021 } 00022 00023 void getAccel(float* data) { 00024 wait(0.2); 00025 cs=0; //start a transmission 00026 acc.write(0x80|0x40|0x32); // RW bit high, MB bit high, plus address 00027 for (int i = 0;i<=5;i++) { 00028 buffer[i]=acc.write(0x00); // read back 6 data bytes 00029 } 00030 cs=1; //end of transmission 00031 tempData[0] = buffer[1] << 8 | buffer[0]; //combine MSB and LSB 00032 tempData[1] = buffer[3] << 8 | buffer[2]; 00033 tempData[2] = buffer[5] << 8 | buffer[4]; 00034 x = tempData[0]*0.004; 00035 y = tempData[1]*0.004; 00036 z = tempData[2]*0.004; 00037 data[0] = x; 00038 data[1] = y; 00039 data[2] = z; 00040 00041 }
Generated on Fri Jul 15 2022 22:03:38 by
1.7.2