HW-15.1
Dependencies: MMA8451Q SLCD_minus_mod mbed
Fork of ACC_LCD_341_Basic by
acc_341.cpp@6:f53933d232fc, 2016-11-28 (annotated)
- Committer:
- kennylujan42
- Date:
- Mon Nov 28 04:56:16 2016 +0000
- Revision:
- 6:f53933d232fc
- Parent:
- 5:29c6ba524263
KLUJ-SSD341-HW15.1
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 | 4:1d559bac561a | 13 | #define LCDDATALEN 10 |
scohennm | 0:203b4129a213 | 14 | |
kennylujan42 | 6:f53933d232fc | 15 | #define LEDON false |
kennylujan42 | 6:f53933d232fc | 16 | #define LEDOFF true |
kennylujan42 | 6:f53933d232fc | 17 | #define BUTDOWN false |
kennylujan42 | 6:f53933d232fc | 18 | #define BUTUP true |
kennylujan42 | 6:f53933d232fc | 19 | #define NUMBUTS 2 |
kennylujan42 | 6:f53933d232fc | 20 | #define LBUT PTC12 |
kennylujan42 | 6:f53933d232fc | 21 | #define RBUT PTC3 |
kennylujan42 | 6:f53933d232fc | 22 | #define LRED "Y " |
kennylujan42 | 6:f53933d232fc | 23 | #define LGREEN "X " |
kennylujan42 | 6:f53933d232fc | 24 | #define PRED "Y\r\n" |
kennylujan42 | 6:f53933d232fc | 25 | #define PGREEN "X\r\n" |
kennylujan42 | 6:f53933d232fc | 26 | #define NUMMESS 2 |
kennylujan42 | 6:f53933d232fc | 27 | |
scohennm | 3:c5291c70af48 | 28 | #define PROGNAME "ACCLCD341-541\r\n" |
scohennm | 0:203b4129a213 | 29 | |
scohennm | 0:203b4129a213 | 30 | #define PRINTDBUG |
scohennm | 2:6003ed409def | 31 | // |
scohennm | 0:203b4129a213 | 32 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 2:6003ed409def | 33 | PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. |
scohennm | 2:6003ed409def | 34 | PinName const SCL = PTE24; // DO NOT CHANGE |
scohennm | 0:203b4129a213 | 35 | #elif defined (TARGET_KL05Z) |
scohennm | 0:203b4129a213 | 36 | PinName const SDA = PTB4; |
scohennm | 0:203b4129a213 | 37 | PinName const SCL = PTB3; |
scohennm | 0:203b4129a213 | 38 | #else |
scohennm | 0:203b4129a213 | 39 | #error TARGET NOT DEFINED |
scohennm | 0:203b4129a213 | 40 | #endif |
scohennm | 0:203b4129a213 | 41 | |
scohennm | 0:203b4129a213 | 42 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:203b4129a213 | 43 | |
scohennm | 0:203b4129a213 | 44 | SLCD slcd; //define LCD display |
scohennm | 4:1d559bac561a | 45 | char lcdData[LCDDATALEN]; //buffer needs places dor decimal pt and colon |
scohennm | 0:203b4129a213 | 46 | |
kennylujan42 | 6:f53933d232fc | 47 | int ledState = LEDON; |
kennylujan42 | 6:f53933d232fc | 48 | int buttonStates[NUMBUTS] = {BUTDOWN,BUTUP}; |
kennylujan42 | 6:f53933d232fc | 49 | DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; |
kennylujan42 | 6:f53933d232fc | 50 | DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED}; |
kennylujan42 | 6:f53933d232fc | 51 | |
scohennm | 0:203b4129a213 | 52 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:203b4129a213 | 53 | Serial pc(USBTX, USBRX); |
scohennm | 3:c5291c70af48 | 54 | Timer dataTimer; |
scohennm | 0:203b4129a213 | 55 | |
scohennm | 2:6003ed409def | 56 | |
scohennm | 0:203b4129a213 | 57 | |
scohennm | 3:c5291c70af48 | 58 | void LCDMess(char *lMess){ |
scohennm | 0:203b4129a213 | 59 | slcd.Home(); |
scohennm | 0:203b4129a213 | 60 | slcd.clear(); |
scohennm | 0:203b4129a213 | 61 | slcd.printf(lMess); |
scohennm | 0:203b4129a213 | 62 | } |
scohennm | 4:1d559bac561a | 63 | |
scohennm | 4:1d559bac561a | 64 | void LCDsignedFloat(float theNumber){ |
scohennm | 5:29c6ba524263 | 65 | sprintf (lcdData," %3.2f",theNumber); |
scohennm | 5:29c6ba524263 | 66 | if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber)); |
scohennm | 4:1d559bac561a | 67 | LCDMess(lcdData); |
scohennm | 4:1d559bac561a | 68 | } |
scohennm | 4:1d559bac561a | 69 | |
scohennm | 3:c5291c70af48 | 70 | void initialize_global_vars(){ |
scohennm | 3:c5291c70af48 | 71 | pc.printf(PROGNAME); |
scohennm | 3:c5291c70af48 | 72 | dataTimer.start(); |
scohennm | 3:c5291c70af48 | 73 | dataTimer.reset(); |
scohennm | 3:c5291c70af48 | 74 | } |
scohennm | 0:203b4129a213 | 75 | |
scohennm | 0:203b4129a213 | 76 | int main() { |
scohennm | 0:203b4129a213 | 77 | float xAcc; |
scohennm | 3:c5291c70af48 | 78 | float yAcc; |
scohennm | 4:1d559bac561a | 79 | float zAcc; |
kennylujan42 | 6:f53933d232fc | 80 | //-------------- |
kennylujan42 | 6:f53933d232fc | 81 | int i; |
kennylujan42 | 6:f53933d232fc | 82 | int currentLED = 0; |
kennylujan42 | 6:f53933d232fc | 83 | char rMess[NUMMESS][LCDDATALEN]={LGREEN, LRED}; |
kennylujan42 | 6:f53933d232fc | 84 | char pMess[NUMMESS][LCDDATALEN]={xAcc, yAcc}; |
kennylujan42 | 6:f53933d232fc | 85 | |
scohennm | 3:c5291c70af48 | 86 | initialize_global_vars(); |
scohennm | 0:203b4129a213 | 87 | // main loop forever |
scohennm | 0:203b4129a213 | 88 | while(true) { |
scohennm | 3:c5291c70af48 | 89 | if(dataTimer.read() > DATAINTERVAL){ |
scohennm | 3:c5291c70af48 | 90 | dataTimer.reset(); |
scohennm | 0:203b4129a213 | 91 | //Get accelerometer data - tilt angles minus offset for zero mark. |
scohennm | 3:c5291c70af48 | 92 | xAcc = acc.getAccX(); |
scohennm | 4:1d559bac561a | 93 | yAcc = acc.getAccY(); |
scohennm | 4:1d559bac561a | 94 | zAcc = acc.getAccZ(); |
scohennm | 0:203b4129a213 | 95 | |
kennylujan42 | 6:f53933d232fc | 96 | |
kennylujan42 | 6:f53933d232fc | 97 | |
kennylujan42 | 6:f53933d232fc | 98 | while(true) { |
kennylujan42 | 6:f53933d232fc | 99 | for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 |
kennylujan42 | 6:f53933d232fc | 100 | LEDs[i] = LEDOFF; |
kennylujan42 | 6:f53933d232fc | 101 | // if(buttons[i] == BUTDOWN) { |
kennylujan42 | 6:f53933d232fc | 102 | if(!buttons[0]) { |
kennylujan42 | 6:f53933d232fc | 103 | LCDMess(rMess[0]); |
kennylujan42 | 6:f53933d232fc | 104 | wait(0.5); |
kennylujan42 | 6:f53933d232fc | 105 | //LCDMess(pMess[i]); |
kennylujan42 | 6:f53933d232fc | 106 | xAcc = acc.getAccX(); |
kennylujan42 | 6:f53933d232fc | 107 | LCDsignedFloat(xAcc); |
kennylujan42 | 6:f53933d232fc | 108 | currentLED = 0; |
kennylujan42 | 6:f53933d232fc | 109 | } |
kennylujan42 | 6:f53933d232fc | 110 | if(!buttons[1]) { |
kennylujan42 | 6:f53933d232fc | 111 | LCDMess(rMess[1]); |
kennylujan42 | 6:f53933d232fc | 112 | wait(0.5); |
kennylujan42 | 6:f53933d232fc | 113 | yAcc = acc.getAccY(); |
kennylujan42 | 6:f53933d232fc | 114 | LCDsignedFloat(yAcc); |
kennylujan42 | 6:f53933d232fc | 115 | currentLED = 1; |
kennylujan42 | 6:f53933d232fc | 116 | } |
kennylujan42 | 6:f53933d232fc | 117 | } |
kennylujan42 | 6:f53933d232fc | 118 | |
kennylujan42 | 6:f53933d232fc | 119 | ledState = !ledState; // Flip the general state |
kennylujan42 | 6:f53933d232fc | 120 | LEDs[currentLED] = ledState; |
kennylujan42 | 6:f53933d232fc | 121 | wait(0.01); |
kennylujan42 | 6:f53933d232fc | 122 | |
kennylujan42 | 6:f53933d232fc | 123 | |
kennylujan42 | 6:f53933d232fc | 124 | } |
kennylujan42 | 6:f53933d232fc | 125 | |
kennylujan42 | 6:f53933d232fc | 126 | |
kennylujan42 | 6:f53933d232fc | 127 | |
kennylujan42 | 6:f53933d232fc | 128 | |
scohennm | 0:203b4129a213 | 129 | #ifdef PRINTDBUG |
scohennm | 1:9340a340e588 | 130 | pc.printf("xAcc = %f\r\n", xAcc); |
scohennm | 1:9340a340e588 | 131 | pc.printf("yAcc = %f\r\n", yAcc); |
scohennm | 4:1d559bac561a | 132 | pc.printf("zAcc = %f\r\n", zAcc); |
scohennm | 0:203b4129a213 | 133 | #endif |
kennylujan42 | 6:f53933d232fc | 134 | |
scohennm | 0:203b4129a213 | 135 | |
kennylujan42 | 6:f53933d232fc | 136 | |
scohennm | 0:203b4129a213 | 137 | // Wait then do the whole thing again. |
scohennm | 3:c5291c70af48 | 138 | } |
scohennm | 0:203b4129a213 | 139 | } |
scohennm | 0:203b4129a213 | 140 | } |