Shubhojit Chattopadhyay
/
xbee_base
Code for Base Module
main.cpp@0:7cf9c0ff4ce8, 2011-02-27 (annotated)
- Committer:
- buntyshubho
- Date:
- Sun Feb 27 06:11:25 2011 +0000
- Revision:
- 0:7cf9c0ff4ce8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
buntyshubho | 0:7cf9c0ff4ce8 | 1 | //Base Unit containing the accelerometer and the Xbee Module |
buntyshubho | 0:7cf9c0ff4ce8 | 2 | #include "mbed.h" |
buntyshubho | 0:7cf9c0ff4ce8 | 3 | #include "ADXL345.h" |
buntyshubho | 0:7cf9c0ff4ce8 | 4 | |
buntyshubho | 0:7cf9c0ff4ce8 | 5 | ADXL345 accelerometer(p5, p6, p7, p8); |
buntyshubho | 0:7cf9c0ff4ce8 | 6 | Serial pc(USBTX, USBRX); |
buntyshubho | 0:7cf9c0ff4ce8 | 7 | |
buntyshubho | 0:7cf9c0ff4ce8 | 8 | Serial xbee1(p9, p10); |
buntyshubho | 0:7cf9c0ff4ce8 | 9 | DigitalOut rst1(p11); |
buntyshubho | 0:7cf9c0ff4ce8 | 10 | |
buntyshubho | 0:7cf9c0ff4ce8 | 11 | |
buntyshubho | 0:7cf9c0ff4ce8 | 12 | DigitalOut myled(LED3);//Create variable for Led 3 on the mbed |
buntyshubho | 0:7cf9c0ff4ce8 | 13 | |
buntyshubho | 0:7cf9c0ff4ce8 | 14 | int main() { |
buntyshubho | 0:7cf9c0ff4ce8 | 15 | |
buntyshubho | 0:7cf9c0ff4ce8 | 16 | // reset the xbees (at least 200ns) |
buntyshubho | 0:7cf9c0ff4ce8 | 17 | rst1 = 0; |
buntyshubho | 0:7cf9c0ff4ce8 | 18 | // rst2 = 0; |
buntyshubho | 0:7cf9c0ff4ce8 | 19 | wait_ms(1); |
buntyshubho | 0:7cf9c0ff4ce8 | 20 | rst1 = 1; |
buntyshubho | 0:7cf9c0ff4ce8 | 21 | // rst2 = 1; |
buntyshubho | 0:7cf9c0ff4ce8 | 22 | wait_ms(1); |
buntyshubho | 0:7cf9c0ff4ce8 | 23 | |
buntyshubho | 0:7cf9c0ff4ce8 | 24 | int readings[3] = {0, 0, 0}; |
buntyshubho | 0:7cf9c0ff4ce8 | 25 | |
buntyshubho | 0:7cf9c0ff4ce8 | 26 | pc.printf("Starting ADXL345 test...\n"); |
buntyshubho | 0:7cf9c0ff4ce8 | 27 | pc.printf("Device ID is: 0x%02x\n", accelerometer.getDevId()); |
buntyshubho | 0:7cf9c0ff4ce8 | 28 | |
buntyshubho | 0:7cf9c0ff4ce8 | 29 | //Go into standby mode to configure the device. |
buntyshubho | 0:7cf9c0ff4ce8 | 30 | accelerometer.setPowerControl(0x00); |
buntyshubho | 0:7cf9c0ff4ce8 | 31 | |
buntyshubho | 0:7cf9c0ff4ce8 | 32 | //Full resolution, +/-16g, 4mg/LSB. |
buntyshubho | 0:7cf9c0ff4ce8 | 33 | accelerometer.setDataFormatControl(0x0B); |
buntyshubho | 0:7cf9c0ff4ce8 | 34 | |
buntyshubho | 0:7cf9c0ff4ce8 | 35 | //3.2kHz data rate. |
buntyshubho | 0:7cf9c0ff4ce8 | 36 | accelerometer.setDataRate(ADXL345_3200HZ); |
buntyshubho | 0:7cf9c0ff4ce8 | 37 | |
buntyshubho | 0:7cf9c0ff4ce8 | 38 | //Measurement mode. |
buntyshubho | 0:7cf9c0ff4ce8 | 39 | accelerometer.setPowerControl(0x08); |
buntyshubho | 0:7cf9c0ff4ce8 | 40 | |
buntyshubho | 0:7cf9c0ff4ce8 | 41 | while(1) { |
buntyshubho | 0:7cf9c0ff4ce8 | 42 | |
buntyshubho | 0:7cf9c0ff4ce8 | 43 | wait(0.5); |
buntyshubho | 0:7cf9c0ff4ce8 | 44 | |
buntyshubho | 0:7cf9c0ff4ce8 | 45 | accelerometer.getOutput(readings); |
buntyshubho | 0:7cf9c0ff4ce8 | 46 | |
buntyshubho | 0:7cf9c0ff4ce8 | 47 | //13-bit, sign extended values. |
buntyshubho | 0:7cf9c0ff4ce8 | 48 | pc.printf("%f, %f, %f\n\r", (float)readings[0]/65535, (float)readings[1]/65535, (float)readings[2]/65535); |
buntyshubho | 0:7cf9c0ff4ce8 | 49 | |
buntyshubho | 0:7cf9c0ff4ce8 | 50 | if ((float)readings[1]/65535 > 0.5) |
buntyshubho | 0:7cf9c0ff4ce8 | 51 | xbee1.putc('C'); //Forward |
buntyshubho | 0:7cf9c0ff4ce8 | 52 | if ((float)readings[1]/65535 < 0.5) |
buntyshubho | 0:7cf9c0ff4ce8 | 53 | xbee1.putc('D'); //Reverse |
buntyshubho | 0:7cf9c0ff4ce8 | 54 | myled = 1; |
buntyshubho | 0:7cf9c0ff4ce8 | 55 | wait_ms(100); |
buntyshubho | 0:7cf9c0ff4ce8 | 56 | myled=0; |
buntyshubho | 0:7cf9c0ff4ce8 | 57 | wait_ms(100); |
buntyshubho | 0:7cf9c0ff4ce8 | 58 | |
buntyshubho | 0:7cf9c0ff4ce8 | 59 | } |
buntyshubho | 0:7cf9c0ff4ce8 | 60 | } |