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