KLUJA-SSD446-Midterm
Dependencies: MMA8451Q8b SLCD mbed
Fork of KL46z_single_tap_2017_blank by
KL46z_single_tap_2017.cpp@12:10e6d0d12833, 2017-03-09 (annotated)
- Committer:
- scohennm
- Date:
- Thu Mar 09 02:37:39 2017 +0000
- Revision:
- 12:10e6d0d12833
- Child:
- 13:49f888ad923f
Blank parameters for 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 | *************************************/ |
scohennm | 12:10e6d0d12833 | 32 | #define LATCH_DATA 0x00 // for use in REG_PULSE_CFG |
scohennm | 12:10e6d0d12833 | 33 | #define AXIS_DATA 0x00 |
scohennm | 12:10e6d0d12833 | 34 | #define SET_INTERRUPT 0x00 //using interrupt INT1 PTC5 |
scohennm | 12:10e6d0d12833 | 35 | #define SET_INT_LINE 0x00 |
scohennm | 12:10e6d0d12833 | 36 | |
scohennm | 12:10e6d0d12833 | 37 | #define SET_THZ 0x00 // See Table 49 in data sheet |
scohennm | 12:10e6d0d12833 | 38 | |
scohennm | 12:10e6d0d12833 | 39 | #define SET_TMLT 0x00 // See Table 51 in data sheet |
scohennm | 12:10e6d0d12833 | 40 | /*********************************************************/ |
scohennm | 12:10e6d0d12833 | 41 | |
scohennm | 12:10e6d0d12833 | 42 | //#define PRINTDBUG |
scohennm | 12:10e6d0d12833 | 43 | // Accelerometer I2C pins |
scohennm | 12:10e6d0d12833 | 44 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 12:10e6d0d12833 | 45 | PinName const SDA = PTE25; |
scohennm | 12:10e6d0d12833 | 46 | PinName const SCL = PTE24; |
scohennm | 12:10e6d0d12833 | 47 | #elif defined (TARGET_KL05Z) |
scohennm | 12:10e6d0d12833 | 48 | PinName const SDA = PTB4; |
scohennm | 12:10e6d0d12833 | 49 | PinName const SCL = PTB3; |
scohennm | 12:10e6d0d12833 | 50 | #else |
scohennm | 12:10e6d0d12833 | 51 | #error TARGET NOT DEFINED |
scohennm | 12:10e6d0d12833 | 52 | #endif |
scohennm | 12:10e6d0d12833 | 53 | |
scohennm | 12:10e6d0d12833 | 54 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 12:10e6d0d12833 | 55 | |
scohennm | 12:10e6d0d12833 | 56 | Ticker ledBlink; // timinginterrupt for RED led |
scohennm | 12:10e6d0d12833 | 57 | InterruptIn MMA8451QInt1(PTC5); //push botton with internal pullup |
scohennm | 12:10e6d0d12833 | 58 | //InterruptIn MMA8451QInt1(PTD1); |
scohennm | 12:10e6d0d12833 | 59 | DigitalOut myled(LED_RED); // red led |
scohennm | 12:10e6d0d12833 | 60 | DigitalOut relay(LED_GREEN); // green led |
scohennm | 12:10e6d0d12833 | 61 | |
scohennm | 12:10e6d0d12833 | 62 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 12:10e6d0d12833 | 63 | |
scohennm | 12:10e6d0d12833 | 64 | float delay = WAITDELAY; |
scohennm | 12:10e6d0d12833 | 65 | int relayState = RELAYOFF; |
scohennm | 12:10e6d0d12833 | 66 | int outState = false; |
scohennm | 12:10e6d0d12833 | 67 | SLCD slcd; //define LCD display |
scohennm | 12:10e6d0d12833 | 68 | char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"}; |
scohennm | 12:10e6d0d12833 | 69 | |
scohennm | 12:10e6d0d12833 | 70 | |
scohennm | 12:10e6d0d12833 | 71 | void LCDMess(char *lMess, float dWait){ |
scohennm | 12:10e6d0d12833 | 72 | slcd.Home(); |
scohennm | 12:10e6d0d12833 | 73 | slcd.clear(); |
scohennm | 12:10e6d0d12833 | 74 | slcd.printf(lMess); |
scohennm | 12:10e6d0d12833 | 75 | wait(dWait); |
scohennm | 12:10e6d0d12833 | 76 | } |
scohennm | 12:10e6d0d12833 | 77 | void LCDMessNoDwell(char *lMess){ |
scohennm | 12:10e6d0d12833 | 78 | slcd.Home(); |
scohennm | 12:10e6d0d12833 | 79 | slcd.clear(); |
scohennm | 12:10e6d0d12833 | 80 | slcd.printf(lMess); |
scohennm | 12:10e6d0d12833 | 81 | } |
scohennm | 12:10e6d0d12833 | 82 | |
scohennm | 12:10e6d0d12833 | 83 | // Interrupt routines |
scohennm | 12:10e6d0d12833 | 84 | void LEDBlinker(){ // RED LED interrupt |
scohennm | 12:10e6d0d12833 | 85 | outState = !outState; |
scohennm | 12:10e6d0d12833 | 86 | myled.write(outState); |
scohennm | 12:10e6d0d12833 | 87 | } |
scohennm | 12:10e6d0d12833 | 88 | |
scohennm | 12:10e6d0d12833 | 89 | void GreenLEDBlinker(){ // Green LED interrupt |
scohennm | 12:10e6d0d12833 | 90 | //uint8_t i_regData; |
scohennm | 12:10e6d0d12833 | 91 | relayState = !relayState; |
scohennm | 12:10e6d0d12833 | 92 | relay.write(relayState); |
scohennm | 12:10e6d0d12833 | 93 | //acc.readRegs(REG_PULSE_SRC, &i_regData, 1); // Clear the tap event |
scohennm | 12:10e6d0d12833 | 94 | } |
scohennm | 12:10e6d0d12833 | 95 | |
scohennm | 12:10e6d0d12833 | 96 | // end interrupt routines |
scohennm | 12:10e6d0d12833 | 97 | |
scohennm | 12:10e6d0d12833 | 98 | int main() |
scohennm | 12:10e6d0d12833 | 99 | { |
scohennm | 12:10e6d0d12833 | 100 | uint8_t regData; |
scohennm | 12:10e6d0d12833 | 101 | |
scohennm | 12:10e6d0d12833 | 102 | |
scohennm | 12:10e6d0d12833 | 103 | char lcdData[LCDLEN]; |
scohennm | 12:10e6d0d12833 | 104 | |
scohennm | 12:10e6d0d12833 | 105 | myled.write(outState); |
scohennm | 12:10e6d0d12833 | 106 | relay.write(relayState); |
scohennm | 12:10e6d0d12833 | 107 | |
scohennm | 12:10e6d0d12833 | 108 | // set up interrrupts to be used later for taps |
scohennm | 12:10e6d0d12833 | 109 | MMA8451QInt1.rise(&GreenLEDBlinker); |
scohennm | 12:10e6d0d12833 | 110 | MMA8451QInt1.mode(PullNone); |
scohennm | 12:10e6d0d12833 | 111 | |
scohennm | 12:10e6d0d12833 | 112 | // set up interrrupts to be used later for taps |
scohennm | 12:10e6d0d12833 | 113 | ledBlink.attach(&LEDBlinker, LEDDELAY); |
scohennm | 12:10e6d0d12833 | 114 | |
scohennm | 12:10e6d0d12833 | 115 | // Read Pulse Source Data and check to see if things have been set |
scohennm | 12:10e6d0d12833 | 116 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 12:10e6d0d12833 | 117 | sprintf (lcdData,"%x",regData); |
scohennm | 12:10e6d0d12833 | 118 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 119 | |
scohennm | 12:10e6d0d12833 | 120 | // *********** Initialize for tap tecognition *********** |
scohennm | 12:10e6d0d12833 | 121 | regData = LATCH_DATA | AXIS_DATA; |
scohennm | 12:10e6d0d12833 | 122 | acc.setRegisterInStandby(REG_PULSE_CFG, regData); // write the data |
scohennm | 12:10e6d0d12833 | 123 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 12:10e6d0d12833 | 124 | sprintf (lcdData,"%x",regData); |
scohennm | 12:10e6d0d12833 | 125 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 126 | |
scohennm | 12:10e6d0d12833 | 127 | // Check to see if accerlometer is alive and well |
scohennm | 12:10e6d0d12833 | 128 | acc.setGLimit(MAX_4G); // For now set to 2g |
scohennm | 12:10e6d0d12833 | 129 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
scohennm | 12:10e6d0d12833 | 130 | sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal |
scohennm | 12:10e6d0d12833 | 131 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 132 | |
scohennm | 12:10e6d0d12833 | 133 | // Setup single-tap pulse prarameters. |
scohennm | 12:10e6d0d12833 | 134 | acc.setRegisterInStandby(REG_PULSE_THSZ, SET_THZ); // write the data |
scohennm | 12:10e6d0d12833 | 135 | acc.setRegisterInStandby(REH_PULSE_TMLT, SET_TMLT); // write the data |
scohennm | 12:10e6d0d12833 | 136 | |
scohennm | 12:10e6d0d12833 | 137 | // Set up (pulse) interrupt to INT1 pin |
scohennm | 12:10e6d0d12833 | 138 | acc.setRegisterInStandby(REG_CTRL_4, SET_INTERRUPT); // write the data |
scohennm | 12:10e6d0d12833 | 139 | acc.setRegisterInStandby(REG_CTRL_5, SET_INT_LINE); // write the data |
scohennm | 12:10e6d0d12833 | 140 | // End or seetup |
scohennm | 12:10e6d0d12833 | 141 | |
scohennm | 12:10e6d0d12833 | 142 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
scohennm | 12:10e6d0d12833 | 143 | sprintf (lcdData,"%x",regData); |
scohennm | 12:10e6d0d12833 | 144 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 145 | |
scohennm | 12:10e6d0d12833 | 146 | |
scohennm | 12:10e6d0d12833 | 147 | while (true) { |
scohennm | 12:10e6d0d12833 | 148 | acc.readRegs(REG_PULSE_SRC, ®Data, 1); |
scohennm | 12:10e6d0d12833 | 149 | sprintf (lcdData,"%x",regData); |
scohennm | 12:10e6d0d12833 | 150 | LCDMess(lcdData,BLINKTIME); |
scohennm | 12:10e6d0d12833 | 151 | LCDMessNoDwell(LCDMessages[relayState]); |
scohennm | 12:10e6d0d12833 | 152 | wait(delay); |
scohennm | 12:10e6d0d12833 | 153 | } |
scohennm | 12:10e6d0d12833 | 154 | } |