BMA180 triple axis accelerometer example. https://www.sparkfun.com/commerce/product_info.php?products_id=9723
main.cpp@0:10e5fb9080cf, 2010-11-10 (annotated)
- Committer:
- mux
- Date:
- Wed Nov 10 20:13:23 2010 +0000
- Revision:
- 0:10e5fb9080cf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mux | 0:10e5fb9080cf | 1 | #include "mbed.h" |
mux | 0:10e5fb9080cf | 2 | #define BMA180_ID 0x00 |
mux | 0:10e5fb9080cf | 3 | #define BMA180_ADDR 0x80 //datasheet says 0x40 ! |
mux | 0:10e5fb9080cf | 4 | #define BMA180_X_ACC 0x02 |
mux | 0:10e5fb9080cf | 5 | #define BMA180_Y_ACC 0x04 |
mux | 0:10e5fb9080cf | 6 | #define BMA180_Z_ACC 0x06 |
mux | 0:10e5fb9080cf | 7 | #define BMA180_SDA p9 |
mux | 0:10e5fb9080cf | 8 | #define BMA180_SCL p10 |
mux | 0:10e5fb9080cf | 9 | I2C i2c(BMA180_SDA, BMA180_SCL); |
mux | 0:10e5fb9080cf | 10 | |
mux | 0:10e5fb9080cf | 11 | int main() { |
mux | 0:10e5fb9080cf | 12 | char data[2]; |
mux | 0:10e5fb9080cf | 13 | int16_t x,y,z; |
mux | 0:10e5fb9080cf | 14 | |
mux | 0:10e5fb9080cf | 15 | i2c.frequency(100000); |
mux | 0:10e5fb9080cf | 16 | |
mux | 0:10e5fb9080cf | 17 | //read chip_id |
mux | 0:10e5fb9080cf | 18 | data[0] = BMA180_ID; |
mux | 0:10e5fb9080cf | 19 | data[1] = 0x00; |
mux | 0:10e5fb9080cf | 20 | |
mux | 0:10e5fb9080cf | 21 | i2c.write(BMA180_ADDR, data, 1); |
mux | 0:10e5fb9080cf | 22 | i2c.read(BMA180_ADDR, data, 1); |
mux | 0:10e5fb9080cf | 23 | |
mux | 0:10e5fb9080cf | 24 | if (data[0] != 0x03) { |
mux | 0:10e5fb9080cf | 25 | printf("invalid chip id %d\n", data[0]); |
mux | 0:10e5fb9080cf | 26 | return 1; |
mux | 0:10e5fb9080cf | 27 | } |
mux | 0:10e5fb9080cf | 28 | |
mux | 0:10e5fb9080cf | 29 | while(1) { |
mux | 0:10e5fb9080cf | 30 | data[0] = BMA180_X_ACC; |
mux | 0:10e5fb9080cf | 31 | i2c.write(BMA180_ADDR, data, 1); |
mux | 0:10e5fb9080cf | 32 | i2c.read(BMA180_ADDR, data, 2); |
mux | 0:10e5fb9080cf | 33 | data[0] >>= 2; |
mux | 0:10e5fb9080cf | 34 | x = *((int16_t*)data); |
mux | 0:10e5fb9080cf | 35 | |
mux | 0:10e5fb9080cf | 36 | data[0] = BMA180_Y_ACC; |
mux | 0:10e5fb9080cf | 37 | i2c.write(BMA180_ADDR, data, 1); |
mux | 0:10e5fb9080cf | 38 | i2c.read(BMA180_ADDR, data, 2); |
mux | 0:10e5fb9080cf | 39 | data[0] >>= 2; |
mux | 0:10e5fb9080cf | 40 | y = *((int16_t*)data); |
mux | 0:10e5fb9080cf | 41 | |
mux | 0:10e5fb9080cf | 42 | data[0] = BMA180_Z_ACC; |
mux | 0:10e5fb9080cf | 43 | i2c.write(BMA180_ADDR, data, 1); |
mux | 0:10e5fb9080cf | 44 | i2c.read(BMA180_ADDR, data, 2); |
mux | 0:10e5fb9080cf | 45 | data[0] >>= 2; |
mux | 0:10e5fb9080cf | 46 | z = *((int16_t*)data); |
mux | 0:10e5fb9080cf | 47 | |
mux | 0:10e5fb9080cf | 48 | //printf("x: %d y: %d z: %d\n", x, y, z); |
mux | 0:10e5fb9080cf | 49 | printf("x: %.1f y: %.1f z: %.1f\n", x/16384.0f, y/16384.0f, z/16384.0f); //2^14 |
mux | 0:10e5fb9080cf | 50 | |
mux | 0:10e5fb9080cf | 51 | wait_ms(100); |
mux | 0:10e5fb9080cf | 52 | } |
mux | 0:10e5fb9080cf | 53 | } |