Peter Swanson / ADXL345_I2C参考 http://mbed.org/users/peterswanson87/code/ADXL345_I2C/
Fork of ADXL345_I2C by
Revision 2:84e6d627c619, committed 2013-08-01
- Comitter:
- emanon
- Date:
- Thu Aug 01 08:13:05 2013 +0000
- Parent:
- 1:d9412b56f98a
- Commit message:
- ADXL354_I2C ???????
Changed in this revision
--- a/ADXL345_I2C.cpp Thu May 12 01:19:36 2011 +0000
+++ b/ADXL345_I2C.cpp Thu Aug 01 08:13:05 2013 +0000
@@ -68,7 +68,7 @@
char rx[2];
rx[0] = ADXL345_DATA_FORMAT_REG;
rx[1] = 0x0B;
- // full res and +_16g
+ // full res and +_16g
i2c_.write( ADXL345_I2C_WRITE , rx, 2);
// Set Offset - programmed into the OFSX, OFSY, and OFXZ registers, respectively, as 0xFD, 0x03 and 0xFE.
--- a/ADXL345_I2C.h Thu May 12 01:19:36 2011 +0000 +++ b/ADXL345_I2C.h Thu Aug 01 08:13:05 2013 +0000 @@ -109,7 +109,7 @@ #define ADXL345_I2C_ADDRESS 0x53 //the ADXL345 7-bit address is 0x53 when ALT ADDRESS is low as it is on the sparkfun chip: when ALT ADDRESS is high the address is 0x1D /////////////when ALT ADDRESS pin is high: -//#define ADXL345_I2C_READ 0x3B +//#define ADXL345_I2C_READ 0x3B //#define ADXL345_I2C_WRITE 0x3A //#define ADXL345_I2C_ADDRESS 0x1D
--- a/main.cpp Thu May 12 01:19:36 2011 +0000
+++ b/main.cpp Thu Aug 01 08:13:05 2013 +0000
@@ -1,49 +1,56 @@
#include "ADXL345_I2C.h"
- ADXL345_I2C accelerometer(p28, p27);
- Serial pc(USBTX, USBRX);
+DigitalOut myled(LED1);
+ADXL345_I2C accelerometer(p9, p10);
+Serial pc(USBTX, USBRX);
- int main() {
- pc.baud(115200);
- int readings[3] = {0, 0, 0};
-
- pc.printf("Starting ADXL345 test...\n");
- wait(.001);
- pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
+int main() {
+ pc.baud(115200);
+// pc.baud(9600);
+ myled = 1;
+
+ int readings[3] = {0, 0, 0};
+
+ pc.printf("Starting ADXL345 test...\n");
+ wait(.001);
+ pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
wait(.001);
- // These are here to test whether any of the initialization fails. It will print the failure
+ // These are here to test whether any of the initialization fails. It will print the failure
if (accelerometer.setPowerControl(0x00)){
- pc.printf("didn't intitialize power control\n");
- return 0; }
- //Full resolution, +/-16g, 4mg/LSB.
- wait(.001);
+ pc.printf("didn't intitialize power control\n");
+ return 0;
+ }
+ //Full resolution, +/-16g, 4mg/LSB.
+ wait(.001);
- if(accelerometer.setDataFormatControl(0x0B)){
+ if(accelerometer.setDataFormatControl(0x0B)){
pc.printf("didn't set data format\n");
return 0; }
- wait(.001);
-
- //3.2kHz data rate.
- if(accelerometer.setDataRate(ADXL345_3200HZ)){
+ wait(.001);
+
+ //3.2kHz data rate.
+ if(accelerometer.setDataRate(ADXL345_3200HZ)){
pc.printf("didn't set data rate\n");
- return 0; }
- wait(.001);
-
- //Measurement mode.
-
- if(accelerometer.setPowerControl(MeasurementMode)) {
+ return 0;
+ }
+ wait(.001);
+
+ //Measurement mode.
+
+ if(accelerometer.setPowerControl(MeasurementMode)) {
pc.printf("didn't set the power control to measurement\n");
- return 0; }
-
- while (1) {
-
+ return 0;
+ }
+ myled = 0;
+
+ while (1) {
wait(0.1);
accelerometer.getOutput(readings);
-
-
- pc.printf("%i, %i, %i\n", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]);
- }
-
- }
+ pc.printf("%+4.2f, %+4.2f, %+4.2f\n",
+ (float((int16_t)readings[0]+18)/256),
+ (float((int16_t)readings[1]-4 )/256),
+ (float((int16_t)readings[2]+22)/256));
+ }
+}
\ No newline at end of file
