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 /*Program Example 7.4: Reads values from accelerometer through SPI, and outputs 00002 continuously to terminal screen. 00003 */ 00004 00005 #include "mbed.h" 00006 SPI acc(p11,p12,p13); // set up SPI interface on pins 11,12,13 00007 DigitalOut cs(p14); // use pin 14 as chip select 00008 Serial pc(USBTX, USBRX); // set up USB interface to host terminal 00009 char buffer[6]; //raw data array type char 00010 int16_t data[3]; // 16-bit twos-complement integer data 00011 float x, y, z; // floating point data, to be displayed on-screen 00012 00013 int main() { 00014 cs=1; //initially ADXL345 is not activated 00015 acc.format(8,3); // 8 bit data, Mode 3 00016 acc.frequency(2000000); // 2MHz clock rate 00017 cs=0; //select the device 00018 acc.write(0x31); // data format register 00019 acc.write(0x0B); // format +/-16g, 0.004g/LSB 00020 cs=1; //end of transmission 00021 cs=0; //start a new transmission 00022 acc.write(0x2D); // power ctrl register 00023 acc.write(0x08); // measure mode 00024 cs=1; //end of transmission 00025 while (1) { // infinite loop 00026 wait(0.2); 00027 cs=0; //start a transmission 00028 acc.write(0x80|0x40|0x32); // RW bit high, MB bit high, plus address 00029 for (int i = 0;i<=5;i++) { 00030 buffer[i]=acc.write(0x00); // read back 6 data bytes 00031 } 00032 cs=1; //end of transmission 00033 data[0] = buffer[1]<<8 | buffer[0]; // combine MSB and LSB 00034 data[1] = buffer[3]<<8 | buffer[2]; 00035 data[2] = buffer[5]<<8 | buffer[4]; 00036 x=0.004*data[0]; y=0.004*data[1]; z=0.004*data[2]; // convert to float, 00037 //actual g value 00038 pc.printf("x = %+1.2fg\t y = %+1.2fg\t z = %+1.2fg\n\r", x, y,z); //print 00039 } 00040 } 00041
Generated on Sat Aug 13 2022 21:24:10 by
1.7.2