The whole program
Dependencies: MMA8451Q8 SLCD mbed
Fork of KL46z_single_tap_empty by
main.cpp@4:d75c7e69796b, 2015-02-09 (annotated)
- Committer:
- bomalley
- Date:
- Mon Feb 09 18:19:28 2015 +0000
- Revision:
- 4:d75c7e69796b
- Parent:
- 3:53d47a5dbb2c
pgm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:912303e63cbd | 1 | #include "mbed.h" |
scohennm | 2:17a0550771c4 | 2 | #include "MMA8451Q8.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 | 3:53d47a5dbb2c | 9 | #define WAITDELAY 3.0 |
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 |
scohennm | 3:53d47a5dbb2c | 18 | #define REG_PULSE_SRC 0x22 |
scohennm | 3:53d47a5dbb2c | 19 | #define REG_PULSE_CFG 0x21 |
scohennm | 3:53d47a5dbb2c | 20 | |
bomalley | 4:d75c7e69796b | 21 | #define REG_PULSE_THSX 0x23 |
bomalley | 4:d75c7e69796b | 22 | #define REG_PULSE_THSY 0x24 |
bomalley | 4:d75c7e69796b | 23 | #define REG_PULSE_THSZ 0x25 |
bomalley | 4:d75c7e69796b | 24 | #define REG_PULSE_TMLT 0x26 |
bomalley | 4:d75c7e69796b | 25 | #define REG_PULSE_LTCY 0x27 |
bomalley | 4:d75c7e69796b | 26 | #define REG_PULSE_WIND 0x28 |
bomalley | 4:d75c7e69796b | 27 | |
bomalley | 4:d75c7e69796b | 28 | |
scohennm | 3:53d47a5dbb2c | 29 | #define MAX_2G 0x00 |
scohennm | 3:53d47a5dbb2c | 30 | #define MAX_4G 0x01 |
scohennm | 3:53d47a5dbb2c | 31 | #define MAX_8G 0x02 |
scohennm | 3:53d47a5dbb2c | 32 | |
scohennm | 3:53d47a5dbb2c | 33 | //#define PRINTDBUG |
scohennm | 3:53d47a5dbb2c | 34 | // Accelerometer SPI pins |
scohennm | 3:53d47a5dbb2c | 35 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 3:53d47a5dbb2c | 36 | PinName const SDA = PTE25; |
scohennm | 3:53d47a5dbb2c | 37 | PinName const SCL = PTE24; |
scohennm | 3:53d47a5dbb2c | 38 | #elif defined (TARGET_KL05Z) |
scohennm | 3:53d47a5dbb2c | 39 | PinName const SDA = PTB4; |
scohennm | 3:53d47a5dbb2c | 40 | PinName const SCL = PTB3; |
scohennm | 3:53d47a5dbb2c | 41 | #else |
scohennm | 3:53d47a5dbb2c | 42 | #error TARGET NOT DEFINED |
scohennm | 3:53d47a5dbb2c | 43 | #endif |
scohennm | 3:53d47a5dbb2c | 44 | |
scohennm | 3:53d47a5dbb2c | 45 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
bcostm | 0:912303e63cbd | 46 | |
scohennm | 1:65b0e488f02a | 47 | Ticker ledBlink; // timinginterrupt for RED led |
scohennm | 1:65b0e488f02a | 48 | InterruptIn mybutton(PTC3); //push botton with internal pullup |
scohennm | 1:65b0e488f02a | 49 | DigitalOut myled(LED_RED); // red led |
scohennm | 1:65b0e488f02a | 50 | DigitalOut relay(LED_GREEN); // green led |
scohennm | 1:65b0e488f02a | 51 | |
bomalley | 4:d75c7e69796b | 52 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
bcostm | 0:912303e63cbd | 53 | |
scohennm | 1:65b0e488f02a | 54 | float delay = WAITDELAY; |
scohennm | 1:65b0e488f02a | 55 | int relayState = RELAYOFF; |
scohennm | 1:65b0e488f02a | 56 | int outState = false; |
scohennm | 1:65b0e488f02a | 57 | SLCD slcd; //define LCD display |
scohennm | 3:53d47a5dbb2c | 58 | char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"}; |
bomalley | 4:d75c7e69796b | 59 | uint8_t registerArray[8] = {REG_PULSE_CFG, REG_PULSE_SRC, REG_PULSE_THSX, REG_PULSE_THSY, REG_PULSE_THSZ, REG_PULSE_TMLT, REG_PULSE_LTCY, REG_PULSE_WIND}; |
bomalley | 4:d75c7e69796b | 60 | uint8_t regAddress; |
scohennm | 1:65b0e488f02a | 61 | |
scohennm | 1:65b0e488f02a | 62 | |
scohennm | 3:53d47a5dbb2c | 63 | void LCDMess(char *lMess, float dWait){ |
scohennm | 3:53d47a5dbb2c | 64 | slcd.Home(); |
scohennm | 1:65b0e488f02a | 65 | slcd.clear(); |
scohennm | 1:65b0e488f02a | 66 | slcd.printf(lMess); |
scohennm | 3:53d47a5dbb2c | 67 | wait(dWait); |
scohennm | 3:53d47a5dbb2c | 68 | } |
scohennm | 3:53d47a5dbb2c | 69 | void LCDMessNoDwell(char *lMess){ |
scohennm | 3:53d47a5dbb2c | 70 | slcd.Home(); |
scohennm | 3:53d47a5dbb2c | 71 | slcd.clear(); |
scohennm | 3:53d47a5dbb2c | 72 | slcd.printf(lMess); |
scohennm | 3:53d47a5dbb2c | 73 | } |
scohennm | 3:53d47a5dbb2c | 74 | |
scohennm | 1:65b0e488f02a | 75 | |
scohennm | 1:65b0e488f02a | 76 | void LEDBlinker(){ // RED LED interrupt |
scohennm | 1:65b0e488f02a | 77 | outState = !outState; |
scohennm | 1:65b0e488f02a | 78 | myled.write(outState); |
scohennm | 1:65b0e488f02a | 79 | } |
scohennm | 1:65b0e488f02a | 80 | |
scohennm | 1:65b0e488f02a | 81 | |
scohennm | 1:65b0e488f02a | 82 | void pressed() // button intterupt |
bcostm | 0:912303e63cbd | 83 | { |
scohennm | 1:65b0e488f02a | 84 | relayState = !relayState; |
scohennm | 1:65b0e488f02a | 85 | relay.write(relayState); |
bcostm | 0:912303e63cbd | 86 | } |
scohennm | 1:65b0e488f02a | 87 | |
bcostm | 0:912303e63cbd | 88 | int main() |
bcostm | 0:912303e63cbd | 89 | { |
scohennm | 3:53d47a5dbb2c | 90 | uint8_t regData = MAX_4G; |
scohennm | 3:53d47a5dbb2c | 91 | uint8_t latchData = 0x40; //0b01000000; //for pulse config register |
scohennm | 3:53d47a5dbb2c | 92 | uint8_t axisData = 0x10; //0b00010000; |
scohennm | 3:53d47a5dbb2c | 93 | char lcdData[LCDLEN]; |
scohennm | 3:53d47a5dbb2c | 94 | |
scohennm | 1:65b0e488f02a | 95 | myled.write(outState); |
scohennm | 1:65b0e488f02a | 96 | relay.write(relayState); |
scohennm | 3:53d47a5dbb2c | 97 | |
scohennm | 3:53d47a5dbb2c | 98 | // set up interrrupts to be used later for taps |
bcostm | 0:912303e63cbd | 99 | mybutton.fall(&pressed); |
scohennm | 3:53d47a5dbb2c | 100 | // set up interrrupts to be used later for taps |
scohennm | 1:65b0e488f02a | 101 | ledBlink.attach(&LEDBlinker, LEDDELAY); |
scohennm | 3:53d47a5dbb2c | 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, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 106 | sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal |
scohennm | 3:53d47a5dbb2c | 107 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 108 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 109 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 110 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 111 | |
scohennm | 1:65b0e488f02a | 112 | while (true) { |
scohennm | 3:53d47a5dbb2c | 113 | |
scohennm | 3:53d47a5dbb2c | 114 | // Read Pulse Source Data and check to see if things have been set |
scohennm | 3:53d47a5dbb2c | 115 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 3:53d47a5dbb2c | 116 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 117 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 118 | |
scohennm | 3:53d47a5dbb2c | 119 | acc.setPulseConfiguration(latchData, axisData); // write the data |
scohennm | 3:53d47a5dbb2c | 120 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 3:53d47a5dbb2c | 121 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 122 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 123 | |
bomalley | 4:d75c7e69796b | 124 | //send address |
bomalley | 4:d75c7e69796b | 125 | regData = latchData | axisData; |
bomalley | 4:d75c7e69796b | 126 | regAddress = 0; |
bomalley | 4:d75c7e69796b | 127 | acc.setRegisterInStandby(registerArray[regAddress], regData); |
scohennm | 3:53d47a5dbb2c | 128 | acc.readRegs(REG_PULSE_SRC, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 129 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 130 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 131 | |
scohennm | 3:53d47a5dbb2c | 132 | LCDMessNoDwell(LCDMessages[relayState]); |
bcostm | 0:912303e63cbd | 133 | wait(delay); |
bcostm | 0:912303e63cbd | 134 | } |
bcostm | 0:912303e63cbd | 135 | } |