Second part of midterm

Dependencies:   MMA8451Q8b SLCD mbed

Fork of KL46z_single_tap_2017 by Stanley Cohen

Committer:
mireyarod23
Date:
Wed Mar 15 14:29:24 2017 +0000
Revision:
13:df4917c65cc7
Parent:
12:4774c4fc718e
Second part of mid term

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 11:c842619c49a1 1 #include "mbed.h"
scohennm 11:c842619c49a1 2 #include "MMA8451Q8g.h"
scohennm 11:c842619c49a1 3 #include "SLCD.h"
scohennm 11:c842619c49a1 4
scohennm 12:4774c4fc718e 5 #define BLINKTIME 1.5
scohennm 11:c842619c49a1 6 #define RELAYON 0
scohennm 11:c842619c49a1 7 #define RELAYOFF 1
scohennm 11:c842619c49a1 8 #define LEDDELAY 0.4
scohennm 11:c842619c49a1 9 #define WAITDELAY 0.7
scohennm 11:c842619c49a1 10 #define LCDLEN 10
scohennm 11:c842619c49a1 11
scohennm 11:c842619c49a1 12 #define REG_WHO_AM_I 0x0D
scohennm 11:c842619c49a1 13 #define XYZ_DATA_CFG 0x0E
scohennm 11:c842619c49a1 14
scohennm 11:c842619c49a1 15 #define REG_OUT_X_MSB 0x01
scohennm 11:c842619c49a1 16 #define REG_OUT_Y_MSB 0x03
scohennm 11:c842619c49a1 17 #define REG_OUT_Z_MSB 0x05
scohennm 11:c842619c49a1 18 #define REG_PULSE_CFG 0x21
scohennm 11:c842619c49a1 19 #define REG_PULSE_SRC 0x22
scohennm 11:c842619c49a1 20 #define REG_PULSE_THSZ 0x25
scohennm 11:c842619c49a1 21 #define REH_PULSE_TMLT 0x26
scohennm 11:c842619c49a1 22 #define REG_CTRL_4 0x2D
scohennm 11:c842619c49a1 23 #define REG_CTRL_5 0x2E
scohennm 11:c842619c49a1 24
scohennm 11:c842619c49a1 25 #define MAX_2G 0x00
scohennm 11:c842619c49a1 26 #define MAX_4G 0x01
scohennm 11:c842619c49a1 27 #define MAX_8G 0x02
mireyarod23 13:df4917c65cc7 28
mireyarod23 13:df4917c65cc7 29 #define SET_INTERRUPT 0x20 //using interrupt INT1 PTC5 CTRL_REG4 0d1010 0000
mireyarod23 13:df4917c65cc7 30 #define SET_INT_LINE 0x00 // CTRL_REG5 register 0d0000 0000 all set to zero to user int2
mireyarod23 13:df4917c65cc7 31
mireyarod23 13:df4917c65cc7 32 /**********
mireyarod23 13:df4917c65cc7 33 Where to set the pulse
mireyarod23 13:df4917c65cc7 34 PULSE_CFG for double tap
mireyarod23 13:df4917c65cc7 35 0d0110 0000 = 60
mireyarod23 13:df4917c65cc7 36 **********/
mireyarod23 13:df4917c65cc7 37
mireyarod23 13:df4917c65cc7 38 #define SET_THZ 0x28 // See Table 49 in data sheet
scohennm 11:c842619c49a1 39 /***********
scohennm 12:4774c4fc718e 40 0b0010 0000
mireyarod23 13:df4917c65cc7 41 4g/0.063g/count = 40 counts = 0d1010 0000
scohennm 11:c842619c49a1 42 ************/
scohennm 11:c842619c49a1 43 #define SET_TMLT 0x18 // See Talbe 51 in data sheet
scohennm 11:c842619c49a1 44 /******
scohennm 12:4774c4fc718e 45 0b0001 1000
scohennm 11:c842619c49a1 46 24 = 0x18
mireyarod23 13:df4917c65cc7 47 time step at 400Hz ODR = 2.5ms
mireyarod23 13:df4917c65cc7 48 24x 1.25 = 28 ms = 1C
scohennm 11:c842619c49a1 49 ******/
scohennm 11:c842619c49a1 50
scohennm 11:c842619c49a1 51 //#define PRINTDBUG
scohennm 11:c842619c49a1 52 // Accelerometer SPI pins
scohennm 11:c842619c49a1 53 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
scohennm 11:c842619c49a1 54 PinName const SDA = PTE25;
scohennm 11:c842619c49a1 55 PinName const SCL = PTE24;
scohennm 11:c842619c49a1 56 #elif defined (TARGET_KL05Z)
scohennm 11:c842619c49a1 57 PinName const SDA = PTB4;
scohennm 11:c842619c49a1 58 PinName const SCL = PTB3;
scohennm 11:c842619c49a1 59 #else
scohennm 11:c842619c49a1 60 #error TARGET NOT DEFINED
scohennm 11:c842619c49a1 61 #endif
scohennm 11:c842619c49a1 62
scohennm 11:c842619c49a1 63 #define MMA8451_I2C_ADDRESS (0x1d<<1)
scohennm 11:c842619c49a1 64
scohennm 11:c842619c49a1 65 Ticker ledBlink; // timinginterrupt for RED led
scohennm 11:c842619c49a1 66 InterruptIn MMA8451QInt1(PTC5); //push botton with internal pullup
scohennm 11:c842619c49a1 67 DigitalOut myled(LED_RED); // red led
scohennm 11:c842619c49a1 68 DigitalOut relay(LED_GREEN); // green led
scohennm 11:c842619c49a1 69
scohennm 11:c842619c49a1 70 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
scohennm 11:c842619c49a1 71
scohennm 11:c842619c49a1 72 float delay = WAITDELAY;
scohennm 11:c842619c49a1 73 int relayState = RELAYOFF;
scohennm 11:c842619c49a1 74 int outState = false;
scohennm 11:c842619c49a1 75 SLCD slcd; //define LCD display
mireyarod23 13:df4917c65cc7 76 Serial pc(USBTX, USBRX); // set up USB as communications to Host PC via USB connectons
scohennm 11:c842619c49a1 77 char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"};
scohennm 11:c842619c49a1 78
scohennm 11:c842619c49a1 79
scohennm 11:c842619c49a1 80 void LCDMess(char *lMess, float dWait){
scohennm 11:c842619c49a1 81 slcd.Home();
scohennm 11:c842619c49a1 82 slcd.clear();
scohennm 11:c842619c49a1 83 slcd.printf(lMess);
scohennm 11:c842619c49a1 84 wait(dWait);
scohennm 11:c842619c49a1 85 }
scohennm 11:c842619c49a1 86 void LCDMessNoDwell(char *lMess){
scohennm 11:c842619c49a1 87 slcd.Home();
scohennm 11:c842619c49a1 88 slcd.clear();
scohennm 11:c842619c49a1 89 slcd.printf(lMess);
scohennm 11:c842619c49a1 90 }
scohennm 11:c842619c49a1 91
scohennm 11:c842619c49a1 92 // Interrupt routines
scohennm 11:c842619c49a1 93 void LEDBlinker(){ // RED LED interrupt
scohennm 11:c842619c49a1 94 outState = !outState;
scohennm 11:c842619c49a1 95 myled.write(outState);
scohennm 11:c842619c49a1 96 }
scohennm 11:c842619c49a1 97
scohennm 11:c842619c49a1 98 void GreenLEDBlinker(){ // Green LED interrupt
scohennm 11:c842619c49a1 99 //uint8_t i_regData;
scohennm 11:c842619c49a1 100 relayState = !relayState;
scohennm 11:c842619c49a1 101 relay.write(relayState);
scohennm 11:c842619c49a1 102 //acc.readRegs(REG_PULSE_SRC, &i_regData, 1); // Clear the tap event
scohennm 11:c842619c49a1 103 }
scohennm 11:c842619c49a1 104
scohennm 11:c842619c49a1 105 // end interrupt routines
scohennm 11:c842619c49a1 106
scohennm 11:c842619c49a1 107 int main()
scohennm 11:c842619c49a1 108 {
scohennm 11:c842619c49a1 109 uint8_t regData;
mireyarod23 13:df4917c65cc7 110 uint8_t latchData = 0x60; //0b01100000; //for pulse config register double tap
scohennm 11:c842619c49a1 111 uint8_t axisData = 0x10; //0b00010000;
scohennm 11:c842619c49a1 112
scohennm 11:c842619c49a1 113 char lcdData[LCDLEN];
scohennm 11:c842619c49a1 114
scohennm 11:c842619c49a1 115 myled.write(outState);
scohennm 11:c842619c49a1 116 relay.write(relayState);
scohennm 11:c842619c49a1 117
scohennm 11:c842619c49a1 118 // set up interrrupts to be used later for taps
scohennm 11:c842619c49a1 119 MMA8451QInt1.rise(&GreenLEDBlinker);
scohennm 11:c842619c49a1 120 MMA8451QInt1.mode(PullNone);
scohennm 11:c842619c49a1 121
scohennm 11:c842619c49a1 122 // set up interrrupts to be used later for taps
scohennm 11:c842619c49a1 123 ledBlink.attach(&LEDBlinker, LEDDELAY);
scohennm 11:c842619c49a1 124
scohennm 11:c842619c49a1 125 // Read Pulse Source Data and check to see if things have been set
scohennm 11:c842619c49a1 126 acc.readRegs(REG_PULSE_CFG, &regData, 1); // check it
scohennm 11:c842619c49a1 127 sprintf (lcdData,"%x",regData);
mireyarod23 13:df4917c65cc7 128 pc.printf("Source register","%x",regData); // source register
scohennm 11:c842619c49a1 129 LCDMess(lcdData,BLINKTIME);
scohennm 11:c842619c49a1 130
scohennm 11:c842619c49a1 131 // *********** Initialize for tap tecognition ***********
scohennm 11:c842619c49a1 132 regData = latchData | axisData;
scohennm 11:c842619c49a1 133 acc.setRegisterInStandby(REG_PULSE_CFG, regData); // write the data
scohennm 11:c842619c49a1 134 acc.readRegs(REG_PULSE_CFG, &regData, 1); // check it
scohennm 11:c842619c49a1 135 sprintf (lcdData,"%x",regData);
mireyarod23 13:df4917c65cc7 136
scohennm 11:c842619c49a1 137 LCDMess(lcdData,BLINKTIME);
scohennm 11:c842619c49a1 138
scohennm 11:c842619c49a1 139 // Check to see if accerlometer is alive and well
scohennm 11:c842619c49a1 140 acc.setGLimit(MAX_4G); // For now set to 2g
scohennm 11:c842619c49a1 141 acc.readRegs(XYZ_DATA_CFG, &regData, 1);
scohennm 11:c842619c49a1 142 sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal
scohennm 11:c842619c49a1 143 LCDMess(lcdData,BLINKTIME);
scohennm 11:c842619c49a1 144
scohennm 11:c842619c49a1 145 // Setup single-tap pulse prarameters.
scohennm 11:c842619c49a1 146 acc.setRegisterInStandby(REG_PULSE_THSZ, SET_THZ); // write the data
mireyarod23 13:df4917c65cc7 147 acc.readRegs(SET_THZ, &regData, 1);
mireyarod23 13:df4917c65cc7 148 pc.printf("Threshold", "%",regData); // Return thresholdz registery
mireyarod23 13:df4917c65cc7 149
mireyarod23 13:df4917c65cc7 150 acc.setRegisterInStandby(REH_PULSE_TMLT, SET_TMLT); // write the data
mireyarod23 13:df4917c65cc7 151 acc.readRegs(SET_TMLT, &regData, 1);
mireyarod23 13:df4917c65cc7 152 pc.printf("Time limit", "%",regData); // Return time limit registery
scohennm 11:c842619c49a1 153
mireyarod23 13:df4917c65cc7 154 // Set up (pulse) interrupt to INT2 pin
scohennm 11:c842619c49a1 155 acc.setRegisterInStandby(REG_CTRL_4, SET_INTERRUPT); // write the data
mireyarod23 13:df4917c65cc7 156 pc.printf("REG_4", SET_INTERRUPT );
scohennm 11:c842619c49a1 157 acc.setRegisterInStandby(REG_CTRL_5, SET_INT_LINE); // write the data
mireyarod23 13:df4917c65cc7 158 pc.printf("REG_5", SET_INT_LINE );
scohennm 11:c842619c49a1 159 // End or seetup
scohennm 11:c842619c49a1 160
scohennm 11:c842619c49a1 161 acc.readRegs(REG_WHO_AM_I, &regData, 1);
scohennm 11:c842619c49a1 162 sprintf (lcdData,"%x",regData);
mireyarod23 13:df4917c65cc7 163 pc.printf("Main REG", "%x",regData);
scohennm 11:c842619c49a1 164 LCDMess(lcdData,BLINKTIME);
scohennm 11:c842619c49a1 165
scohennm 11:c842619c49a1 166
scohennm 11:c842619c49a1 167 while (true) {
scohennm 11:c842619c49a1 168 acc.readRegs(REG_PULSE_SRC, &regData, 1);
scohennm 11:c842619c49a1 169 sprintf (lcdData,"%x",regData);
mireyarod23 13:df4917c65cc7 170 pc.printf("Reg pulse", "%x", regData);
scohennm 11:c842619c49a1 171 LCDMess(lcdData,BLINKTIME);
scohennm 11:c842619c49a1 172 LCDMessNoDwell(LCDMessages[relayState]);
scohennm 11:c842619c49a1 173 wait(delay);
scohennm 11:c842619c49a1 174 }
scohennm 11:c842619c49a1 175 }