modified
Dependencies: MMA8451Q8 SLCD mbed
Fork of LCD_ACC_46_v4-8g by
lcd_acc_46_v4-8g.cpp@5:3f47c545d722, 2017-03-08 (annotated)
- Committer:
- Raiden817
- Date:
- Wed Mar 08 16:30:02 2017 +0000
- Revision:
- 5:3f47c545d722
- Parent:
- 4:6128eb8206ee
modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 4:6128eb8206ee | 1 | #include "mbed.h" |
scohennm | 4:6128eb8206ee | 2 | #include "MMA8451Q8.h" |
scohennm | 4:6128eb8206ee | 3 | #include "SLCD.h" |
scohennm | 4:6128eb8206ee | 4 | /* |
scohennm | 4:6128eb8206ee | 5 | Test of the accelerometer, digital I/O, on-board LCD screen, and 16-bit ADC. |
scohennm | 4:6128eb8206ee | 6 | |
scohennm | 4:6128eb8206ee | 7 | */ |
scohennm | 4:6128eb8206ee | 8 | |
scohennm | 4:6128eb8206ee | 9 | #define BLINKTIME 0.7 |
scohennm | 4:6128eb8206ee | 10 | #define DATATIME 0.1 |
scohennm | 4:6128eb8206ee | 11 | #define DATADISPDWELL 0.2 |
scohennm | 4:6128eb8206ee | 12 | #define NUMLEDS 2 |
scohennm | 4:6128eb8206ee | 13 | #define LEDON 0 |
scohennm | 4:6128eb8206ee | 14 | #define LEDOFF 1 |
scohennm | 4:6128eb8206ee | 15 | #define SCALING 13 |
scohennm | 4:6128eb8206ee | 16 | #define RSTARTMESS "RSET" |
scohennm | 4:6128eb8206ee | 17 | #define MAXVECT "MAXV" |
scohennm | 4:6128eb8206ee | 18 | #define XCOMP "XCMP" |
scohennm | 4:6128eb8206ee | 19 | #define YCOMP "YCMP" |
scohennm | 4:6128eb8206ee | 20 | #define ZCOMP "ZCMP" |
scohennm | 4:6128eb8206ee | 21 | #define ANALTOVOLTS 3.3 |
scohennm | 4:6128eb8206ee | 22 | #define LEDDELAY 0.400 |
scohennm | 4:6128eb8206ee | 23 | //define states |
scohennm | 4:6128eb8206ee | 24 | #define NUMSTATES 4 |
scohennm | 4:6128eb8206ee | 25 | #define XCOMPD 0 |
scohennm | 4:6128eb8206ee | 26 | #define YCOMPD 1 |
scohennm | 4:6128eb8206ee | 27 | #define ZCOMPD 2 |
scohennm | 4:6128eb8206ee | 28 | #define VMAXD 3 |
scohennm | 4:6128eb8206ee | 29 | |
scohennm | 4:6128eb8206ee | 30 | #define REG_WHO_AM_I 0x0D |
scohennm | 4:6128eb8206ee | 31 | #define XYZ_DATA_CFG 0x0E |
scohennm | 4:6128eb8206ee | 32 | |
scohennm | 4:6128eb8206ee | 33 | #define MAX_2G 0x00 |
scohennm | 4:6128eb8206ee | 34 | #define MAX_4G 0x01 |
scohennm | 4:6128eb8206ee | 35 | #define MAX_8G 0x02 |
scohennm | 4:6128eb8206ee | 36 | |
scohennm | 4:6128eb8206ee | 37 | #define PROGNAME "LCD_ACC_LCDv3 46\r/n" |
scohennm | 4:6128eb8206ee | 38 | |
scohennm | 4:6128eb8206ee | 39 | //#define PRINTDBUG |
scohennm | 4:6128eb8206ee | 40 | // Accelerometer SPI pins |
scohennm | 4:6128eb8206ee | 41 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 4:6128eb8206ee | 42 | PinName const SDA = PTE25; |
scohennm | 4:6128eb8206ee | 43 | PinName const SCL = PTE24; |
scohennm | 4:6128eb8206ee | 44 | #elif defined (TARGET_KL05Z) |
scohennm | 4:6128eb8206ee | 45 | PinName const SDA = PTB4; |
scohennm | 4:6128eb8206ee | 46 | PinName const SCL = PTB3; |
scohennm | 4:6128eb8206ee | 47 | #else |
scohennm | 4:6128eb8206ee | 48 | #error TARGET NOT DEFINED |
scohennm | 4:6128eb8206ee | 49 | #endif |
scohennm | 4:6128eb8206ee | 50 | |
scohennm | 4:6128eb8206ee | 51 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 4:6128eb8206ee | 52 | |
scohennm | 4:6128eb8206ee | 53 | SLCD slcd; //define LCD display |
scohennm | 4:6128eb8206ee | 54 | Timer blinkTimer; |
scohennm | 4:6128eb8206ee | 55 | Timer dataTimer; |
scohennm | 4:6128eb8206ee | 56 | Timer displayTimer; |
scohennm | 4:6128eb8206ee | 57 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 4:6128eb8206ee | 58 | AnalogIn TMP01(PTB1); |
scohennm | 4:6128eb8206ee | 59 | DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs |
scohennm | 4:6128eb8206ee | 60 | Serial pc(USBTX, USBRX); |
scohennm | 4:6128eb8206ee | 61 | |
scohennm | 4:6128eb8206ee | 62 | |
scohennm | 4:6128eb8206ee | 63 | void LCDMess(char *lMess, float dWait){ |
scohennm | 4:6128eb8206ee | 64 | slcd.Home(); |
scohennm | 4:6128eb8206ee | 65 | slcd.clear(); |
scohennm | 4:6128eb8206ee | 66 | slcd.printf(lMess); |
scohennm | 4:6128eb8206ee | 67 | wait(dWait); |
scohennm | 4:6128eb8206ee | 68 | } |
scohennm | 4:6128eb8206ee | 69 | void LCDMessNoDwell(char *lMess){ |
scohennm | 4:6128eb8206ee | 70 | slcd.Home(); |
scohennm | 4:6128eb8206ee | 71 | slcd.clear(); |
scohennm | 4:6128eb8206ee | 72 | slcd.printf(lMess); |
scohennm | 4:6128eb8206ee | 73 | } |
scohennm | 4:6128eb8206ee | 74 | |
scohennm | 4:6128eb8206ee | 75 | |
scohennm | 4:6128eb8206ee | 76 | void allLEDsOFF(int numberOfLEDS) { |
scohennm | 4:6128eb8206ee | 77 | int i; |
scohennm | 4:6128eb8206ee | 78 | for (i=0;i<numberOfLEDS; i++){ |
scohennm | 4:6128eb8206ee | 79 | LEDs[i] = LEDOFF; |
scohennm | 4:6128eb8206ee | 80 | } |
scohennm | 4:6128eb8206ee | 81 | |
scohennm | 4:6128eb8206ee | 82 | } |
scohennm | 4:6128eb8206ee | 83 | |
scohennm | 4:6128eb8206ee | 84 | |
scohennm | 4:6128eb8206ee | 85 | |
scohennm | 4:6128eb8206ee | 86 | int main() { |
scohennm | 4:6128eb8206ee | 87 | |
scohennm | 4:6128eb8206ee | 88 | int RButtonState; |
scohennm | 4:6128eb8206ee | 89 | int LButtonState; |
scohennm | 4:6128eb8206ee | 90 | DigitalIn RtButton(PTC12); |
scohennm | 4:6128eb8206ee | 91 | DigitalIn LftButton(PTC3); |
scohennm | 4:6128eb8206ee | 92 | int displayState = XCOMPD; |
scohennm | 4:6128eb8206ee | 93 | float xAcc = 0.0; |
scohennm | 4:6128eb8206ee | 94 | float yAcc = 0.0; |
scohennm | 4:6128eb8206ee | 95 | float zAcc = 0.0; |
scohennm | 4:6128eb8206ee | 96 | float vector; |
scohennm | 4:6128eb8206ee | 97 | float vMax = 0.0; |
scohennm | 4:6128eb8206ee | 98 | float DisplayTime = DATADISPDWELL; |
scohennm | 4:6128eb8206ee | 99 | int outState = false; |
scohennm | 4:6128eb8206ee | 100 | char lcdData[10]; //buffer needs places for decimal pt and colon |
scohennm | 4:6128eb8206ee | 101 | uint8_t regData = MAX_4G; |
scohennm | 4:6128eb8206ee | 102 | |
scohennm | 4:6128eb8206ee | 103 | |
scohennm | 4:6128eb8206ee | 104 | #ifdef PRINTDBUG |
scohennm | 4:6128eb8206ee | 105 | pc.printf(PROGNAME); |
scohennm | 4:6128eb8206ee | 106 | #endif |
scohennm | 4:6128eb8206ee | 107 | LCDMess(RSTARTMESS, BLINKTIME); |
scohennm | 4:6128eb8206ee | 108 | // Initialze readings and sequence the LED's for dramtic effect. |
scohennm | 4:6128eb8206ee | 109 | allLEDsOFF(NUMLEDS); |
scohennm | 4:6128eb8206ee | 110 | blinkTimer.start(); |
scohennm | 4:6128eb8206ee | 111 | blinkTimer.reset(); |
scohennm | 4:6128eb8206ee | 112 | displayTimer.start(); |
scohennm | 4:6128eb8206ee | 113 | displayTimer.reset(); |
scohennm | 4:6128eb8206ee | 114 | dataTimer.start(); |
scohennm | 4:6128eb8206ee | 115 | dataTimer.reset(); |
scohennm | 4:6128eb8206ee | 116 | // change sensititivity experimental |
scohennm | 4:6128eb8206ee | 117 | acc.setGLimit(); |
scohennm | 4:6128eb8206ee | 118 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
scohennm | 4:6128eb8206ee | 119 | sprintf (lcdData,"%d",regData); |
scohennm | 4:6128eb8206ee | 120 | LCDMess(lcdData,BLINKTIME); |
scohennm | 4:6128eb8206ee | 121 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
scohennm | 4:6128eb8206ee | 122 | sprintf (lcdData,"%d",regData); |
scohennm | 4:6128eb8206ee | 123 | LCDMess(lcdData,BLINKTIME); |
scohennm | 4:6128eb8206ee | 124 | |
scohennm | 4:6128eb8206ee | 125 | // main loop forever |
scohennm | 4:6128eb8206ee | 126 | while(true) { |
scohennm | 4:6128eb8206ee | 127 | |
scohennm | 4:6128eb8206ee | 128 | // Handle user input for display selections |
scohennm | 4:6128eb8206ee | 129 | RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream |
scohennm | 4:6128eb8206ee | 130 | if (RButtonState){ |
scohennm | 4:6128eb8206ee | 131 | vMax = 0.0; // Clear vMax |
scohennm | 4:6128eb8206ee | 132 | LCDMess(RSTARTMESS, BLINKTIME); |
scohennm | 4:6128eb8206ee | 133 | } |
scohennm | 4:6128eb8206ee | 134 | LButtonState = !LftButton; |
scohennm | 4:6128eb8206ee | 135 | if (LButtonState) { //Change data that is displayed |
scohennm | 4:6128eb8206ee | 136 | displayState = (displayState + 1) % NUMSTATES; |
scohennm | 4:6128eb8206ee | 137 | // Change to switch/case soon. |
scohennm | 4:6128eb8206ee | 138 | switch (displayState){ |
scohennm | 4:6128eb8206ee | 139 | case XCOMPD: { |
scohennm | 4:6128eb8206ee | 140 | LCDMess(XCOMP,BLINKTIME); |
scohennm | 4:6128eb8206ee | 141 | break; |
scohennm | 4:6128eb8206ee | 142 | } |
scohennm | 4:6128eb8206ee | 143 | case YCOMPD: { |
scohennm | 4:6128eb8206ee | 144 | LCDMess(YCOMP,BLINKTIME); |
scohennm | 4:6128eb8206ee | 145 | break; |
scohennm | 4:6128eb8206ee | 146 | } |
scohennm | 4:6128eb8206ee | 147 | case ZCOMPD: { |
scohennm | 4:6128eb8206ee | 148 | LCDMess(ZCOMP,BLINKTIME); |
scohennm | 4:6128eb8206ee | 149 | break; |
scohennm | 4:6128eb8206ee | 150 | } |
scohennm | 4:6128eb8206ee | 151 | case VMAXD:{ |
scohennm | 4:6128eb8206ee | 152 | LCDMess(MAXVECT,BLINKTIME); |
scohennm | 4:6128eb8206ee | 153 | break; |
scohennm | 4:6128eb8206ee | 154 | } |
scohennm | 4:6128eb8206ee | 155 | }// switch |
scohennm | 4:6128eb8206ee | 156 | } |
scohennm | 4:6128eb8206ee | 157 | |
scohennm | 4:6128eb8206ee | 158 | // -------------------------------------------- |
scohennm | 4:6128eb8206ee | 159 | while (dataTimer.read() > DATATIME){ |
scohennm | 4:6128eb8206ee | 160 | // No offset |
scohennm | 4:6128eb8206ee | 161 | xAcc = abs(acc.getAccX()); |
scohennm | 4:6128eb8206ee | 162 | yAcc = abs(acc.getAccY()); |
scohennm | 4:6128eb8206ee | 163 | zAcc = abs(acc.getAccZ()); |
scohennm | 4:6128eb8206ee | 164 | // Calulate vector sum of x,y and z reading. |
scohennm | 4:6128eb8206ee | 165 | // vector = sqrt(pow(xAcc,2) + pow(zAcc,2)); |
scohennm | 4:6128eb8206ee | 166 | vector = zAcc; |
scohennm | 4:6128eb8206ee | 167 | if (vector > vMax) { |
scohennm | 4:6128eb8206ee | 168 | vMax = vector; |
scohennm | 4:6128eb8206ee | 169 | } |
scohennm | 4:6128eb8206ee | 170 | dataTimer.reset(); |
scohennm | 4:6128eb8206ee | 171 | } |
scohennm | 4:6128eb8206ee | 172 | |
scohennm | 4:6128eb8206ee | 173 | // Display the appropriate data on the LCD based upon what mode was chosen |
scohennm | 4:6128eb8206ee | 174 | while (displayTimer.read() > DisplayTime){ |
scohennm | 4:6128eb8206ee | 175 | switch (displayState) { |
scohennm | 4:6128eb8206ee | 176 | case XCOMPD: { |
scohennm | 4:6128eb8206ee | 177 | sprintf (lcdData,"%4.3f",xAcc); |
scohennm | 4:6128eb8206ee | 178 | break; |
scohennm | 4:6128eb8206ee | 179 | } |
scohennm | 4:6128eb8206ee | 180 | case YCOMPD: { |
scohennm | 4:6128eb8206ee | 181 | sprintf (lcdData,"%4.3f",yAcc); |
scohennm | 4:6128eb8206ee | 182 | break; |
scohennm | 4:6128eb8206ee | 183 | } |
scohennm | 4:6128eb8206ee | 184 | case ZCOMPD: { |
scohennm | 4:6128eb8206ee | 185 | sprintf (lcdData,"%4.3f",zAcc); |
scohennm | 4:6128eb8206ee | 186 | break; |
scohennm | 4:6128eb8206ee | 187 | } |
scohennm | 4:6128eb8206ee | 188 | case VMAXD:{ |
scohennm | 4:6128eb8206ee | 189 | sprintf (lcdData,"%4.3f",vMax); |
scohennm | 4:6128eb8206ee | 190 | break; |
scohennm | 4:6128eb8206ee | 191 | } |
scohennm | 4:6128eb8206ee | 192 | } |
scohennm | 4:6128eb8206ee | 193 | LCDMessNoDwell(lcdData); |
scohennm | 4:6128eb8206ee | 194 | displayTimer.reset(); |
scohennm | 4:6128eb8206ee | 195 | } // displaytimer |
scohennm | 4:6128eb8206ee | 196 | // Wait then do the whole thing again. |
scohennm | 4:6128eb8206ee | 197 | // Alive LEDs |
scohennm | 4:6128eb8206ee | 198 | while(blinkTimer.read()> LEDDELAY) { |
scohennm | 4:6128eb8206ee | 199 | LEDs[0].write(outState); |
scohennm | 4:6128eb8206ee | 200 | LEDs[1].write(!outState); |
scohennm | 4:6128eb8206ee | 201 | outState = !outState; |
scohennm | 4:6128eb8206ee | 202 | blinkTimer.reset(); |
scohennm | 4:6128eb8206ee | 203 | } |
scohennm | 4:6128eb8206ee | 204 | }//forever loop |
scohennm | 4:6128eb8206ee | 205 | }// main |