![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
main.cpp@0:dbea5fa1cd2c, 2011-10-17 (annotated)
- Committer:
- gbeardall
- Date:
- Mon Oct 17 10:40:49 2011 +0000
- Revision:
- 0:dbea5fa1cd2c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gbeardall | 0:dbea5fa1cd2c | 1 | #include "ADXL345_I2C.h" |
gbeardall | 0:dbea5fa1cd2c | 2 | |
gbeardall | 0:dbea5fa1cd2c | 3 | ADXL345_I2C accelerometer(p28, p27); |
gbeardall | 0:dbea5fa1cd2c | 4 | Serial pc(USBTX, USBRX); |
gbeardall | 0:dbea5fa1cd2c | 5 | |
gbeardall | 0:dbea5fa1cd2c | 6 | int main() { |
gbeardall | 0:dbea5fa1cd2c | 7 | pc.baud(9600); //115200); |
gbeardall | 0:dbea5fa1cd2c | 8 | int readings[3] = {0, 0, 0}; |
gbeardall | 0:dbea5fa1cd2c | 9 | |
gbeardall | 0:dbea5fa1cd2c | 10 | pc.printf("Starting ADXL345 test...\n\r"); |
gbeardall | 0:dbea5fa1cd2c | 11 | wait(.001); |
gbeardall | 0:dbea5fa1cd2c | 12 | pc.printf("Device ID is: 0x%02x\n\r", accelerometer.getDeviceID()); |
gbeardall | 0:dbea5fa1cd2c | 13 | wait(.001); |
gbeardall | 0:dbea5fa1cd2c | 14 | |
gbeardall | 0:dbea5fa1cd2c | 15 | // These are here to test whether any of the initialization fails. It will print the failure |
gbeardall | 0:dbea5fa1cd2c | 16 | if (accelerometer.setPowerControl(0x00)){ |
gbeardall | 0:dbea5fa1cd2c | 17 | pc.printf("didn't intitialize power control\n\r"); |
gbeardall | 0:dbea5fa1cd2c | 18 | return 0; } |
gbeardall | 0:dbea5fa1cd2c | 19 | //Full resolution, +/-16g, 4mg/LSB. |
gbeardall | 0:dbea5fa1cd2c | 20 | wait(.001); |
gbeardall | 0:dbea5fa1cd2c | 21 | |
gbeardall | 0:dbea5fa1cd2c | 22 | if(accelerometer.setDataFormatControl(0x0B)){ |
gbeardall | 0:dbea5fa1cd2c | 23 | pc.printf("didn't set data format\n\r"); |
gbeardall | 0:dbea5fa1cd2c | 24 | return 0; } |
gbeardall | 0:dbea5fa1cd2c | 25 | wait(.001); |
gbeardall | 0:dbea5fa1cd2c | 26 | |
gbeardall | 0:dbea5fa1cd2c | 27 | //3.2kHz data rate. |
gbeardall | 0:dbea5fa1cd2c | 28 | if(accelerometer.setDataRate(ADXL345_3200HZ)){ |
gbeardall | 0:dbea5fa1cd2c | 29 | pc.printf("didn't set data rate\n\r"); |
gbeardall | 0:dbea5fa1cd2c | 30 | return 0; } |
gbeardall | 0:dbea5fa1cd2c | 31 | wait(.001); |
gbeardall | 0:dbea5fa1cd2c | 32 | |
gbeardall | 0:dbea5fa1cd2c | 33 | //Measurement mode. |
gbeardall | 0:dbea5fa1cd2c | 34 | |
gbeardall | 0:dbea5fa1cd2c | 35 | if(accelerometer.setPowerControl(MeasurementMode)) { |
gbeardall | 0:dbea5fa1cd2c | 36 | pc.printf("didn't set the power control to measurement\n\r"); |
gbeardall | 0:dbea5fa1cd2c | 37 | return 0; } |
gbeardall | 0:dbea5fa1cd2c | 38 | |
gbeardall | 0:dbea5fa1cd2c | 39 | while (1) { |
gbeardall | 0:dbea5fa1cd2c | 40 | |
gbeardall | 0:dbea5fa1cd2c | 41 | wait(.5); |
gbeardall | 0:dbea5fa1cd2c | 42 | |
gbeardall | 0:dbea5fa1cd2c | 43 | accelerometer.getOutput(readings); |
gbeardall | 0:dbea5fa1cd2c | 44 | |
gbeardall | 0:dbea5fa1cd2c | 45 | |
gbeardall | 0:dbea5fa1cd2c | 46 | pc.printf("%i, %i, %i\n\r", (int16_t)readings[0], (int16_t)readings[1], (int16_t)readings[2]); |
gbeardall | 0:dbea5fa1cd2c | 47 | } |
gbeardall | 0:dbea5fa1cd2c | 48 | |
gbeardall | 0:dbea5fa1cd2c | 49 | } |