attempt at pt 2 of the midterm
Dependencies: MMA8451Q8b SLCD mbed
KL46z_single_tap_2016..cpp@0:78436f4486d2, 2017-03-15 (annotated)
- Committer:
- CKMonroe
- Date:
- Wed Mar 15 05:34:55 2017 +0000
- Revision:
- 0:78436f4486d2
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CKMonroe | 0:78436f4486d2 | 1 | #include "mbed.h" |
CKMonroe | 0:78436f4486d2 | 2 | #include "MMA8451Q8g.h" |
CKMonroe | 0:78436f4486d2 | 3 | #include "SLCD.h" |
CKMonroe | 0:78436f4486d2 | 4 | |
CKMonroe | 0:78436f4486d2 | 5 | #define BLINKTIME 1.5 |
CKMonroe | 0:78436f4486d2 | 6 | #define RELAYON 0 |
CKMonroe | 0:78436f4486d2 | 7 | #define RELAYOFF 1 |
CKMonroe | 0:78436f4486d2 | 8 | #define LEDDELAY 0.4 |
CKMonroe | 0:78436f4486d2 | 9 | #define WAITDELAY 0.7 |
CKMonroe | 0:78436f4486d2 | 10 | #define LCDLEN 10 |
CKMonroe | 0:78436f4486d2 | 11 | |
CKMonroe | 0:78436f4486d2 | 12 | #define REG_WHO_AM_I 0x0D |
CKMonroe | 0:78436f4486d2 | 13 | #define XYZ_DATA_CFG 0x0E |
CKMonroe | 0:78436f4486d2 | 14 | |
CKMonroe | 0:78436f4486d2 | 15 | #define REG_OUT_X_MSB 0x01 |
CKMonroe | 0:78436f4486d2 | 16 | #define REG_OUT_Y_MSB 0x03 |
CKMonroe | 0:78436f4486d2 | 17 | #define REG_OUT_Z_MSB 0x05 |
CKMonroe | 0:78436f4486d2 | 18 | #define REG_PULSE_CFG 0x21 |
CKMonroe | 0:78436f4486d2 | 19 | #define REG_PULSE_SRC 0x22 |
CKMonroe | 0:78436f4486d2 | 20 | #define REG_PULSE_THSZ 0x25 |
CKMonroe | 0:78436f4486d2 | 21 | |
CKMonroe | 0:78436f4486d2 | 22 | //#define REH_PULSE_TMLT 0x26 |
CKMonroe | 0:78436f4486d2 | 23 | //CKM- changed this to 0x38 to try to set |
CKMonroe | 0:78436f4486d2 | 24 | //transient tap detection limit to 35ms |
CKMonroe | 0:78436f4486d2 | 25 | #define REH_PULSE_TMLT 0x38 |
CKMonroe | 0:78436f4486d2 | 26 | |
CKMonroe | 0:78436f4486d2 | 27 | #define REG_CTRL_4 0x2D |
CKMonroe | 0:78436f4486d2 | 28 | #define REG_CTRL_5 0x2E |
CKMonroe | 0:78436f4486d2 | 29 | |
CKMonroe | 0:78436f4486d2 | 30 | #define MAX_2G 0x00 |
CKMonroe | 0:78436f4486d2 | 31 | #define MAX_4G 0x01 |
CKMonroe | 0:78436f4486d2 | 32 | #define MAX_8G 0x02 |
CKMonroe | 0:78436f4486d2 | 33 | |
CKMonroe | 0:78436f4486d2 | 34 | //CKM- leaving set_interrupt to change the pin to enable interrupts, |
CKMonroe | 0:78436f4486d2 | 35 | //but changing the interrupt line to INT2 |
CKMonroe | 0:78436f4486d2 | 36 | #define SET_INTERRUPT 0x08 |
CKMonroe | 0:78436f4486d2 | 37 | //#define SET_INT_LINE 0x08 |
CKMonroe | 0:78436f4486d2 | 38 | #define SET_INT_LINE 0x2E&0xF7 |
CKMonroe | 0:78436f4486d2 | 39 | |
CKMonroe | 0:78436f4486d2 | 40 | //#define SET_THZ 0x20 // See Table 49 in data sheet |
CKMonroe | 0:78436f4486d2 | 41 | //CKM- changed this to 40 counts, |
CKMonroe | 0:78436f4486d2 | 42 | //which is 0x28 in hex |
CKMonroe | 0:78436f4486d2 | 43 | #define SET_THZ 0x28 |
CKMonroe | 0:78436f4486d2 | 44 | |
CKMonroe | 0:78436f4486d2 | 45 | //CKM- adding another define for the 400Hz mask |
CKMonroe | 0:78436f4486d2 | 46 | #define HZ_MASK 0x08 |
CKMonroe | 0:78436f4486d2 | 47 | #define REG_CTRL_REG_1 0x2A |
CKMonroe | 0:78436f4486d2 | 48 | |
CKMonroe | 0:78436f4486d2 | 49 | /*********** |
CKMonroe | 0:78436f4486d2 | 50 | 0b0010 0000 |
CKMonroe | 0:78436f4486d2 | 51 | 2g/0.063g/count = 32 counts |
CKMonroe | 0:78436f4486d2 | 52 | ************/ |
CKMonroe | 0:78436f4486d2 | 53 | #define SET_TMLT 0x18 // See Talbe 51 in data sheet |
CKMonroe | 0:78436f4486d2 | 54 | /****** |
CKMonroe | 0:78436f4486d2 | 55 | 0b0001 1000 |
CKMonroe | 0:78436f4486d2 | 56 | 24 = 0x18 |
CKMonroe | 0:78436f4486d2 | 57 | time step at 800Hz ODR = 0.625ms |
CKMonroe | 0:78436f4486d2 | 58 | 24x0.625 = 15 ms |
CKMonroe | 0:78436f4486d2 | 59 | ******/ |
CKMonroe | 0:78436f4486d2 | 60 | |
CKMonroe | 0:78436f4486d2 | 61 | //#define PRINTDBUG |
CKMonroe | 0:78436f4486d2 | 62 | // Accelerometer SPI pins |
CKMonroe | 0:78436f4486d2 | 63 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
CKMonroe | 0:78436f4486d2 | 64 | PinName const SDA = PTE25; |
CKMonroe | 0:78436f4486d2 | 65 | PinName const SCL = PTE24; |
CKMonroe | 0:78436f4486d2 | 66 | #elif defined (TARGET_KL05Z) |
CKMonroe | 0:78436f4486d2 | 67 | PinName const SDA = PTB4; |
CKMonroe | 0:78436f4486d2 | 68 | PinName const SCL = PTB3; |
CKMonroe | 0:78436f4486d2 | 69 | #else |
CKMonroe | 0:78436f4486d2 | 70 | #error TARGET NOT DEFINED |
CKMonroe | 0:78436f4486d2 | 71 | #endif |
CKMonroe | 0:78436f4486d2 | 72 | |
CKMonroe | 0:78436f4486d2 | 73 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
CKMonroe | 0:78436f4486d2 | 74 | |
CKMonroe | 0:78436f4486d2 | 75 | Ticker ledBlink; // timinginterrupt for RED led |
CKMonroe | 0:78436f4486d2 | 76 | InterruptIn MMA8451QInt1(PTC5); //push botton with internal pullup |
CKMonroe | 0:78436f4486d2 | 77 | DigitalOut myled(LED_RED); // red led |
CKMonroe | 0:78436f4486d2 | 78 | DigitalOut relay(LED_GREEN); // green led |
CKMonroe | 0:78436f4486d2 | 79 | |
CKMonroe | 0:78436f4486d2 | 80 | //CKM- added serial pc object to print to terminal |
CKMonroe | 0:78436f4486d2 | 81 | Serial pc(USBTX, USBRX); |
CKMonroe | 0:78436f4486d2 | 82 | |
CKMonroe | 0:78436f4486d2 | 83 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
CKMonroe | 0:78436f4486d2 | 84 | |
CKMonroe | 0:78436f4486d2 | 85 | float delay = WAITDELAY; |
CKMonroe | 0:78436f4486d2 | 86 | int relayState = RELAYOFF; |
CKMonroe | 0:78436f4486d2 | 87 | int outState = false; |
CKMonroe | 0:78436f4486d2 | 88 | SLCD slcd; //define LCD display |
CKMonroe | 0:78436f4486d2 | 89 | char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"}; |
CKMonroe | 0:78436f4486d2 | 90 | |
CKMonroe | 0:78436f4486d2 | 91 | |
CKMonroe | 0:78436f4486d2 | 92 | void LCDMess(char *lMess, float dWait){ |
CKMonroe | 0:78436f4486d2 | 93 | slcd.Home(); |
CKMonroe | 0:78436f4486d2 | 94 | slcd.clear(); |
CKMonroe | 0:78436f4486d2 | 95 | slcd.printf(lMess); |
CKMonroe | 0:78436f4486d2 | 96 | wait(dWait); |
CKMonroe | 0:78436f4486d2 | 97 | } |
CKMonroe | 0:78436f4486d2 | 98 | void LCDMessNoDwell(char *lMess){ |
CKMonroe | 0:78436f4486d2 | 99 | slcd.Home(); |
CKMonroe | 0:78436f4486d2 | 100 | slcd.clear(); |
CKMonroe | 0:78436f4486d2 | 101 | slcd.printf(lMess); |
CKMonroe | 0:78436f4486d2 | 102 | } |
CKMonroe | 0:78436f4486d2 | 103 | |
CKMonroe | 0:78436f4486d2 | 104 | // Interrupt routines |
CKMonroe | 0:78436f4486d2 | 105 | void LEDBlinker(){ // RED LED interrupt |
CKMonroe | 0:78436f4486d2 | 106 | outState = !outState; |
CKMonroe | 0:78436f4486d2 | 107 | myled.write(outState); |
CKMonroe | 0:78436f4486d2 | 108 | } |
CKMonroe | 0:78436f4486d2 | 109 | |
CKMonroe | 0:78436f4486d2 | 110 | void GreenLEDBlinker(){ // Green LED interrupt |
CKMonroe | 0:78436f4486d2 | 111 | //uint8_t i_regData; |
CKMonroe | 0:78436f4486d2 | 112 | relayState = !relayState; |
CKMonroe | 0:78436f4486d2 | 113 | relay.write(relayState); |
CKMonroe | 0:78436f4486d2 | 114 | //acc.readRegs(REG_PULSE_SRC, &i_regData, 1); // Clear the tap event |
CKMonroe | 0:78436f4486d2 | 115 | } |
CKMonroe | 0:78436f4486d2 | 116 | |
CKMonroe | 0:78436f4486d2 | 117 | // end interrupt routines |
CKMonroe | 0:78436f4486d2 | 118 | |
CKMonroe | 0:78436f4486d2 | 119 | int main() |
CKMonroe | 0:78436f4486d2 | 120 | { |
CKMonroe | 0:78436f4486d2 | 121 | uint8_t regData; |
CKMonroe | 0:78436f4486d2 | 122 | uint8_t latchData = 0x40; //0b01000000; //for pulse config register |
CKMonroe | 0:78436f4486d2 | 123 | //uint8_t axisData = 0x10; //0b00010000; |
CKMonroe | 0:78436f4486d2 | 124 | |
CKMonroe | 0:78436f4486d2 | 125 | //CKM- changed this to 0x20- 0b00100000, |
CKMonroe | 0:78436f4486d2 | 126 | //to register the axis as a double Z tap only |
CKMonroe | 0:78436f4486d2 | 127 | uint8_t axisData = 0x20;// |
CKMonroe | 0:78436f4486d2 | 128 | |
CKMonroe | 0:78436f4486d2 | 129 | char lcdData[LCDLEN]; |
CKMonroe | 0:78436f4486d2 | 130 | |
CKMonroe | 0:78436f4486d2 | 131 | myled.write(outState); |
CKMonroe | 0:78436f4486d2 | 132 | relay.write(relayState); |
CKMonroe | 0:78436f4486d2 | 133 | |
CKMonroe | 0:78436f4486d2 | 134 | // set up interrrupts to be used later for taps |
CKMonroe | 0:78436f4486d2 | 135 | MMA8451QInt1.rise(&GreenLEDBlinker); |
CKMonroe | 0:78436f4486d2 | 136 | MMA8451QInt1.mode(PullNone); |
CKMonroe | 0:78436f4486d2 | 137 | |
CKMonroe | 0:78436f4486d2 | 138 | // set up interrrupts to be used later for taps |
CKMonroe | 0:78436f4486d2 | 139 | ledBlink.attach(&LEDBlinker, LEDDELAY); |
CKMonroe | 0:78436f4486d2 | 140 | |
CKMonroe | 0:78436f4486d2 | 141 | // Read Pulse Source Data and check to see if things have been set |
CKMonroe | 0:78436f4486d2 | 142 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
CKMonroe | 0:78436f4486d2 | 143 | sprintf (lcdData,"%x",regData); |
CKMonroe | 0:78436f4486d2 | 144 | |
CKMonroe | 0:78436f4486d2 | 145 | //CKM: print to terminal//////////////////////// |
CKMonroe | 0:78436f4486d2 | 146 | pc.printf("reg Data: ",regData); |
CKMonroe | 0:78436f4486d2 | 147 | |
CKMonroe | 0:78436f4486d2 | 148 | LCDMess(lcdData,BLINKTIME); |
CKMonroe | 0:78436f4486d2 | 149 | |
CKMonroe | 0:78436f4486d2 | 150 | // *********** Initialize for tap tecognition *********** |
CKMonroe | 0:78436f4486d2 | 151 | regData = latchData | axisData; |
CKMonroe | 0:78436f4486d2 | 152 | acc.setRegisterInStandby(REG_PULSE_CFG, regData); // write the data |
CKMonroe | 0:78436f4486d2 | 153 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
CKMonroe | 0:78436f4486d2 | 154 | sprintf (lcdData,"%x",regData); |
CKMonroe | 0:78436f4486d2 | 155 | |
CKMonroe | 0:78436f4486d2 | 156 | //CKM: print to terminal////////////////////// |
CKMonroe | 0:78436f4486d2 | 157 | pc.printf("reg Data: ",regData); |
CKMonroe | 0:78436f4486d2 | 158 | |
CKMonroe | 0:78436f4486d2 | 159 | LCDMess(lcdData,BLINKTIME); |
CKMonroe | 0:78436f4486d2 | 160 | |
CKMonroe | 0:78436f4486d2 | 161 | // Check to see if accerlometer is alive and well |
CKMonroe | 0:78436f4486d2 | 162 | acc.setGLimit(MAX_4G); // For now set to 2g |
CKMonroe | 0:78436f4486d2 | 163 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
CKMonroe | 0:78436f4486d2 | 164 | sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal |
CKMonroe | 0:78436f4486d2 | 165 | |
CKMonroe | 0:78436f4486d2 | 166 | //CKM: print to terminal///////////////// |
CKMonroe | 0:78436f4486d2 | 167 | pc.printf("reg Data: ",regData); |
CKMonroe | 0:78436f4486d2 | 168 | |
CKMonroe | 0:78436f4486d2 | 169 | LCDMess(lcdData,BLINKTIME); |
CKMonroe | 0:78436f4486d2 | 170 | |
CKMonroe | 0:78436f4486d2 | 171 | //CKM: added this line of code to set the KL into 400Hz mode |
CKMonroe | 0:78436f4486d2 | 172 | acc.setRegisterInStandby(REG_CTRL_REG_1, (REG_CTRL_REG_1 | HZ_MASK) ); |
CKMonroe | 0:78436f4486d2 | 173 | |
CKMonroe | 0:78436f4486d2 | 174 | // Setup single-tap pulse prarameters. |
CKMonroe | 0:78436f4486d2 | 175 | acc.setRegisterInStandby(REG_PULSE_THSZ, SET_THZ); // write the data |
CKMonroe | 0:78436f4486d2 | 176 | acc.setRegisterInStandby(REH_PULSE_TMLT, SET_TMLT); // write the data |
CKMonroe | 0:78436f4486d2 | 177 | |
CKMonroe | 0:78436f4486d2 | 178 | // Set up (pulse) interrupt to INT1 pin |
CKMonroe | 0:78436f4486d2 | 179 | acc.setRegisterInStandby(REG_CTRL_4, SET_INTERRUPT); // write the data |
CKMonroe | 0:78436f4486d2 | 180 | acc.setRegisterInStandby(REG_CTRL_5, SET_INT_LINE); // write the data |
CKMonroe | 0:78436f4486d2 | 181 | // End or seetup |
CKMonroe | 0:78436f4486d2 | 182 | |
CKMonroe | 0:78436f4486d2 | 183 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
CKMonroe | 0:78436f4486d2 | 184 | sprintf (lcdData,"%x",regData); |
CKMonroe | 0:78436f4486d2 | 185 | |
CKMonroe | 0:78436f4486d2 | 186 | //CKM: print to terminal/////////////////// |
CKMonroe | 0:78436f4486d2 | 187 | pc.printf("reg Data: ",regData); |
CKMonroe | 0:78436f4486d2 | 188 | |
CKMonroe | 0:78436f4486d2 | 189 | LCDMess(lcdData,BLINKTIME); |
CKMonroe | 0:78436f4486d2 | 190 | |
CKMonroe | 0:78436f4486d2 | 191 | |
CKMonroe | 0:78436f4486d2 | 192 | while (true) { |
CKMonroe | 0:78436f4486d2 | 193 | acc.readRegs(REG_PULSE_SRC, ®Data, 1); |
CKMonroe | 0:78436f4486d2 | 194 | sprintf (lcdData,"%x",regData); |
CKMonroe | 0:78436f4486d2 | 195 | |
CKMonroe | 0:78436f4486d2 | 196 | //CKM: print to terminal//////////////////// |
CKMonroe | 0:78436f4486d2 | 197 | pc.printf("reg Data: ",regData); |
CKMonroe | 0:78436f4486d2 | 198 | |
CKMonroe | 0:78436f4486d2 | 199 | LCDMess(lcdData,BLINKTIME); |
CKMonroe | 0:78436f4486d2 | 200 | LCDMessNoDwell(LCDMessages[relayState]); |
CKMonroe | 0:78436f4486d2 | 201 | wait(delay); |
CKMonroe | 0:78436f4486d2 | 202 | } |
CKMonroe | 0:78436f4486d2 | 203 | } |