KBrat-SSD645-Midterm_Part2
Dependencies: MMA8451Q8g_Update SLCD mbed
Fork of KL46z_single_tap_2017 by
Revision 13:24d44a0a1307, committed 2017-03-15
- Comitter:
- tisbrat
- Date:
- Wed Mar 15 16:50:28 2017 +0000
- Parent:
- 12:4774c4fc718e
- Commit message:
- KBrat-SSD645-Midterm_Part2
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/KL46z_double_tap.cpp Wed Mar 15 16:50:28 2017 +0000 @@ -0,0 +1,175 @@ +#include "mbed.h" +#include "MMA8451Q8g.h" +#include "SLCD.h" +#define PROGNAME "KBrat-SSD645-Midterm_Part2\r\n" + +#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//Pulse Source Register +#define REG_PULSE_THSZ 0x25//Threshold on z axis +#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 +//#define SET_INTERRUPT 0x08 //using interrupt INT1 PTC5 +#define SET_INTERRUPT 0x08 //using interrupt INT2 PTD1 +#define SET_INT_LINE 0x00 +//#define SET_THZ 0x20 +//#define SET_TMLT 0x18 +#define SET_THZ 0x28 //See Table 49 in data sheet +/*********** +0b0010 1000 +2.5g/0.063g/count = 40 counts +************/ +#define SET_TMLT 0x1C//0x1C//0x38 // See Table 51 in data sheet +/****** +0b0001 1100 +28 = 0x1C +Time step at 400Hz ODR = 1.25ms +28x1.25 = 35 ms +******/ + + + +//#define PRINTDBUG +// Accelerometer SPI 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 button with internal pullup //INT1 +InterruptIn MMA8451QInt2(PTD1);//push button with internal pullup //INT2 +DigitalOut myled(LED_RED); // red led +DigitalOut relay(LED_GREEN); // green led +Serial pc(USBTX, USBRX); + + 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() +{ + pc.printf(PROGNAME); + //pc.printf("REG_CTRL_4: 0x%2x\r\n", REG_CTRL_4); + //pc.printf("REG_CTRL_5: 0x%2x\r\n", REG_CTRL_5); + uint8_t regData; + uint8_t latchData = 0x40; //0b01000000; //for pulse config register + uint8_t axisData = 0x10; //0b00010000; + + char lcdData[LCDLEN]; + + myled.write(outState); + relay.write(relayState); + +// set up interrupts to be used later for taps + MMA8451QInt2.rise(&GreenLEDBlinker); + MMA8451QInt2.mode(PullNone); + +// set up interrupts 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 recognition *********** + regData = latchData | axisData; + 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 accerlometer is alive and well + acc.setGLimit(MAX_4G); // For now set to 2g + 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 INT2 pin + acc.setRegisterInStandby(REG_CTRL_4, SET_INTERRUPT); // write the data + acc.setRegisterInStandby(REG_CTRL_5, SET_INT_LINE); // write the data + + pc.printf("REG_CTRL_4: 0x%2x\r\n", REG_CTRL_4); + pc.printf("REG_CTRL_5: 0x%2x\r\n", REG_CTRL_5); + pc.printf("REG_PULSE_SRC: 0x%2x\r\n", REG_PULSE_SRC); + pc.printf("REG_PULSE_TMLT: 0x%2x\r\n", REG_PULSE_TMLT); + pc.printf("SET_THZ: 0x%2x\r\n", SET_THZ); + pc.printf("SET_TMLT: 0x%2x\r\n", SET_TMLT); + pc.printf("SET_INTERRUPT: 0x%2x\r\n", SET_INTERRUPT); + pc.printf("SET_INT_LINE: 0x%2x\r\n", SET_INT_LINE); +// End or 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); + } +} \ No newline at end of file
--- a/KL46z_single_tap_2016..cpp Tue Mar 14 16:42:14 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,155 +0,0 @@ -#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 REH_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 -#define SET_INTERRUPT 0x08 //using interrupt INT1 PTC5 -#define SET_INT_LINE 0x08 -#define SET_THZ 0x20 // See Table 49 in data sheet -/*********** -0b0010 0000 -2g/0.063g/count = 32 counts -************/ -#define SET_TMLT 0x18 // See Talbe 51 in data sheet -/****** -0b0001 1000 -24 = 0x18 -time step at 800Hz ODR = 0.625ms -24x0.625 = 15 ms -******/ - -//#define PRINTDBUG -// Accelerometer SPI 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 -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; - uint8_t latchData = 0x40; //0b01000000; //for pulse config register - uint8_t axisData = 0x10; //0b00010000; - - 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 = latchData | axisData; - 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 accerlometer is alive and well - acc.setGLimit(MAX_4G); // For now set to 2g - 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(REH_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 or seetup - - 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); - } -} \ No newline at end of file
--- a/MMA8451Q8g.lib Tue Mar 14 16:42:14 2017 +0000 +++ b/MMA8451Q8g.lib Wed Mar 15 16:50:28 2017 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/scohennm/code/MMA8451Q8b/#12b18fdb72f7 +https://developer.mbed.org/users/tisbrat/code/MMA8451Q8g_Update/#b58b302c53f6