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.
Dependencies: ADXL345 TextLCD mbed
main.cpp
00001 #include "mbed.h" 00002 #include "ADXL345.h" 00003 #include "TextLCD.h" 00004 00005 ADXL345 accelerometer(p5, p6, p7, p8); // (SDA, SDO, SCL, CS); 00006 Serial pc(USBTX, USBRX); //For raw data 00007 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); // rs, e, d4-d7 00008 00009 float x,y,z; 00010 00011 00012 int main() 00013 { 00014 00015 int readings[3] = {0, 0, 0}; 00016 00017 lcd.cls(); 00018 lcd.printf("Starting ADXL345 test...\n"); 00019 lcd.printf("Device ID is: 0x%02x\n", accelerometer.getDevId()); 00020 pc.printf("Starting ADXL345 test...\n"); 00021 pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId()); 00022 00023 //Go into standby mode to configure the device. 00024 accelerometer.setPowerControl(0x00); 00025 00026 //Full resolution, +/-4g, 3.9mg/LSB. 00027 accelerometer.setDataFormatControl(0x0B); 00028 00029 //3.2kHz data rate. 00030 accelerometer.setDataRate(ADXL345_3200HZ); 00031 00032 //Measurement mode. 00033 accelerometer.setPowerControl(0x08); 00034 00035 while (1) { 00036 00037 wait(0.1); //Intaval 00038 00039 accelerometer.getOutput(readings); 00040 00041 x=(int16_t)readings[0]; 00042 x=x*4e-3; 00043 y=(int16_t)readings[1]; 00044 y=y*4e-3; 00045 z=(int16_t)readings[2]; 00046 z=z*4e-3; 00047 00048 //13-bit, sign extended values. 00049 lcd.cls(); 00050 lcd.printf("%.1f, %.1f, %.1f\n", x,y,z); //Convertet to G 00051 lcd.printf(" X Y Z "); 00052 pc.printf("%i, %i, %i\n\r", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); //Raw data 00053 00054 } 00055 00056 }
Generated on Sat Aug 6 2022 01:22:24 by
1.7.2