v1
Dependencies: MMA8451Q SLCD_minus_mod mbed
Fork of ACC_LCD_341_Basic by
acc_341.cpp@11:40fda348bb5e, 2016-11-28 (annotated)
- Committer:
- dorian505
- Date:
- Mon Nov 28 05:18:40 2016 +0000
- Revision:
- 11:40fda348bb5e
- Parent:
- 10:3de03f06624e
final version
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" |
dorian505 | 10:3de03f06624e | 5 | #define NUMBUTS 2 |
dorian505 | 10:3de03f06624e | 6 | #define RBUT PTC3 |
dorian505 | 6:045defd0d877 | 7 | #define LBUT PTC12 // port addresses for left button |
dorian505 | 10:3de03f06624e | 8 | #define BUTTONTIME 0.1 |
scohennm | 0:203b4129a213 | 9 | /* |
scohennm | 0:203b4129a213 | 10 | Test of the accelerometer, digital I/O, on-board LCD screen. |
scohennm | 0:203b4129a213 | 11 | Looing at vector product of the x-y components of the accelerometer. |
scohennm | 0:203b4129a213 | 12 | Works pretty well. Still rough, program wise - sc 140710 |
scohennm | 0:203b4129a213 | 13 | */ |
scohennm | 0:203b4129a213 | 14 | |
scohennm | 3:c5291c70af48 | 15 | #define DATAINTERVAL 0.200 |
scohennm | 4:1d559bac561a | 16 | #define LCDDATALEN 10 |
dorian505 | 6:045defd0d877 | 17 | #define xAxis 0 |
dorian505 | 6:045defd0d877 | 18 | #define yAxis 1 |
dorian505 | 6:045defd0d877 | 19 | #define zAxis 2 |
dorian505 | 8:971f38affa78 | 20 | |
scohennm | 3:c5291c70af48 | 21 | #define PROGNAME "ACCLCD341-541\r\n" |
scohennm | 0:203b4129a213 | 22 | |
scohennm | 0:203b4129a213 | 23 | #define PRINTDBUG |
scohennm | 2:6003ed409def | 24 | // |
scohennm | 0:203b4129a213 | 25 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 2:6003ed409def | 26 | PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. |
scohennm | 2:6003ed409def | 27 | PinName const SCL = PTE24; // DO NOT CHANGE |
scohennm | 0:203b4129a213 | 28 | #elif defined (TARGET_KL05Z) |
scohennm | 0:203b4129a213 | 29 | PinName const SDA = PTB4; |
scohennm | 0:203b4129a213 | 30 | PinName const SCL = PTB3; |
scohennm | 0:203b4129a213 | 31 | #else |
scohennm | 0:203b4129a213 | 32 | #error TARGET NOT DEFINED |
scohennm | 0:203b4129a213 | 33 | #endif |
scohennm | 0:203b4129a213 | 34 | |
scohennm | 0:203b4129a213 | 35 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:203b4129a213 | 36 | |
scohennm | 0:203b4129a213 | 37 | SLCD slcd; //define LCD display |
scohennm | 4:1d559bac561a | 38 | char lcdData[LCDDATALEN]; //buffer needs places dor decimal pt and colon |
scohennm | 0:203b4129a213 | 39 | |
scohennm | 0:203b4129a213 | 40 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:203b4129a213 | 41 | Serial pc(USBTX, USBRX); |
scohennm | 3:c5291c70af48 | 42 | Timer dataTimer; |
dorian505 | 10:3de03f06624e | 43 | Timer ButtonTimer; |
dorian505 | 10:3de03f06624e | 44 | |
dorian505 | 10:3de03f06624e | 45 | int displayState; |
dorian505 | 10:3de03f06624e | 46 | DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; |
dorian505 | 10:3de03f06624e | 47 | DigitalIn button(LBUT); |
scohennm | 2:6003ed409def | 48 | |
scohennm | 0:203b4129a213 | 49 | |
scohennm | 3:c5291c70af48 | 50 | void LCDMess(char *lMess){ |
scohennm | 0:203b4129a213 | 51 | slcd.Home(); |
scohennm | 0:203b4129a213 | 52 | slcd.clear(); |
scohennm | 0:203b4129a213 | 53 | slcd.printf(lMess); |
scohennm | 0:203b4129a213 | 54 | } |
scohennm | 4:1d559bac561a | 55 | |
scohennm | 4:1d559bac561a | 56 | void LCDsignedFloat(float theNumber){ |
scohennm | 5:29c6ba524263 | 57 | sprintf (lcdData," %3.2f",theNumber); |
scohennm | 5:29c6ba524263 | 58 | if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber)); |
scohennm | 4:1d559bac561a | 59 | LCDMess(lcdData); |
scohennm | 4:1d559bac561a | 60 | } |
scohennm | 4:1d559bac561a | 61 | |
scohennm | 3:c5291c70af48 | 62 | void initialize_global_vars(){ |
scohennm | 3:c5291c70af48 | 63 | pc.printf(PROGNAME); |
scohennm | 3:c5291c70af48 | 64 | dataTimer.start(); |
scohennm | 3:c5291c70af48 | 65 | dataTimer.reset(); |
scohennm | 3:c5291c70af48 | 66 | } |
scohennm | 0:203b4129a213 | 67 | |
scohennm | 0:203b4129a213 | 68 | int main() { |
scohennm | 0:203b4129a213 | 69 | float xAcc; |
scohennm | 3:c5291c70af48 | 70 | float yAcc; |
dorian505 | 6:045defd0d877 | 71 | float zAcc; |
dorian505 | 6:045defd0d877 | 72 | xAcc = acc.getAccX(); |
dorian505 | 6:045defd0d877 | 73 | yAcc = acc.getAccY(); |
dorian505 | 7:ae5c27eb1492 | 74 | zAcc = acc.getAccZ(); |
scohennm | 0:203b4129a213 | 75 | |
scohennm | 3:c5291c70af48 | 76 | initialize_global_vars(); |
scohennm | 0:203b4129a213 | 77 | // main loop forever |
dorian505 | 6:045defd0d877 | 78 | //Get accelerometer data - tilt angles minus offset for zero mark. |
dorian505 | 9:b2768bb45a68 | 79 | while (true) { |
dorian505 | 8:971f38affa78 | 80 | if (ButtonTimer > BUTTONTIME){ |
dorian505 | 10:3de03f06624e | 81 | //buttonValue = !buttons[1].read(); |
dorian505 | 11:40fda348bb5e | 82 | //pc.printf("%d \r\n",buttonValue); |
dorian505 | 8:971f38affa78 | 83 | for (int i=0; i < NUMBUTS; i++){ // index will be 0 or 1 |
dorian505 | 8:971f38affa78 | 84 | if(!buttons[i]) { |
dorian505 | 9:b2768bb45a68 | 85 | displayState = i; |
dorian505 | 6:045defd0d877 | 86 | switch (displayState){ // this keeps things generic |
dorian505 | 6:045defd0d877 | 87 | case xAxis: |
dorian505 | 6:045defd0d877 | 88 | LCDsignedFloat(xAcc); |
dorian505 | 6:045defd0d877 | 89 | break; |
dorian505 | 6:045defd0d877 | 90 | case yAxis: |
dorian505 | 6:045defd0d877 | 91 | LCDsignedFloat(yAcc); |
dorian505 | 6:045defd0d877 | 92 | break; |
dorian505 | 6:045defd0d877 | 93 | case zAxis: |
dorian505 | 6:045defd0d877 | 94 | LCDsignedFloat(zAcc); |
dorian505 | 6:045defd0d877 | 95 | i=-1; |
dorian505 | 6:045defd0d877 | 96 | break; |
dorian505 | 6:045defd0d877 | 97 | } |
dorian505 | 6:045defd0d877 | 98 | }} |
dorian505 | 6:045defd0d877 | 99 | // if ! buttons |
scohennm | 0:203b4129a213 | 100 | |
scohennm | 0:203b4129a213 | 101 | #ifdef PRINTDBUG |
dorian505 | 10:3de03f06624e | 102 | pc.printf("xAcc = %f\r\n", xAcc); |
dorian505 | 10:3de03f06624e | 103 | pc.printf("yAcc = %f\r\n", yAcc); |
dorian505 | 10:3de03f06624e | 104 | pc.printf("zAcc = %f\r\n", zAcc); |
dorian505 | 10:3de03f06624e | 105 | pc.printf("LBUT = %f\r\n", LBUT); |
scohennm | 0:203b4129a213 | 106 | #endif |
scohennm | 0:203b4129a213 | 107 | |
scohennm | 0:203b4129a213 | 108 | // Wait then do the whole thing again. |
dorian505 | 10:3de03f06624e | 109 | } |
dorian505 | 10:3de03f06624e | 110 | }} |