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.
Fork of ADXL345 by
main.cpp
00001 #include "mbed.h" 00002 #include "ADXL345.h" 00003 00004 ADXL345 accelerometer(p5, p6, p7, p8); // MOSI, MISO, SCK, CS_bar 00005 Serial pc(USBTX, USBRX); 00006 // 11-bit extented values; 50 readings stored in an array. 00007 int data[50][3]; 00008 int reading = 0; 00009 int main() { 00010 00011 int readings[3] = {0, 0, 0}; 00012 00013 pc.printf("\nStarting ADXL345 test...\n"); 00014 pc.printf("Device ID is: 0x%02x\n\r", accelerometer.getDevId()); 00015 //Go into standby mode to configure the device. 00016 accelerometer.setPowerControl(0x00); 00017 00018 //Full resolution, +/-4g, 4mg/LSB. 00019 accelerometer.setDataFormatControl(0x09); 00020 00021 //3.2kHz data rate. 00022 accelerometer.setDataRate(ADXL345_3200HZ); 00023 00024 //Measurement mode. 00025 accelerometer.setPowerControl(0x08); 00026 00027 // Create timers 00028 Timer Write_time; 00029 Write_time.start(); 00030 00031 //Fill the data with values. 00032 for (int i=0; i<50; i++){ 00033 // Create timers for counting sampling time, and hence sampling rate. 00034 // Timer Sample_time; 00035 // Sample_time.start(); 00036 accelerometer.getOutput(readings); 00037 // Sample_time.stop(); 00038 // pc.printf("\nTime taken to take one sample= %f seconds\n\r",Sample_time.read()); 00039 00040 for (int j=0; j<3; j++){ 00041 data[i][j] = readings[j]; 00042 } 00043 } 00044 00045 Write_time.stop(); 00046 00047 pc.printf("\nTime taken to write array= %f seconds\n\r",Write_time.read()); 00048 00049 // Create timers 00050 Timer Read_time; 00051 Read_time.start(); 00052 // Write to PC 00053 for (int i=0; i<50; i++){ 00054 reading++; 00055 //11-bit, sign extended values. 00056 pc.printf("\nAx=%i, Ay=%i, Az=%i\n", (int16_t)data[i][0], (int16_t)data[i][1], (int16_t)data[i][2]); 00057 pc.printf ("reading %d\n\r", reading); 00058 } 00059 Read_time.stop(); 00060 pc.printf("\nTime taken to read array= %f seconds\n\r",Read_time.read()); 00061 00062 } 00063
Generated on Tue Aug 9 2022 18:38:32 by
1.7.2
