ALM Final Project
Dependencies: MMA8451Q SLCD_minus_mod mbed
Fork of ACC_LCD_341_Basic by
acc_341.cpp@6:328da59dfafc, 2016-11-28 (annotated)
- Committer:
- annalou
- Date:
- Mon Nov 28 01:29:12 2016 +0000
- Revision:
- 6:328da59dfafc
- Parent:
- 5:29c6ba524263
ALM_FINAL
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 | |
annalou | 6:328da59dfafc | 12 | #define DATAINTERVAL 0.150 |
scohennm | 4:1d559bac561a | 13 | #define LCDDATALEN 10 |
annalou | 6:328da59dfafc | 14 | #define LCDAXISLEN 3 |
scohennm | 0:203b4129a213 | 15 | |
scohennm | 3:c5291c70af48 | 16 | #define PROGNAME "ACCLCD341-541\r\n" |
scohennm | 0:203b4129a213 | 17 | |
scohennm | 0:203b4129a213 | 18 | #define PRINTDBUG |
scohennm | 2:6003ed409def | 19 | // |
scohennm | 0:203b4129a213 | 20 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 2:6003ed409def | 21 | PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. |
scohennm | 2:6003ed409def | 22 | PinName const SCL = PTE24; // DO NOT CHANGE |
scohennm | 0:203b4129a213 | 23 | #elif defined (TARGET_KL05Z) |
scohennm | 0:203b4129a213 | 24 | PinName const SDA = PTB4; |
scohennm | 0:203b4129a213 | 25 | PinName const SCL = PTB3; |
scohennm | 0:203b4129a213 | 26 | #else |
scohennm | 0:203b4129a213 | 27 | #error TARGET NOT DEFINED |
scohennm | 0:203b4129a213 | 28 | #endif |
scohennm | 0:203b4129a213 | 29 | |
scohennm | 0:203b4129a213 | 30 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:203b4129a213 | 31 | |
scohennm | 0:203b4129a213 | 32 | SLCD slcd; //define LCD display |
scohennm | 4:1d559bac561a | 33 | char lcdData[LCDDATALEN]; //buffer needs places dor decimal pt and colon |
annalou | 6:328da59dfafc | 34 | char lcdAxis[LCDAXISLEN]; |
scohennm | 0:203b4129a213 | 35 | |
scohennm | 0:203b4129a213 | 36 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:203b4129a213 | 37 | Serial pc(USBTX, USBRX); |
scohennm | 3:c5291c70af48 | 38 | Timer dataTimer; |
scohennm | 0:203b4129a213 | 39 | |
scohennm | 2:6003ed409def | 40 | |
scohennm | 0:203b4129a213 | 41 | |
scohennm | 3:c5291c70af48 | 42 | void LCDMess(char *lMess){ |
scohennm | 0:203b4129a213 | 43 | slcd.Home(); |
scohennm | 0:203b4129a213 | 44 | slcd.clear(); |
scohennm | 0:203b4129a213 | 45 | slcd.printf(lMess); |
annalou | 6:328da59dfafc | 46 | |
scohennm | 0:203b4129a213 | 47 | } |
scohennm | 4:1d559bac561a | 48 | |
annalou | 6:328da59dfafc | 49 | void LCDsignedFloat(float theNumber, Timer dataTime, char axis){ |
scohennm | 5:29c6ba524263 | 50 | sprintf (lcdData," %3.2f",theNumber); |
annalou | 6:328da59dfafc | 51 | if ((dataTime < 0.150) && (axis == 'X')) |
annalou | 6:328da59dfafc | 52 | { sprintf (lcdAxis,"%c", axis); |
annalou | 6:328da59dfafc | 53 | if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber));} |
annalou | 6:328da59dfafc | 54 | else if ((dataTime < 0.150) && (axis == 'Y')){ |
annalou | 6:328da59dfafc | 55 | sprintf (lcdAxis,"%c", axis); |
annalou | 6:328da59dfafc | 56 | if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber));} |
annalou | 6:328da59dfafc | 57 | else {sprintf (lcdAxis,"%c", axis); |
annalou | 6:328da59dfafc | 58 | if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber));}; |
annalou | 6:328da59dfafc | 59 | |
annalou | 6:328da59dfafc | 60 | |
scohennm | 4:1d559bac561a | 61 | LCDMess(lcdData); |
scohennm | 4:1d559bac561a | 62 | } |
scohennm | 4:1d559bac561a | 63 | |
scohennm | 3:c5291c70af48 | 64 | void initialize_global_vars(){ |
scohennm | 3:c5291c70af48 | 65 | pc.printf(PROGNAME); |
scohennm | 3:c5291c70af48 | 66 | dataTimer.start(); |
scohennm | 3:c5291c70af48 | 67 | dataTimer.reset(); |
scohennm | 3:c5291c70af48 | 68 | } |
scohennm | 0:203b4129a213 | 69 | |
scohennm | 0:203b4129a213 | 70 | int main() { |
scohennm | 0:203b4129a213 | 71 | float xAcc; |
scohennm | 3:c5291c70af48 | 72 | float yAcc; |
scohennm | 4:1d559bac561a | 73 | float zAcc; |
scohennm | 0:203b4129a213 | 74 | |
scohennm | 3:c5291c70af48 | 75 | initialize_global_vars(); |
scohennm | 0:203b4129a213 | 76 | // main loop forever |
scohennm | 0:203b4129a213 | 77 | while(true) { |
annalou | 6:328da59dfafc | 78 | if(dataTimer.read() > DATAINTERVAL){ |
scohennm | 0:203b4129a213 | 79 | //Get accelerometer data - tilt angles minus offset for zero mark. |
scohennm | 3:c5291c70af48 | 80 | xAcc = acc.getAccX(); |
annalou | 6:328da59dfafc | 81 | LCDsignedFloat(xAcc, dataTimer, 'X'); |
annalou | 6:328da59dfafc | 82 | dataTimer.reset(); |
annalou | 6:328da59dfafc | 83 | yAcc = acc.getAccY(); |
annalou | 6:328da59dfafc | 84 | LCDsignedFloat(yAcc, dataTimer, 'Y'); |
annalou | 6:328da59dfafc | 85 | dataTimer.reset(); |
scohennm | 4:1d559bac561a | 86 | zAcc = acc.getAccZ(); |
annalou | 6:328da59dfafc | 87 | LCDsignedFloat(zAcc, dataTimer, 'Z'); |
annalou | 6:328da59dfafc | 88 | dataTimer.reset(); |
scohennm | 0:203b4129a213 | 89 | |
scohennm | 0:203b4129a213 | 90 | #ifdef PRINTDBUG |
scohennm | 1:9340a340e588 | 91 | pc.printf("xAcc = %f\r\n", xAcc); |
scohennm | 1:9340a340e588 | 92 | pc.printf("yAcc = %f\r\n", yAcc); |
scohennm | 4:1d559bac561a | 93 | pc.printf("zAcc = %f\r\n", zAcc); |
scohennm | 0:203b4129a213 | 94 | #endif |
scohennm | 0:203b4129a213 | 95 | // Wait then do the whole thing again. |
scohennm | 3:c5291c70af48 | 96 | } |
scohennm | 0:203b4129a213 | 97 | } |
scohennm | 0:203b4129a213 | 98 | } |