KBrat-SSD645-Midterm

Dependencies:   MMA8451Q8g SLCD mbed

Fork of KL46z_single_tap_2017_blank by Stanley Cohen

Committer:
tisbrat
Date:
Mon Mar 13 14:04:35 2017 +0000
Revision:
13:d07864798cc9
Parent:
12:10e6d0d12833
KBrat-SSD645-Midterm

Who changed what in which revision?

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