![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Midterm defines.
Dependencies: MMA8451Q8b SLCD mbed
Fork of KL46z_single_tap_2017_blank by
KL46z_single_tap_2017.cpp@13:d7cfc3110df9, 2017-03-13 (annotated)
- Committer:
- mturner5
- Date:
- Mon Mar 13 10:11:22 2017 +0000
- Revision:
- 13:d7cfc3110df9
- Parent:
- 12:10e6d0d12833
midterm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 12:10e6d0d12833 | 1 | #include "mbed.h" |
scohennm | 12:10e6d0d12833 | 2 | #include "MMA8451Q8g.h" |
scohennm | 12:10e6d0d12833 | 3 | #include "SLCD.h" |
scohennm | 12:10e6d0d12833 | 4 | |
scohennm | 12:10e6d0d12833 | 5 | #define BLINKTIME 1.5 |
scohennm | 12:10e6d0d12833 | 6 | #define RELAYON 0 |
scohennm | 12:10e6d0d12833 | 7 | #define RELAYOFF 1 |
scohennm | 12:10e6d0d12833 | 8 | #define LEDDELAY 0.4 |
scohennm | 12:10e6d0d12833 | 9 | #define WAITDELAY 0.7 |
scohennm | 12:10e6d0d12833 | 10 | #define LCDLEN 10 |
scohennm | 12:10e6d0d12833 | 11 | |
scohennm | 12:10e6d0d12833 | 12 | #define REG_WHO_AM_I 0x0D |
scohennm | 12:10e6d0d12833 | 13 | #define XYZ_DATA_CFG 0x0E |
scohennm | 12:10e6d0d12833 | 14 | |
scohennm | 12:10e6d0d12833 | 15 | #define REG_OUT_X_MSB 0x01 |
scohennm | 12:10e6d0d12833 | 16 | #define REG_OUT_Y_MSB 0x03 |
scohennm | 12:10e6d0d12833 | 17 | #define REG_OUT_Z_MSB 0x05 |
scohennm | 12:10e6d0d12833 | 18 | #define REG_PULSE_CFG 0x21 |
scohennm | 12:10e6d0d12833 | 19 | #define REG_PULSE_SRC 0x22 |
scohennm | 12:10e6d0d12833 | 20 | #define REG_PULSE_THSZ 0x25 |
scohennm | 12:10e6d0d12833 | 21 | #define REH_PULSE_TMLT 0x26 |
scohennm | 12:10e6d0d12833 | 22 | #define REG_CTRL_4 0x2D |
scohennm | 12:10e6d0d12833 | 23 | #define REG_CTRL_5 0x2E |
scohennm | 12:10e6d0d12833 | 24 | |
scohennm | 12:10e6d0d12833 | 25 | #define MAX_2G 0x00 |
scohennm | 12:10e6d0d12833 | 26 | #define MAX_4G 0x01 |
scohennm | 12:10e6d0d12833 | 27 | #define MAX_8G 0x02 |
scohennm | 12:10e6d0d12833 | 28 | /************************************** |
scohennm | 12:10e6d0d12833 | 29 | You need to fill nn the missing numbers below for this to work |
scohennm | 12:10e6d0d12833 | 30 | 0x00 is just a place holder |
scohennm | 12:10e6d0d12833 | 31 | *************************************/ |
mturner5 | 13:d7cfc3110df9 | 32 | #define LATCH_DATA 0x40 // for use in REG_PULSE_CFG |
mturner5 | 13:d7cfc3110df9 | 33 | #define AXIS_DATA 0x10 |
mturner5 | 13:d7cfc3110df9 | 34 | #define SET_INTERRUPT 0x08 //looks like step 1 for setting interrupts. //Initialy thought this was 0x2E, |
mturner5 | 13:d7cfc3110df9 | 35 | //but after seeing the process for setting interrupts, it looks like it is a |
mturner5 | 13:d7cfc3110df9 | 36 | //1,2 process, so thus 0x08 seems like the first step and then 0x08, the second |
mturner5 | 13:d7cfc3110df9 | 37 | //but again, is just a theory. |
mturner5 | 13:d7cfc3110df9 | 38 | |
mturner5 | 13:d7cfc3110df9 | 39 | #define SET_INT_LINE 0x08 //looks like step 2 |
scohennm | 12:10e6d0d12833 | 40 | |
mturner5 | 13:d7cfc3110df9 | 41 | #define SET_THZ 0x02 // See Table 49 in data sheet //0x2A seems like the most logical hex to use. |
scohennm | 12:10e6d0d12833 | 42 | |
mturner5 | 13:d7cfc3110df9 | 43 | #define SET_TMLT 0x0F // See Table 51 in data sheet //0x0F is 15 ms I think |
scohennm | 12:10e6d0d12833 | 44 | /*********************************************************/ |
scohennm | 12:10e6d0d12833 | 45 | |
scohennm | 12:10e6d0d12833 | 46 | //#define PRINTDBUG |
scohennm | 12:10e6d0d12833 | 47 | // Accelerometer I2C pins |
scohennm | 12:10e6d0d12833 | 48 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 12:10e6d0d12833 | 49 | PinName const SDA = PTE25; |
scohennm | 12:10e6d0d12833 | 50 | PinName const SCL = PTE24; |
scohennm | 12:10e6d0d12833 | 51 | #elif defined (TARGET_KL05Z) |
scohennm | 12:10e6d0d12833 | 52 | PinName const SDA = PTB4; |
scohennm | 12:10e6d0d12833 | 53 | PinName const SCL = PTB3; |
scohennm | 12:10e6d0d12833 | 54 | #else |
scohennm | 12:10e6d0d12833 | 55 | #error TARGET NOT DEFINED |
scohennm | 12:10e6d0d12833 | 56 | #endif |
scohennm | 12:10e6d0d12833 | 57 | |
scohennm | 12:10e6d0d12833 | 58 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 12:10e6d0d12833 | 59 | |
scohennm | 12:10e6d0d12833 | 60 | Ticker ledBlink; // timinginterrupt for RED led |
mturner5 | 13:d7cfc3110df9 | 61 | InterruptIn MMA8451QInt1(PTC5); //push botton with internal pullup <<- already set? |
scohennm | 12:10e6d0d12833 | 62 | //InterruptIn MMA8451QInt1(PTD1); |
scohennm | 12:10e6d0d12833 | 63 | DigitalOut myled(LED_RED); // red led |
scohennm | 12:10e6d0d12833 | 64 | DigitalOut relay(LED_GREEN); // green led |
scohennm | 12:10e6d0d12833 | 65 | |
scohennm | 12:10e6d0d12833 | 66 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 12:10e6d0d12833 | 67 | |
scohennm | 12:10e6d0d12833 | 68 | float delay = WAITDELAY; |
scohennm | 12:10e6d0d12833 | 69 | int relayState = RELAYOFF; |
scohennm | 12:10e6d0d12833 | 70 | int outState = false; |
scohennm | 12:10e6d0d12833 | 71 | SLCD slcd; //define LCD display |
scohennm | 12:10e6d0d12833 | 72 | char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"}; |
scohennm | 12:10e6d0d12833 | 73 | |
scohennm | 12:10e6d0d12833 | 74 | |
scohennm | 12:10e6d0d12833 | 75 | void LCDMess(char *lMess, float dWait){ |
scohennm | 12:10e6d0d12833 | 76 | slcd.Home(); |
scohennm | 12:10e6d0d12833 | 77 | slcd.clear(); |
scohennm | 12:10e6d0d12833 | 78 | slcd.printf(lMess); |
scohennm | 12:10e6d0d12833 | 79 | wait(dWait); |
scohennm | 12:10e6d0d12833 | 80 | } |
scohennm | 12:10e6d0d12833 | 81 | void LCDMessNoDwell(char *lMess){ |
scohennm | 12:10e6d0d12833 | 82 | slcd.Home(); |
scohennm | 12:10e6d0d12833 | 83 | slcd.clear(); |
scohennm | 12:10e6d0d12833 | 84 | slcd.printf(lMess); |
scohennm | 12:10e6d0d12833 | 85 | } |
scohennm | 12:10e6d0d12833 | 86 | |
scohennm | 12:10e6d0d12833 | 87 | // Interrupt routines |
scohennm | 12:10e6d0d12833 | 88 | void LEDBlinker(){ // RED LED interrupt |
scohennm | 12:10e6d0d12833 | 89 | outState = !outState; |
scohennm | 12:10e6d0d12833 | 90 | myled.write(outState); |
scohennm | 12:10e6d0d12833 | 91 | } |
scohennm | 12:10e6d0d12833 | 92 | |
scohennm | 12:10e6d0d12833 | 93 | void GreenLEDBlinker(){ // Green LED interrupt |
scohennm | 12:10e6d0d12833 | 94 | //uint8_t i_regData; |
scohennm | 12:10e6d0d12833 | 95 | relayState = !relayState; |
scohennm | 12:10e6d0d12833 | 96 | relay.write(relayState); |
scohennm | 12:10e6d0d12833 | 97 | //acc.readRegs(REG_PULSE_SRC, &i_regData, 1); // Clear the tap event |
scohennm | 12:10e6d0d12833 | 98 | } |
scohennm | 12:10e6d0d12833 | 99 | |
scohennm | 12:10e6d0d12833 | 100 | // end interrupt routines |
scohennm | 12:10e6d0d12833 | 101 | |
scohennm | 12:10e6d0d12833 | 102 | int main() |
scohennm | 12:10e6d0d12833 | 103 | { |
scohennm | 12:10e6d0d12833 | 104 | uint8_t regData; |
scohennm | 12:10e6d0d12833 | 105 | |
scohennm | 12:10e6d0d12833 | 106 | |
scohennm | 12:10e6d0d12833 | 107 | char lcdData[LCDLEN]; |
scohennm | 12:10e6d0d12833 | 108 | |
scohennm | 12:10e6d0d12833 | 109 | myled.write(outState); |
scohennm | 12:10e6d0d12833 | 110 | relay.write(relayState); |
scohennm | 12:10e6d0d12833 | 111 | |
scohennm | 12:10e6d0d12833 | 112 | // set up interrrupts to be used later for taps |
scohennm | 12:10e6d0d12833 | 113 | MMA8451QInt1.rise(&GreenLEDBlinker); |
scohennm | 12:10e6d0d12833 | 114 | MMA8451QInt1.mode(PullNone); |
scohennm | 12:10e6d0d12833 | 115 | |
scohennm | 12:10e6d0d12833 | 116 | // set up interrrupts to be used later for taps |
scohennm | 12:10e6d0d12833 | 117 | ledBlink.attach(&LEDBlinker, LEDDELAY); |
scohennm | 12:10e6d0d12833 | 118 | |
scohennm | 12:10e6d0d12833 | 119 | // Read Pulse Source Data and check to see if things have been set |
scohennm | 12:10e6d0d12833 | 120 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 12:10e6d0d12833 | 121 | sprintf (lcdData,"%x",regData); |
scohennm | 12:10e6d0d12833 | 122 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 123 | |
scohennm | 12:10e6d0d12833 | 124 | // *********** Initialize for tap tecognition *********** |
scohennm | 12:10e6d0d12833 | 125 | regData = LATCH_DATA | AXIS_DATA; |
scohennm | 12:10e6d0d12833 | 126 | acc.setRegisterInStandby(REG_PULSE_CFG, regData); // write the data |
scohennm | 12:10e6d0d12833 | 127 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 12:10e6d0d12833 | 128 | sprintf (lcdData,"%x",regData); |
scohennm | 12:10e6d0d12833 | 129 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 130 | |
scohennm | 12:10e6d0d12833 | 131 | // Check to see if accerlometer is alive and well |
mturner5 | 13:d7cfc3110df9 | 132 | acc.setGLimit(MAX_2G); //this is set to 2g. |
scohennm | 12:10e6d0d12833 | 133 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
scohennm | 12:10e6d0d12833 | 134 | sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal |
scohennm | 12:10e6d0d12833 | 135 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 136 | |
scohennm | 12:10e6d0d12833 | 137 | // Setup single-tap pulse prarameters. |
scohennm | 12:10e6d0d12833 | 138 | acc.setRegisterInStandby(REG_PULSE_THSZ, SET_THZ); // write the data |
scohennm | 12:10e6d0d12833 | 139 | acc.setRegisterInStandby(REH_PULSE_TMLT, SET_TMLT); // write the data |
scohennm | 12:10e6d0d12833 | 140 | |
scohennm | 12:10e6d0d12833 | 141 | // Set up (pulse) interrupt to INT1 pin |
scohennm | 12:10e6d0d12833 | 142 | acc.setRegisterInStandby(REG_CTRL_4, SET_INTERRUPT); // write the data |
scohennm | 12:10e6d0d12833 | 143 | acc.setRegisterInStandby(REG_CTRL_5, SET_INT_LINE); // write the data |
scohennm | 12:10e6d0d12833 | 144 | // End or seetup |
scohennm | 12:10e6d0d12833 | 145 | |
scohennm | 12:10e6d0d12833 | 146 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
scohennm | 12:10e6d0d12833 | 147 | sprintf (lcdData,"%x",regData); |
scohennm | 12:10e6d0d12833 | 148 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 149 | |
scohennm | 12:10e6d0d12833 | 150 | |
scohennm | 12:10e6d0d12833 | 151 | while (true) { |
scohennm | 12:10e6d0d12833 | 152 | acc.readRegs(REG_PULSE_SRC, ®Data, 1); |
scohennm | 12:10e6d0d12833 | 153 | sprintf (lcdData,"%x",regData); |
scohennm | 12:10e6d0d12833 | 154 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 155 | LCDMessNoDwell(LCDMessages[relayState]); |
scohennm | 12:10e6d0d12833 | 156 | wait(delay); |
scohennm | 12:10e6d0d12833 | 157 | } |
scohennm | 12:10e6d0d12833 | 158 | } |