Pulse Detection in z-axis

Dependencies:   MMA8451Q8b SLCD mbed

Committer:
vbharam
Date:
Tue Feb 17 03:52:51 2015 +0000
Revision:
11:adb9adf636cf
Parent:
10:92c7ff5198ee
stan comment

Who changed what in which revision?

UserRevisionLine numberNew 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 9:08acb96591a8 89
scohennm 3:53d47a5dbb2c 90 char lcdData[LCDLEN];
scohennm 3:53d47a5dbb2c 91
scohennm 1:65b0e488f02a 92 myled.write(outState);
scohennm 1:65b0e488f02a 93 relay.write(relayState);
scohennm 3:53d47a5dbb2c 94
scohennm 3:53d47a5dbb2c 95 // set up interrrupts to be used later for taps
vbharam 5:1fe35a74799a 96 MMA8451QInt1.rise(&GreenLEDBlinker);
vbharam 9:08acb96591a8 97 MMA8451QInt1.mode(PullNone);
vbharam 5:1fe35a74799a 98
scohennm 3:53d47a5dbb2c 99 // set up interrrupts to be used later for taps
vbharam 5:1fe35a74799a 100 // mybutton.fall(&pressed);
scohennm 1:65b0e488f02a 101 ledBlink.attach(&LEDBlinker, LEDDELAY);
vbharam 5:1fe35a74799a 102
scohennm 3:53d47a5dbb2c 103 // Check to see if accerlometer is alive and well
scohennm 3:53d47a5dbb2c 104 acc.setGLimit(MAX_2G); // For now set to 2g
scohennm 3:53d47a5dbb2c 105 acc.readRegs(XYZ_DATA_CFG, &regData, 1);
scohennm 3:53d47a5dbb2c 106 sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal
scohennm 3:53d47a5dbb2c 107 LCDMess(lcdData,BLINKTIME);
vbharam 9:08acb96591a8 108
vbharam 9:08acb96591a8 109 // Set up (pulse) interrupt to INT1 pin
vbharam 9:08acb96591a8 110 acc.setRegisterInStandby(REG_CTRL_4, SET_INTERRUPT); // write the data
vbharam 9:08acb96591a8 111 acc.setRegisterInStandby(REG_CTRL_5, SET_INT_LINE); // write the data
vbharam 9:08acb96591a8 112
scohennm 3:53d47a5dbb2c 113 acc.readRegs(REG_WHO_AM_I, &regData, 1);
scohennm 3:53d47a5dbb2c 114 sprintf (lcdData,"%x",regData);
scohennm 3:53d47a5dbb2c 115 LCDMess(lcdData,BLINKTIME);
scohennm 3:53d47a5dbb2c 116
vbharam 10:92c7ff5198ee 117 // Read Pulse Source Data and check to see if things have been set
scohennm 3:53d47a5dbb2c 118 acc.readRegs(REG_PULSE_CFG, &regData, 1); // check it
scohennm 3:53d47a5dbb2c 119 sprintf (lcdData,"%x",regData);
scohennm 3:53d47a5dbb2c 120 LCDMess(lcdData,BLINKTIME);
scohennm 3:53d47a5dbb2c 121
scohennm 4:ba9ca0a0f87e 122 regData = latchData | axisData;
scohennm 4:ba9ca0a0f87e 123 acc.setRegisterInStandby(REG_PULSE_CFG, regData); // write the data
scohennm 3:53d47a5dbb2c 124 acc.readRegs(REG_PULSE_CFG, &regData, 1); // check it
scohennm 3:53d47a5dbb2c 125 sprintf (lcdData,"%x",regData);
scohennm 3:53d47a5dbb2c 126 LCDMess(lcdData,BLINKTIME);
vbharam 10:92c7ff5198ee 127
vbharam 10:92c7ff5198ee 128 while (true) {
scohennm 3:53d47a5dbb2c 129 acc.readRegs(REG_PULSE_SRC, &regData, 1);
scohennm 3:53d47a5dbb2c 130 sprintf (lcdData,"%x",regData);
vbharam 5:1fe35a74799a 131 LCDMess(lcdData,BLINKTIME);
scohennm 3:53d47a5dbb2c 132
scohennm 3:53d47a5dbb2c 133 LCDMessNoDwell(LCDMessages[relayState]);
bcostm 0:912303e63cbd 134 wait(delay);
bcostm 0:912303e63cbd 135 }
vbharam 5:1fe35a74799a 136 }