Augusto Panecatl
/
I2C_ACC
I2C Accelerometer code example for FRDM-KL25Z
main.cpp@0:64f44dd118c8, 2014-06-04 (annotated)
- Committer:
- APanecatl
- Date:
- Wed Jun 04 19:24:11 2014 +0000
- Revision:
- 0:64f44dd118c8
Rev 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
APanecatl | 0:64f44dd118c8 | 1 | #include "mbed.h" |
APanecatl | 0:64f44dd118c8 | 2 | #include "MMA8451Q.h" |
APanecatl | 0:64f44dd118c8 | 3 | |
APanecatl | 0:64f44dd118c8 | 4 | PinName const SDA = PTE25; |
APanecatl | 0:64f44dd118c8 | 5 | PinName const SCL = PTE24; |
APanecatl | 0:64f44dd118c8 | 6 | |
APanecatl | 0:64f44dd118c8 | 7 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
APanecatl | 0:64f44dd118c8 | 8 | |
APanecatl | 0:64f44dd118c8 | 9 | int main(void) { |
APanecatl | 0:64f44dd118c8 | 10 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
APanecatl | 0:64f44dd118c8 | 11 | PwmOut rled(LED1); |
APanecatl | 0:64f44dd118c8 | 12 | PwmOut gled(LED2); |
APanecatl | 0:64f44dd118c8 | 13 | PwmOut bled(LED3); |
APanecatl | 0:64f44dd118c8 | 14 | |
APanecatl | 0:64f44dd118c8 | 15 | printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
APanecatl | 0:64f44dd118c8 | 16 | |
APanecatl | 0:64f44dd118c8 | 17 | while (true) |
APanecatl | 0:64f44dd118c8 | 18 | { |
APanecatl | 0:64f44dd118c8 | 19 | float x, y, z; |
APanecatl | 0:64f44dd118c8 | 20 | x = rled = 1.0 - abs(acc.getAccX()); |
APanecatl | 0:64f44dd118c8 | 21 | y = gled = 1.0 - abs(acc.getAccY()); |
APanecatl | 0:64f44dd118c8 | 22 | z = bled = 1.0 - abs(acc.getAccZ()); |
APanecatl | 0:64f44dd118c8 | 23 | wait(0.1); |
APanecatl | 0:64f44dd118c8 | 24 | printf("X: %1.2f, Y: %1.2f, Z: %1.2f\n", x, y, z); |
APanecatl | 0:64f44dd118c8 | 25 | } |
APanecatl | 0:64f44dd118c8 | 26 | } |