![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
The whole program
Dependencies: MMA8451Q8_v2 SLCD mbed
Fork of KL46z_single_tap_Project_1_v1 by
main.cpp@5:0930b17d337d, 2015-02-16 (annotated)
- Committer:
- bomalley
- Date:
- Mon Feb 16 03:23:33 2015 +0000
- Revision:
- 5:0930b17d337d
- Parent:
- 4:d75c7e69796b
- Child:
- 6:990e73b26c15
Project v1
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_THSZ 0x25 |
bomalley | 4:d75c7e69796b | 22 | #define REG_PULSE_TMLT 0x26 |
bomalley | 4:d75c7e69796b | 23 | #define REG_PULSE_LTCY 0x27 |
bomalley | 5:0930b17d337d | 24 | |
bomalley | 5:0930b17d337d | 25 | #define REG_CTRL_REG4 0x2D |
bomalley | 5:0930b17d337d | 26 | #define REG_CTRL_REG5 0x2E |
bomalley | 4:d75c7e69796b | 27 | |
bomalley | 5:0930b17d337d | 28 | #define REG_DATA_THSZ 0x08 |
bomalley | 5:0930b17d337d | 29 | #define REG_DATA_TMLT 0x26 |
bomalley | 5:0930b17d337d | 30 | #define REG_DATA_LTCY 0x27 |
bomalley | 4:d75c7e69796b | 31 | |
scohennm | 3:53d47a5dbb2c | 32 | #define MAX_2G 0x00 |
scohennm | 3:53d47a5dbb2c | 33 | #define MAX_4G 0x01 |
scohennm | 3:53d47a5dbb2c | 34 | #define MAX_8G 0x02 |
scohennm | 3:53d47a5dbb2c | 35 | |
scohennm | 3:53d47a5dbb2c | 36 | //#define PRINTDBUG |
scohennm | 3:53d47a5dbb2c | 37 | // Accelerometer SPI pins |
scohennm | 3:53d47a5dbb2c | 38 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 3:53d47a5dbb2c | 39 | PinName const SDA = PTE25; |
scohennm | 3:53d47a5dbb2c | 40 | PinName const SCL = PTE24; |
scohennm | 3:53d47a5dbb2c | 41 | #elif defined (TARGET_KL05Z) |
scohennm | 3:53d47a5dbb2c | 42 | PinName const SDA = PTB4; |
scohennm | 3:53d47a5dbb2c | 43 | PinName const SCL = PTB3; |
scohennm | 3:53d47a5dbb2c | 44 | #else |
scohennm | 3:53d47a5dbb2c | 45 | #error TARGET NOT DEFINED |
scohennm | 3:53d47a5dbb2c | 46 | #endif |
scohennm | 3:53d47a5dbb2c | 47 | |
scohennm | 3:53d47a5dbb2c | 48 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
bcostm | 0:912303e63cbd | 49 | |
scohennm | 1:65b0e488f02a | 50 | Ticker ledBlink; // timinginterrupt for RED led |
scohennm | 1:65b0e488f02a | 51 | InterruptIn mybutton(PTC3); //push botton with internal pullup |
scohennm | 1:65b0e488f02a | 52 | DigitalOut myled(LED_RED); // red led |
scohennm | 1:65b0e488f02a | 53 | DigitalOut relay(LED_GREEN); // green led |
scohennm | 1:65b0e488f02a | 54 | |
bomalley | 4:d75c7e69796b | 55 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
bcostm | 0:912303e63cbd | 56 | |
scohennm | 1:65b0e488f02a | 57 | float delay = WAITDELAY; |
scohennm | 1:65b0e488f02a | 58 | int relayState = RELAYOFF; |
scohennm | 1:65b0e488f02a | 59 | int outState = false; |
scohennm | 1:65b0e488f02a | 60 | SLCD slcd; //define LCD display |
scohennm | 3:53d47a5dbb2c | 61 | char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"}; |
scohennm | 1:65b0e488f02a | 62 | |
scohennm | 1:65b0e488f02a | 63 | |
scohennm | 3:53d47a5dbb2c | 64 | void LCDMess(char *lMess, float dWait){ |
scohennm | 3:53d47a5dbb2c | 65 | slcd.Home(); |
scohennm | 1:65b0e488f02a | 66 | slcd.clear(); |
scohennm | 1:65b0e488f02a | 67 | slcd.printf(lMess); |
scohennm | 3:53d47a5dbb2c | 68 | wait(dWait); |
scohennm | 3:53d47a5dbb2c | 69 | } |
scohennm | 3:53d47a5dbb2c | 70 | void LCDMessNoDwell(char *lMess){ |
scohennm | 3:53d47a5dbb2c | 71 | slcd.Home(); |
scohennm | 3:53d47a5dbb2c | 72 | slcd.clear(); |
scohennm | 3:53d47a5dbb2c | 73 | slcd.printf(lMess); |
scohennm | 3:53d47a5dbb2c | 74 | } |
scohennm | 3:53d47a5dbb2c | 75 | |
scohennm | 1:65b0e488f02a | 76 | |
scohennm | 1:65b0e488f02a | 77 | void LEDBlinker(){ // RED LED interrupt |
scohennm | 1:65b0e488f02a | 78 | outState = !outState; |
scohennm | 1:65b0e488f02a | 79 | myled.write(outState); |
scohennm | 1:65b0e488f02a | 80 | } |
scohennm | 1:65b0e488f02a | 81 | |
scohennm | 1:65b0e488f02a | 82 | |
scohennm | 1:65b0e488f02a | 83 | void pressed() // button intterupt |
bcostm | 0:912303e63cbd | 84 | { |
scohennm | 1:65b0e488f02a | 85 | relayState = !relayState; |
scohennm | 1:65b0e488f02a | 86 | relay.write(relayState); |
bcostm | 0:912303e63cbd | 87 | } |
scohennm | 1:65b0e488f02a | 88 | |
bcostm | 0:912303e63cbd | 89 | int main() |
bcostm | 0:912303e63cbd | 90 | { |
scohennm | 3:53d47a5dbb2c | 91 | uint8_t regData = MAX_4G; |
scohennm | 3:53d47a5dbb2c | 92 | uint8_t latchData = 0x40; //0b01000000; //for pulse config register |
scohennm | 3:53d47a5dbb2c | 93 | uint8_t axisData = 0x10; //0b00010000; |
scohennm | 3:53d47a5dbb2c | 94 | char lcdData[LCDLEN]; |
scohennm | 3:53d47a5dbb2c | 95 | |
scohennm | 1:65b0e488f02a | 96 | myled.write(outState); |
scohennm | 1:65b0e488f02a | 97 | relay.write(relayState); |
scohennm | 3:53d47a5dbb2c | 98 | |
scohennm | 3:53d47a5dbb2c | 99 | // set up interrrupts to be used later for taps |
bomalley | 5:0930b17d337d | 100 | mybutton.rise(&pressed); |
bomalley | 5:0930b17d337d | 101 | mybutton.mode(PullNone); |
scohennm | 3:53d47a5dbb2c | 102 | // set up interrrupts to be used later for taps |
scohennm | 1:65b0e488f02a | 103 | ledBlink.attach(&LEDBlinker, LEDDELAY); |
scohennm | 3:53d47a5dbb2c | 104 | |
scohennm | 3:53d47a5dbb2c | 105 | // Check to see if accerlometer is alive and well |
scohennm | 3:53d47a5dbb2c | 106 | acc.setGLimit(MAX_2G); // For now set to 2g |
scohennm | 3:53d47a5dbb2c | 107 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 108 | sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal |
scohennm | 3:53d47a5dbb2c | 109 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 110 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 111 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 112 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 113 | |
scohennm | 1:65b0e488f02a | 114 | while (true) { |
scohennm | 3:53d47a5dbb2c | 115 | |
scohennm | 3:53d47a5dbb2c | 116 | // Read Pulse Source Data and check to see if things have been set |
scohennm | 3:53d47a5dbb2c | 117 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 3:53d47a5dbb2c | 118 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 119 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 120 | |
scohennm | 3:53d47a5dbb2c | 121 | acc.setPulseConfiguration(latchData, axisData); // write the data |
scohennm | 3:53d47a5dbb2c | 122 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 3:53d47a5dbb2c | 123 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 124 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 125 | |
bomalley | 4:d75c7e69796b | 126 | //send address |
bomalley | 5:0930b17d337d | 127 | acc.setRegisterInStandby(REG_PULSE_THSZ, REG_DATA_THSZ); |
bomalley | 5:0930b17d337d | 128 | acc.setRegisterInStandby(REG_PULSE_TMLT, REG_DATA_TMLT); |
bomalley | 5:0930b17d337d | 129 | acc.setRegisterInStandby(REG_PULSE_LTCY, REG_DATA_LTCY); |
bomalley | 5:0930b17d337d | 130 | |
bomalley | 5:0930b17d337d | 131 | acc.setRegisterInStandby(REG_CTRL_REG4, 0x08); |
bomalley | 5:0930b17d337d | 132 | acc.setRegisterInStandby(REG_CTRL_REG5, 0x08); |
bomalley | 5:0930b17d337d | 133 | |
scohennm | 3:53d47a5dbb2c | 134 | acc.readRegs(REG_PULSE_SRC, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 135 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 136 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 137 | |
scohennm | 3:53d47a5dbb2c | 138 | LCDMessNoDwell(LCDMessages[relayState]); |
bcostm | 0:912303e63cbd | 139 | wait(delay); |
bcostm | 0:912303e63cbd | 140 | } |
bcostm | 0:912303e63cbd | 141 | } |