Pulse Detection in z-axis
Dependencies: MMA8451Q8b SLCD mbed
main.cpp@5:1fe35a74799a, 2015-02-16 (annotated)
- Committer:
- vbharam
- Date:
- Mon Feb 16 18:18:34 2015 +0000
- Revision:
- 5:1fe35a74799a
- Parent:
- 4:ba9ca0a0f87e
- Child:
- 6:825780dcf426
Setting interrupt for tap
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:912303e63cbd | 1 | #include "mbed.h" |
scohennm | 4:ba9ca0a0f87e | 2 | #include "MMA8451Q8g.h" |
scohennm | 1:65b0e488f02a | 3 | #include "SLCD.h" |
scohennm | 3:53d47a5dbb2c | 4 | |
scohennm | 3:53d47a5dbb2c | 5 | #define BLINKTIME 0.7 |
scohennm | 3:53d47a5dbb2c | 6 | #define RELAYON 0 |
scohennm | 3:53d47a5dbb2c | 7 | #define RELAYOFF 1 |
scohennm | 3:53d47a5dbb2c | 8 | #define LEDDELAY 0.75 |
scohennm | 4:ba9ca0a0f87e | 9 | #define WAITDELAY 0.7 |
scohennm | 3:53d47a5dbb2c | 10 | #define LCDLEN 10 |
scohennm | 3:53d47a5dbb2c | 11 | |
scohennm | 3:53d47a5dbb2c | 12 | #define REG_WHO_AM_I 0x0D |
scohennm | 3:53d47a5dbb2c | 13 | #define XYZ_DATA_CFG 0x0E |
scohennm | 3:53d47a5dbb2c | 14 | |
scohennm | 3:53d47a5dbb2c | 15 | #define REG_OUT_X_MSB 0x01 |
scohennm | 3:53d47a5dbb2c | 16 | #define REG_OUT_Y_MSB 0x03 |
scohennm | 3:53d47a5dbb2c | 17 | #define REG_OUT_Z_MSB 0x05 |
vbharam | 5:1fe35a74799a | 18 | #define REG_PULSE_CFG 0x21 |
scohennm | 3:53d47a5dbb2c | 19 | #define REG_PULSE_SRC 0x22 |
vbharam | 5:1fe35a74799a | 20 | #define REG_PULSE_THSZ 0x25 |
vbharam | 5:1fe35a74799a | 21 | #define REG_CTRL_4 0x2D |
vbharam | 5:1fe35a74799a | 22 | #define REG_CTRL_5 0x2E |
scohennm | 3:53d47a5dbb2c | 23 | |
scohennm | 3:53d47a5dbb2c | 24 | #define MAX_2G 0x00 |
scohennm | 3:53d47a5dbb2c | 25 | #define MAX_4G 0x01 |
scohennm | 3:53d47a5dbb2c | 26 | #define MAX_8G 0x02 |
vbharam | 5:1fe35a74799a | 27 | #define SET_INTERRUPT 0x08 |
vbharam | 5:1fe35a74799a | 28 | #define SET_INT_LINE 0x08 |
vbharam | 5:1fe35a74799a | 29 | |
scohennm | 3:53d47a5dbb2c | 30 | |
scohennm | 3:53d47a5dbb2c | 31 | //#define PRINTDBUG |
scohennm | 3:53d47a5dbb2c | 32 | // Accelerometer SPI pins |
scohennm | 3:53d47a5dbb2c | 33 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 3:53d47a5dbb2c | 34 | PinName const SDA = PTE25; |
scohennm | 3:53d47a5dbb2c | 35 | PinName const SCL = PTE24; |
scohennm | 3:53d47a5dbb2c | 36 | #elif defined (TARGET_KL05Z) |
scohennm | 3:53d47a5dbb2c | 37 | PinName const SDA = PTB4; |
scohennm | 3:53d47a5dbb2c | 38 | PinName const SCL = PTB3; |
scohennm | 3:53d47a5dbb2c | 39 | #else |
scohennm | 3:53d47a5dbb2c | 40 | #error TARGET NOT DEFINED |
scohennm | 3:53d47a5dbb2c | 41 | #endif |
scohennm | 3:53d47a5dbb2c | 42 | |
scohennm | 3:53d47a5dbb2c | 43 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
bcostm | 0:912303e63cbd | 44 | |
scohennm | 1:65b0e488f02a | 45 | Ticker ledBlink; // timinginterrupt for RED led |
vbharam | 5:1fe35a74799a | 46 | //InterruptIn mybutton(PTC3); //push botton with internal pullup |
vbharam | 5:1fe35a74799a | 47 | InterruptIn MMA8451QInt1(PTC5); //push botton with internal pullup |
scohennm | 1:65b0e488f02a | 48 | DigitalOut myled(LED_RED); // red led |
scohennm | 1:65b0e488f02a | 49 | DigitalOut relay(LED_GREEN); // green led |
scohennm | 1:65b0e488f02a | 50 | |
scohennm | 3:53d47a5dbb2c | 51 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
bcostm | 0:912303e63cbd | 52 | |
scohennm | 1:65b0e488f02a | 53 | float delay = WAITDELAY; |
scohennm | 1:65b0e488f02a | 54 | int relayState = RELAYOFF; |
scohennm | 1:65b0e488f02a | 55 | int outState = false; |
scohennm | 1:65b0e488f02a | 56 | SLCD slcd; //define LCD display |
scohennm | 3:53d47a5dbb2c | 57 | char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"}; |
scohennm | 1:65b0e488f02a | 58 | |
scohennm | 1:65b0e488f02a | 59 | |
scohennm | 3:53d47a5dbb2c | 60 | void LCDMess(char *lMess, float dWait){ |
scohennm | 3:53d47a5dbb2c | 61 | slcd.Home(); |
scohennm | 1:65b0e488f02a | 62 | slcd.clear(); |
scohennm | 1:65b0e488f02a | 63 | slcd.printf(lMess); |
scohennm | 3:53d47a5dbb2c | 64 | wait(dWait); |
scohennm | 3:53d47a5dbb2c | 65 | } |
scohennm | 3:53d47a5dbb2c | 66 | void LCDMessNoDwell(char *lMess){ |
scohennm | 3:53d47a5dbb2c | 67 | slcd.Home(); |
scohennm | 3:53d47a5dbb2c | 68 | slcd.clear(); |
scohennm | 3:53d47a5dbb2c | 69 | slcd.printf(lMess); |
scohennm | 3:53d47a5dbb2c | 70 | } |
scohennm | 3:53d47a5dbb2c | 71 | |
scohennm | 1:65b0e488f02a | 72 | |
scohennm | 1:65b0e488f02a | 73 | void LEDBlinker(){ // RED LED interrupt |
scohennm | 1:65b0e488f02a | 74 | outState = !outState; |
scohennm | 1:65b0e488f02a | 75 | myled.write(outState); |
scohennm | 1:65b0e488f02a | 76 | } |
vbharam | 5:1fe35a74799a | 77 | |
vbharam | 5:1fe35a74799a | 78 | void GreenLEDBlinker(){ // Green LED interrupt |
vbharam | 5:1fe35a74799a | 79 | relayState = !relayState; |
scohennm | 1:65b0e488f02a | 80 | relay.write(relayState); |
bcostm | 0:912303e63cbd | 81 | } |
vbharam | 5:1fe35a74799a | 82 | |
scohennm | 1:65b0e488f02a | 83 | |
bcostm | 0:912303e63cbd | 84 | int main() |
bcostm | 0:912303e63cbd | 85 | { |
scohennm | 3:53d47a5dbb2c | 86 | uint8_t regData = MAX_4G; |
scohennm | 3:53d47a5dbb2c | 87 | uint8_t latchData = 0x40; //0b01000000; //for pulse config register |
scohennm | 3:53d47a5dbb2c | 88 | uint8_t axisData = 0x10; //0b00010000; |
vbharam | 5:1fe35a74799a | 89 | uint8_t interrupt1Data = 0x08; //0b00001000; |
vbharam | 5:1fe35a74799a | 90 | uint8_t interrupt2Data = 0x04; //0b00000100; |
scohennm | 3:53d47a5dbb2c | 91 | char lcdData[LCDLEN]; |
scohennm | 3:53d47a5dbb2c | 92 | |
scohennm | 1:65b0e488f02a | 93 | myled.write(outState); |
scohennm | 1:65b0e488f02a | 94 | relay.write(relayState); |
scohennm | 3:53d47a5dbb2c | 95 | |
scohennm | 3:53d47a5dbb2c | 96 | // set up interrrupts to be used later for taps |
vbharam | 5:1fe35a74799a | 97 | MMA8451QInt1.rise(&GreenLEDBlinker); |
vbharam | 5:1fe35a74799a | 98 | MMA8451QInt1.mode(PullNone); |
vbharam | 5:1fe35a74799a | 99 | |
scohennm | 3:53d47a5dbb2c | 100 | // set up interrrupts to be used later for taps |
vbharam | 5:1fe35a74799a | 101 | // mybutton.fall(&pressed); |
scohennm | 1:65b0e488f02a | 102 | ledBlink.attach(&LEDBlinker, LEDDELAY); |
vbharam | 5:1fe35a74799a | 103 | |
scohennm | 3:53d47a5dbb2c | 104 | // Check to see if accerlometer is alive and well |
scohennm | 3:53d47a5dbb2c | 105 | acc.setGLimit(MAX_2G); // For now set to 2g |
scohennm | 3:53d47a5dbb2c | 106 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 107 | sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal |
scohennm | 3:53d47a5dbb2c | 108 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 109 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 110 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 111 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 112 | |
vbharam | 5:1fe35a74799a | 113 | |
vbharam | 5:1fe35a74799a | 114 | // Set up (pulse) interrupt to INT1 pin |
vbharam | 5:1fe35a74799a | 115 | regData = interrupt1Data; |
vbharam | 5:1fe35a74799a | 116 | acc.setRegisterInStandby(REG_CTRL_4, regData); // write the data |
vbharam | 5:1fe35a74799a | 117 | regData = interrupt2Data; |
vbharam | 5:1fe35a74799a | 118 | acc.setRegisterInStandby(REG_CTRL_5, regData); // write the data |
vbharam | 5:1fe35a74799a | 119 | |
scohennm | 1:65b0e488f02a | 120 | while (true) { |
scohennm | 3:53d47a5dbb2c | 121 | |
scohennm | 3:53d47a5dbb2c | 122 | // Read Pulse Source Data and check to see if things have been set |
scohennm | 3:53d47a5dbb2c | 123 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 3:53d47a5dbb2c | 124 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 125 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 126 | |
scohennm | 4:ba9ca0a0f87e | 127 | regData = latchData | axisData; |
scohennm | 4:ba9ca0a0f87e | 128 | acc.setRegisterInStandby(REG_PULSE_CFG, regData); // write the data |
scohennm | 3:53d47a5dbb2c | 129 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 3:53d47a5dbb2c | 130 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 131 | LCDMess(lcdData,BLINKTIME); |
vbharam | 5:1fe35a74799a | 132 | |
scohennm | 3:53d47a5dbb2c | 133 | acc.readRegs(REG_PULSE_SRC, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 134 | sprintf (lcdData,"%x",regData); |
vbharam | 5:1fe35a74799a | 135 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 136 | |
scohennm | 3:53d47a5dbb2c | 137 | LCDMessNoDwell(LCDMessages[relayState]); |
bcostm | 0:912303e63cbd | 138 | wait(delay); |
bcostm | 0:912303e63cbd | 139 | } |
vbharam | 5:1fe35a74799a | 140 | } |