3 axis accel
Dependencies: MMA8451Q SLCD_minus_mod mbed
Fork of ACC_LCD_341_Basic by
acc_341.cpp@3:c5291c70af48, 2016-11-05 (annotated)
- Committer:
- scohennm
- Date:
- Sat Nov 05 23:46:00 2016 +0000
- Revision:
- 3:c5291c70af48
- Parent:
- 2:6003ed409def
- Child:
- 4:1d559bac561a
Added negative sign for display and updated data interval.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:203b4129a213 | 1 | #include "mbed.h" |
scohennm | 3:c5291c70af48 | 2 | #include <math.h> |
scohennm | 0:203b4129a213 | 3 | #include "MMA8451Q.h" |
scohennm | 0:203b4129a213 | 4 | #include "SLCD.h" |
scohennm | 0:203b4129a213 | 5 | |
scohennm | 0:203b4129a213 | 6 | /* |
scohennm | 0:203b4129a213 | 7 | Test of the accelerometer, digital I/O, on-board LCD screen. |
scohennm | 0:203b4129a213 | 8 | Looing at vector product of the x-y components of the accelerometer. |
scohennm | 0:203b4129a213 | 9 | Works pretty well. Still rough, program wise - sc 140710 |
scohennm | 0:203b4129a213 | 10 | */ |
scohennm | 0:203b4129a213 | 11 | |
scohennm | 3:c5291c70af48 | 12 | #define DATAINTERVAL 0.200 |
scohennm | 0:203b4129a213 | 13 | |
scohennm | 3:c5291c70af48 | 14 | #define PROGNAME "ACCLCD341-541\r\n" |
scohennm | 0:203b4129a213 | 15 | |
scohennm | 0:203b4129a213 | 16 | #define PRINTDBUG |
scohennm | 2:6003ed409def | 17 | // |
scohennm | 0:203b4129a213 | 18 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 2:6003ed409def | 19 | PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. |
scohennm | 2:6003ed409def | 20 | PinName const SCL = PTE24; // DO NOT CHANGE |
scohennm | 0:203b4129a213 | 21 | #elif defined (TARGET_KL05Z) |
scohennm | 0:203b4129a213 | 22 | PinName const SDA = PTB4; |
scohennm | 0:203b4129a213 | 23 | PinName const SCL = PTB3; |
scohennm | 0:203b4129a213 | 24 | #else |
scohennm | 0:203b4129a213 | 25 | #error TARGET NOT DEFINED |
scohennm | 0:203b4129a213 | 26 | #endif |
scohennm | 0:203b4129a213 | 27 | |
scohennm | 0:203b4129a213 | 28 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:203b4129a213 | 29 | |
scohennm | 0:203b4129a213 | 30 | SLCD slcd; //define LCD display |
scohennm | 0:203b4129a213 | 31 | |
scohennm | 0:203b4129a213 | 32 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:203b4129a213 | 33 | Serial pc(USBTX, USBRX); |
scohennm | 3:c5291c70af48 | 34 | Timer dataTimer; |
scohennm | 0:203b4129a213 | 35 | |
scohennm | 2:6003ed409def | 36 | |
scohennm | 0:203b4129a213 | 37 | |
scohennm | 3:c5291c70af48 | 38 | void LCDMess(char *lMess){ |
scohennm | 0:203b4129a213 | 39 | slcd.Home(); |
scohennm | 0:203b4129a213 | 40 | slcd.clear(); |
scohennm | 0:203b4129a213 | 41 | slcd.printf(lMess); |
scohennm | 0:203b4129a213 | 42 | } |
scohennm | 3:c5291c70af48 | 43 | void initialize_global_vars(){ |
scohennm | 3:c5291c70af48 | 44 | pc.printf(PROGNAME); |
scohennm | 3:c5291c70af48 | 45 | dataTimer.start(); |
scohennm | 3:c5291c70af48 | 46 | dataTimer.reset(); |
scohennm | 3:c5291c70af48 | 47 | } |
scohennm | 0:203b4129a213 | 48 | |
scohennm | 0:203b4129a213 | 49 | int main() { |
scohennm | 0:203b4129a213 | 50 | float xAcc; |
scohennm | 3:c5291c70af48 | 51 | float yAcc; |
scohennm | 0:203b4129a213 | 52 | char lcdData[10]; //buffer needs places dor decimal pt and colon |
scohennm | 0:203b4129a213 | 53 | |
scohennm | 3:c5291c70af48 | 54 | initialize_global_vars(); |
scohennm | 0:203b4129a213 | 55 | // main loop forever |
scohennm | 0:203b4129a213 | 56 | while(true) { |
scohennm | 3:c5291c70af48 | 57 | if(dataTimer.read() > DATAINTERVAL){ |
scohennm | 3:c5291c70af48 | 58 | dataTimer.reset(); |
scohennm | 0:203b4129a213 | 59 | //Get accelerometer data - tilt angles minus offset for zero mark. |
scohennm | 3:c5291c70af48 | 60 | xAcc = acc.getAccX(); |
scohennm | 3:c5291c70af48 | 61 | yAcc = acc.getAccY(); |
scohennm | 0:203b4129a213 | 62 | |
scohennm | 0:203b4129a213 | 63 | #ifdef PRINTDBUG |
scohennm | 1:9340a340e588 | 64 | pc.printf("xAcc = %f\r\n", xAcc); |
scohennm | 1:9340a340e588 | 65 | pc.printf("yAcc = %f\r\n", yAcc); |
scohennm | 0:203b4129a213 | 66 | #endif |
scohennm | 0:203b4129a213 | 67 | |
scohennm | 3:c5291c70af48 | 68 | sprintf (lcdData,"%3.2f",xAcc); |
scohennm | 3:c5291c70af48 | 69 | if (xAcc < 0) sprintf (lcdData,">%3.2f",fabs(xAcc)); |
scohennm | 3:c5291c70af48 | 70 | LCDMess(lcdData); |
scohennm | 0:203b4129a213 | 71 | // Wait then do the whole thing again. |
scohennm | 3:c5291c70af48 | 72 | } |
scohennm | 0:203b4129a213 | 73 | } |
scohennm | 0:203b4129a213 | 74 | } |