Midterm V2 Pt2
Dependencies: MMA8451Q8b SLCD mbed
Fork of KL46z_single_tap_2017 by
KL46z_single_tap_2016..cpp@13:535c003295df, 2017-03-15 (annotated)
- Committer:
- annalou
- Date:
- Wed Mar 15 15:52:38 2017 +0000
- Revision:
- 13:535c003295df
- Parent:
- 12:4774c4fc718e
ALM_MIdtermV2Pt2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 11:c842619c49a1 | 1 | #include "mbed.h" |
scohennm | 11:c842619c49a1 | 2 | #include "MMA8451Q8g.h" |
scohennm | 11:c842619c49a1 | 3 | #include "SLCD.h" |
scohennm | 11:c842619c49a1 | 4 | |
annalou | 13:535c003295df | 5 | #define PROGNAME "AnnaLouise Martinez_midtermPt2\n\r" |
scohennm | 12:4774c4fc718e | 6 | #define BLINKTIME 1.5 |
scohennm | 11:c842619c49a1 | 7 | #define RELAYON 0 |
scohennm | 11:c842619c49a1 | 8 | #define RELAYOFF 1 |
scohennm | 11:c842619c49a1 | 9 | #define LEDDELAY 0.4 |
scohennm | 11:c842619c49a1 | 10 | #define WAITDELAY 0.7 |
scohennm | 11:c842619c49a1 | 11 | #define LCDLEN 10 |
scohennm | 11:c842619c49a1 | 12 | |
scohennm | 11:c842619c49a1 | 13 | #define REG_WHO_AM_I 0x0D |
scohennm | 11:c842619c49a1 | 14 | #define XYZ_DATA_CFG 0x0E |
scohennm | 11:c842619c49a1 | 15 | |
scohennm | 11:c842619c49a1 | 16 | #define REG_OUT_X_MSB 0x01 |
scohennm | 11:c842619c49a1 | 17 | #define REG_OUT_Y_MSB 0x03 |
scohennm | 11:c842619c49a1 | 18 | #define REG_OUT_Z_MSB 0x05 |
scohennm | 11:c842619c49a1 | 19 | #define REG_PULSE_CFG 0x21 |
scohennm | 11:c842619c49a1 | 20 | #define REG_PULSE_SRC 0x22 |
scohennm | 11:c842619c49a1 | 21 | #define REG_PULSE_THSZ 0x25 |
scohennm | 11:c842619c49a1 | 22 | #define REH_PULSE_TMLT 0x26 |
scohennm | 11:c842619c49a1 | 23 | #define REG_CTRL_4 0x2D |
scohennm | 11:c842619c49a1 | 24 | #define REG_CTRL_5 0x2E |
scohennm | 11:c842619c49a1 | 25 | |
scohennm | 11:c842619c49a1 | 26 | #define MAX_2G 0x00 |
scohennm | 11:c842619c49a1 | 27 | #define MAX_4G 0x01 |
scohennm | 11:c842619c49a1 | 28 | #define MAX_8G 0x02 |
scohennm | 11:c842619c49a1 | 29 | #define SET_INTERRUPT 0x08 //using interrupt INT1 PTC5 |
scohennm | 11:c842619c49a1 | 30 | #define SET_INT_LINE 0x08 |
scohennm | 11:c842619c49a1 | 31 | #define SET_THZ 0x20 // See Table 49 in data sheet |
scohennm | 11:c842619c49a1 | 32 | /*********** |
scohennm | 12:4774c4fc718e | 33 | 0b0010 0000 |
scohennm | 11:c842619c49a1 | 34 | 2g/0.063g/count = 32 counts |
scohennm | 11:c842619c49a1 | 35 | ************/ |
scohennm | 11:c842619c49a1 | 36 | #define SET_TMLT 0x18 // See Talbe 51 in data sheet |
scohennm | 11:c842619c49a1 | 37 | /****** |
scohennm | 12:4774c4fc718e | 38 | 0b0001 1000 |
scohennm | 11:c842619c49a1 | 39 | 24 = 0x18 |
annalou | 13:535c003295df | 40 | time step at 400Hz ODR = 0.625ms |
scohennm | 11:c842619c49a1 | 41 | 24x0.625 = 15 ms |
scohennm | 11:c842619c49a1 | 42 | ******/ |
scohennm | 11:c842619c49a1 | 43 | |
scohennm | 11:c842619c49a1 | 44 | //#define PRINTDBUG |
scohennm | 11:c842619c49a1 | 45 | // Accelerometer SPI pins |
scohennm | 11:c842619c49a1 | 46 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 11:c842619c49a1 | 47 | PinName const SDA = PTE25; |
scohennm | 11:c842619c49a1 | 48 | PinName const SCL = PTE24; |
scohennm | 11:c842619c49a1 | 49 | #elif defined (TARGET_KL05Z) |
scohennm | 11:c842619c49a1 | 50 | PinName const SDA = PTB4; |
scohennm | 11:c842619c49a1 | 51 | PinName const SCL = PTB3; |
scohennm | 11:c842619c49a1 | 52 | #else |
scohennm | 11:c842619c49a1 | 53 | #error TARGET NOT DEFINED |
scohennm | 11:c842619c49a1 | 54 | #endif |
scohennm | 11:c842619c49a1 | 55 | |
scohennm | 11:c842619c49a1 | 56 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 11:c842619c49a1 | 57 | |
scohennm | 11:c842619c49a1 | 58 | Ticker ledBlink; // timinginterrupt for RED led |
annalou | 13:535c003295df | 59 | InterruptIn MMA8451QInt2(PTD1); //push botton with internal pullup |
scohennm | 11:c842619c49a1 | 60 | DigitalOut myled(LED_RED); // red led |
scohennm | 11:c842619c49a1 | 61 | DigitalOut relay(LED_GREEN); // green led |
scohennm | 11:c842619c49a1 | 62 | |
scohennm | 11:c842619c49a1 | 63 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 11:c842619c49a1 | 64 | |
scohennm | 11:c842619c49a1 | 65 | float delay = WAITDELAY; |
scohennm | 11:c842619c49a1 | 66 | int relayState = RELAYOFF; |
scohennm | 11:c842619c49a1 | 67 | int outState = false; |
annalou | 13:535c003295df | 68 | Serial pc(USBTX, USBRX); |
scohennm | 11:c842619c49a1 | 69 | SLCD slcd; //define LCD display |
scohennm | 11:c842619c49a1 | 70 | char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"}; |
scohennm | 11:c842619c49a1 | 71 | |
scohennm | 11:c842619c49a1 | 72 | |
scohennm | 11:c842619c49a1 | 73 | void LCDMess(char *lMess, float dWait){ |
scohennm | 11:c842619c49a1 | 74 | slcd.Home(); |
scohennm | 11:c842619c49a1 | 75 | slcd.clear(); |
scohennm | 11:c842619c49a1 | 76 | slcd.printf(lMess); |
scohennm | 11:c842619c49a1 | 77 | wait(dWait); |
scohennm | 11:c842619c49a1 | 78 | } |
scohennm | 11:c842619c49a1 | 79 | void LCDMessNoDwell(char *lMess){ |
scohennm | 11:c842619c49a1 | 80 | slcd.Home(); |
scohennm | 11:c842619c49a1 | 81 | slcd.clear(); |
scohennm | 11:c842619c49a1 | 82 | slcd.printf(lMess); |
scohennm | 11:c842619c49a1 | 83 | } |
scohennm | 11:c842619c49a1 | 84 | |
scohennm | 11:c842619c49a1 | 85 | // Interrupt routines |
scohennm | 11:c842619c49a1 | 86 | void LEDBlinker(){ // RED LED interrupt |
scohennm | 11:c842619c49a1 | 87 | outState = !outState; |
scohennm | 11:c842619c49a1 | 88 | myled.write(outState); |
scohennm | 11:c842619c49a1 | 89 | } |
scohennm | 11:c842619c49a1 | 90 | |
scohennm | 11:c842619c49a1 | 91 | void GreenLEDBlinker(){ // Green LED interrupt |
scohennm | 11:c842619c49a1 | 92 | //uint8_t i_regData; |
scohennm | 11:c842619c49a1 | 93 | relayState = !relayState; |
scohennm | 11:c842619c49a1 | 94 | relay.write(relayState); |
scohennm | 11:c842619c49a1 | 95 | //acc.readRegs(REG_PULSE_SRC, &i_regData, 1); // Clear the tap event |
scohennm | 11:c842619c49a1 | 96 | } |
scohennm | 11:c842619c49a1 | 97 | |
scohennm | 11:c842619c49a1 | 98 | // end interrupt routines |
scohennm | 11:c842619c49a1 | 99 | |
scohennm | 11:c842619c49a1 | 100 | int main() |
scohennm | 11:c842619c49a1 | 101 | { |
scohennm | 11:c842619c49a1 | 102 | uint8_t regData; |
scohennm | 11:c842619c49a1 | 103 | uint8_t latchData = 0x40; //0b01000000; //for pulse config register |
scohennm | 11:c842619c49a1 | 104 | uint8_t axisData = 0x10; //0b00010000; |
scohennm | 11:c842619c49a1 | 105 | |
scohennm | 11:c842619c49a1 | 106 | char lcdData[LCDLEN]; |
scohennm | 11:c842619c49a1 | 107 | |
scohennm | 11:c842619c49a1 | 108 | myled.write(outState); |
scohennm | 11:c842619c49a1 | 109 | relay.write(relayState); |
annalou | 13:535c003295df | 110 | pc.printf(PROGNAME); |
scohennm | 11:c842619c49a1 | 111 | // set up interrrupts to be used later for taps |
annalou | 13:535c003295df | 112 | MMA8451QInt2.rise(&GreenLEDBlinker); |
annalou | 13:535c003295df | 113 | MMA8451QInt2.mode(PullNone); |
scohennm | 11:c842619c49a1 | 114 | |
scohennm | 11:c842619c49a1 | 115 | // set up interrrupts to be used later for taps |
scohennm | 11:c842619c49a1 | 116 | ledBlink.attach(&LEDBlinker, LEDDELAY); |
scohennm | 11:c842619c49a1 | 117 | |
scohennm | 11:c842619c49a1 | 118 | // Read Pulse Source Data and check to see if things have been set |
scohennm | 11:c842619c49a1 | 119 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 11:c842619c49a1 | 120 | sprintf (lcdData,"%x",regData); |
annalou | 13:535c003295df | 121 | pc.printf("THIS IS SOURCE REG:", "%x",REG_PULSE_CFG, "\n"); |
scohennm | 11:c842619c49a1 | 122 | LCDMess(lcdData,BLINKTIME); |
scohennm | 11:c842619c49a1 | 123 | |
scohennm | 11:c842619c49a1 | 124 | // *********** Initialize for tap tecognition *********** |
scohennm | 11:c842619c49a1 | 125 | regData = latchData | axisData; |
scohennm | 11:c842619c49a1 | 126 | acc.setRegisterInStandby(REG_PULSE_CFG, regData); // write the data |
scohennm | 11:c842619c49a1 | 127 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 11:c842619c49a1 | 128 | sprintf (lcdData,"%x",regData); |
scohennm | 11:c842619c49a1 | 129 | LCDMess(lcdData,BLINKTIME); |
scohennm | 11:c842619c49a1 | 130 | |
scohennm | 11:c842619c49a1 | 131 | // Check to see if accerlometer is alive and well |
annalou | 13:535c003295df | 132 | acc.setGLimit(MAX_2G); // For now set to 2g |
scohennm | 11:c842619c49a1 | 133 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
scohennm | 11:c842619c49a1 | 134 | sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal |
scohennm | 11:c842619c49a1 | 135 | LCDMess(lcdData,BLINKTIME); |
scohennm | 11:c842619c49a1 | 136 | |
scohennm | 11:c842619c49a1 | 137 | // Setup single-tap pulse prarameters. |
scohennm | 11:c842619c49a1 | 138 | acc.setRegisterInStandby(REG_PULSE_THSZ, SET_THZ); // write the data |
scohennm | 11:c842619c49a1 | 139 | acc.setRegisterInStandby(REH_PULSE_TMLT, SET_TMLT); // write the data |
scohennm | 11:c842619c49a1 | 140 | |
scohennm | 11:c842619c49a1 | 141 | // Set up (pulse) interrupt to INT1 pin |
scohennm | 11:c842619c49a1 | 142 | acc.setRegisterInStandby(REG_CTRL_4, SET_INTERRUPT); // write the data |
scohennm | 11:c842619c49a1 | 143 | acc.setRegisterInStandby(REG_CTRL_5, SET_INT_LINE); // write the data |
scohennm | 11:c842619c49a1 | 144 | // End or seetup |
scohennm | 11:c842619c49a1 | 145 | |
scohennm | 11:c842619c49a1 | 146 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
scohennm | 11:c842619c49a1 | 147 | sprintf (lcdData,"%x",regData); |
scohennm | 11:c842619c49a1 | 148 | LCDMess(lcdData,BLINKTIME); |
scohennm | 11:c842619c49a1 | 149 | |
scohennm | 11:c842619c49a1 | 150 | |
scohennm | 11:c842619c49a1 | 151 | while (true) { |
scohennm | 11:c842619c49a1 | 152 | acc.readRegs(REG_PULSE_SRC, ®Data, 1); |
scohennm | 11:c842619c49a1 | 153 | sprintf (lcdData,"%x",regData); |
scohennm | 11:c842619c49a1 | 154 | LCDMess(lcdData,BLINKTIME); |
scohennm | 11:c842619c49a1 | 155 | LCDMessNoDwell(LCDMessages[relayState]); |
scohennm | 11:c842619c49a1 | 156 | wait(delay); |
scohennm | 11:c842619c49a1 | 157 | } |
scohennm | 11:c842619c49a1 | 158 | } |