![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SB-Midterm
Dependencies: MMA8451Q8b SLCD mbed
Fork of KL46z_single_tap_2017_blank by
KL46z_single_tap_2017.cpp
- Committer:
- sbart
- Date:
- 2017-03-13
- Revision:
- 14:65d6d50062ed
- Parent:
- 13:c7b3e97f9bc6
- Child:
- 15:fb9e64c59187
File content as of revision 14:65d6d50062ed:
#include "mbed.h" #include "MMA8451Q8g.h" #include "SLCD.h" #define BLINKTIME 1.5 #define RELAYON 0 #define RELAYOFF 1 #define LEDDELAY 0.4 #define WAITDELAY 0.7 #define LCDLEN 10 #define REG_WHO_AM_I 0x0D #define XYZ_DATA_CFG 0x0E #define REG_OUT_X_MSB 0x01 #define REG_OUT_Y_MSB 0x03 #define REG_OUT_Z_MSB 0x05 #define REG_PULSE_CFG 0x21 #define REG_PULSE_SRC 0x22 #define REG_PULSE_THSZ 0x25 #define REG_PULSE_TMLT 0x26 #define REG_CTRL_4 0x2D #define REG_CTRL_5 0x2E #define MAX_2G 0x00 #define MAX_4G 0x01 #define MAX_8G 0x02 /************************************** You need to fill in the missing numbers below for this to work 0x00 is just a place holder *************************************/ //SB - sets the pulse register for the Z-axis single tap //line 140 - OR these two numbers to make 11010000 to enable single tap on the Z-axis //Mainly want to enable bit 4 #define LATCH_DATA 0xC0 #define AXIS_DATA 0x10 //SB - Sets the interrupt for only the Z-axis single tap //Activate bit 3 with 00000100 #define SET_INTERRUPT 0x08 //SB - sets the interrupt line for the Z-axis single tap to INT1 //Activate bit 3 with 00000100 #define SET_INT_LINE 0x08 //SB - Sets the Z Threshold to 2.016g //The threshold values range from 1 to 127 with steps of 0.063 g/LSB //00100000 = 32 : 32 * 0.063g = 2.016g #define SET_THZ 0x20 //SB - Sets the pulse time limit to 15 ms //At 800Hz, the Normal time step in ms is 0.625 //00011000 = 24 : 24 * 0.625ms = 15ms #define SET_TMLT 0x18 //SB - By default, the data rate is set to 800Hz - Register 0x2A CTRL_REG_1 //SB - The g sensitivty is set to 4g later in the program (line 148) /*********************************************************/ //#define PRINTDBUG // Accelerometer I2C pins #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) PinName const SDA = PTE25; PinName const SCL = PTE24; #elif defined (TARGET_KL05Z) PinName const SDA = PTB4; PinName const SCL = PTB3; #else #error TARGET NOT DEFINED #endif #define MMA8451_I2C_ADDRESS (0x1d<<1) Ticker ledBlink; // timinginterrupt for RED led InterruptIn MMA8451QInt1(PTC5); //push botton with internal pullup //InterruptIn MMA8451QInt1(PTD1); DigitalOut myled(LED_RED); // red led DigitalOut relay(LED_GREEN); // green led MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); float delay = WAITDELAY; int relayState = RELAYOFF; int outState = false; SLCD slcd; //define LCD display char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"}; void LCDMess(char *lMess, float dWait){ slcd.Home(); slcd.clear(); slcd.printf(lMess); wait(dWait); } void LCDMessNoDwell(char *lMess){ slcd.Home(); slcd.clear(); slcd.printf(lMess); } // Interrupt routines void LEDBlinker(){ // RED LED interrupt outState = !outState; myled.write(outState); } void GreenLEDBlinker(){ // Green LED interrupt //uint8_t i_regData; relayState = !relayState; relay.write(relayState); //acc.readRegs(REG_PULSE_SRC, &i_regData, 1); // Clear the tap event } // end interrupt routines int main() { uint8_t regData; char lcdData[LCDLEN]; myled.write(outState); relay.write(relayState); // set up interrrupts to be used later for taps MMA8451QInt1.rise(&GreenLEDBlinker); MMA8451QInt1.mode(PullNone); // set up interrrupts to be used later for taps ledBlink.attach(&LEDBlinker, LEDDELAY); // Read Pulse Source Data and check to see if things have been set acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it sprintf (lcdData,"%x",regData); LCDMess(lcdData,BLINKTIME); // *********** Initialize for tap tecognition *********** regData = LATCH_DATA | AXIS_DATA; acc.setRegisterInStandby(REG_PULSE_CFG, regData); // write the data acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it sprintf (lcdData,"%x",regData); LCDMess(lcdData,BLINKTIME); // Check to see if accelerometer is alive and well acc.setGLimit(MAX_4G); //SB - sets the g sensitivity to 4g acc.readRegs(XYZ_DATA_CFG, ®Data, 1); sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal LCDMess(lcdData,BLINKTIME); // Setup single-tap pulse prarameters. acc.setRegisterInStandby(REG_PULSE_THSZ, SET_THZ); // write the data acc.setRegisterInStandby(REG_PULSE_TMLT, SET_TMLT); // write the data // Set up (pulse) interrupt to INT1 pin acc.setRegisterInStandby(REG_CTRL_4, SET_INTERRUPT); // write the data acc.setRegisterInStandby(REG_CTRL_5, SET_INT_LINE); // write the data // End of setup acc.readRegs(REG_WHO_AM_I, ®Data, 1); sprintf (lcdData,"%x",regData); LCDMess(lcdData,BLINKTIME); while (true) { acc.readRegs(REG_PULSE_SRC, ®Data, 1); sprintf (lcdData,"%x",regData); LCDMess(lcdData,BLINKTIME); LCDMessNoDwell(LCDMessages[relayState]); wait(delay); } }