Generic function for configuring the register
Dependencies: MMA8451Q8 SLCD mbed
Fork of KL46z_single_tap_empty by
main.cpp@3:53d47a5dbb2c, 2015-02-03 (annotated)
- Committer:
- scohennm
- Date:
- Tue Feb 03 18:05:52 2015 +0000
- Revision:
- 3:53d47a5dbb2c
- Parent:
- 2:17a0550771c4
- Child:
- 4:1fd16019191d
Use for setting single tap
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 | |
scohennm | 3:53d47a5dbb2c | 21 | #define MAX_2G 0x00 |
scohennm | 3:53d47a5dbb2c | 22 | #define MAX_4G 0x01 |
scohennm | 3:53d47a5dbb2c | 23 | #define MAX_8G 0x02 |
scohennm | 3:53d47a5dbb2c | 24 | |
scohennm | 3:53d47a5dbb2c | 25 | //#define PRINTDBUG |
scohennm | 3:53d47a5dbb2c | 26 | // Accelerometer SPI pins |
scohennm | 3:53d47a5dbb2c | 27 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 3:53d47a5dbb2c | 28 | PinName const SDA = PTE25; |
scohennm | 3:53d47a5dbb2c | 29 | PinName const SCL = PTE24; |
scohennm | 3:53d47a5dbb2c | 30 | #elif defined (TARGET_KL05Z) |
scohennm | 3:53d47a5dbb2c | 31 | PinName const SDA = PTB4; |
scohennm | 3:53d47a5dbb2c | 32 | PinName const SCL = PTB3; |
scohennm | 3:53d47a5dbb2c | 33 | #else |
scohennm | 3:53d47a5dbb2c | 34 | #error TARGET NOT DEFINED |
scohennm | 3:53d47a5dbb2c | 35 | #endif |
scohennm | 3:53d47a5dbb2c | 36 | |
scohennm | 3:53d47a5dbb2c | 37 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
bcostm | 0:912303e63cbd | 38 | |
scohennm | 1:65b0e488f02a | 39 | Ticker ledBlink; // timinginterrupt for RED led |
scohennm | 1:65b0e488f02a | 40 | InterruptIn mybutton(PTC3); //push botton with internal pullup |
scohennm | 1:65b0e488f02a | 41 | DigitalOut myled(LED_RED); // red led |
scohennm | 1:65b0e488f02a | 42 | DigitalOut relay(LED_GREEN); // green led |
scohennm | 1:65b0e488f02a | 43 | |
scohennm | 3:53d47a5dbb2c | 44 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
bcostm | 0:912303e63cbd | 45 | |
scohennm | 1:65b0e488f02a | 46 | float delay = WAITDELAY; |
scohennm | 1:65b0e488f02a | 47 | int relayState = RELAYOFF; |
scohennm | 1:65b0e488f02a | 48 | int outState = false; |
scohennm | 1:65b0e488f02a | 49 | SLCD slcd; //define LCD display |
scohennm | 3:53d47a5dbb2c | 50 | char LCDMessages[2][LCDLEN] = {"TRUE", "FALS"}; |
scohennm | 1:65b0e488f02a | 51 | |
scohennm | 1:65b0e488f02a | 52 | |
scohennm | 3:53d47a5dbb2c | 53 | void LCDMess(char *lMess, float dWait){ |
scohennm | 3:53d47a5dbb2c | 54 | slcd.Home(); |
scohennm | 1:65b0e488f02a | 55 | slcd.clear(); |
scohennm | 1:65b0e488f02a | 56 | slcd.printf(lMess); |
scohennm | 3:53d47a5dbb2c | 57 | wait(dWait); |
scohennm | 3:53d47a5dbb2c | 58 | } |
scohennm | 3:53d47a5dbb2c | 59 | void LCDMessNoDwell(char *lMess){ |
scohennm | 3:53d47a5dbb2c | 60 | slcd.Home(); |
scohennm | 3:53d47a5dbb2c | 61 | slcd.clear(); |
scohennm | 3:53d47a5dbb2c | 62 | slcd.printf(lMess); |
scohennm | 3:53d47a5dbb2c | 63 | } |
scohennm | 3:53d47a5dbb2c | 64 | |
scohennm | 1:65b0e488f02a | 65 | |
scohennm | 1:65b0e488f02a | 66 | void LEDBlinker(){ // RED LED interrupt |
scohennm | 1:65b0e488f02a | 67 | outState = !outState; |
scohennm | 1:65b0e488f02a | 68 | myled.write(outState); |
scohennm | 1:65b0e488f02a | 69 | } |
scohennm | 1:65b0e488f02a | 70 | |
scohennm | 1:65b0e488f02a | 71 | |
scohennm | 1:65b0e488f02a | 72 | void pressed() // button intterupt |
bcostm | 0:912303e63cbd | 73 | { |
scohennm | 1:65b0e488f02a | 74 | relayState = !relayState; |
scohennm | 1:65b0e488f02a | 75 | relay.write(relayState); |
bcostm | 0:912303e63cbd | 76 | } |
scohennm | 1:65b0e488f02a | 77 | |
bcostm | 0:912303e63cbd | 78 | int main() |
bcostm | 0:912303e63cbd | 79 | { |
scohennm | 3:53d47a5dbb2c | 80 | uint8_t regData = MAX_4G; |
scohennm | 3:53d47a5dbb2c | 81 | uint8_t latchData = 0x40; //0b01000000; //for pulse config register |
scohennm | 3:53d47a5dbb2c | 82 | uint8_t axisData = 0x10; //0b00010000; |
scohennm | 3:53d47a5dbb2c | 83 | char lcdData[LCDLEN]; |
scohennm | 3:53d47a5dbb2c | 84 | |
scohennm | 1:65b0e488f02a | 85 | myled.write(outState); |
scohennm | 1:65b0e488f02a | 86 | relay.write(relayState); |
scohennm | 3:53d47a5dbb2c | 87 | |
scohennm | 3:53d47a5dbb2c | 88 | // set up interrrupts to be used later for taps |
bcostm | 0:912303e63cbd | 89 | mybutton.fall(&pressed); |
scohennm | 3:53d47a5dbb2c | 90 | // set up interrrupts to be used later for taps |
scohennm | 1:65b0e488f02a | 91 | ledBlink.attach(&LEDBlinker, LEDDELAY); |
scohennm | 3:53d47a5dbb2c | 92 | |
scohennm | 3:53d47a5dbb2c | 93 | // Check to see if accerlometer is alive and well |
scohennm | 3:53d47a5dbb2c | 94 | acc.setGLimit(MAX_2G); // For now set to 2g |
scohennm | 3:53d47a5dbb2c | 95 | acc.readRegs(XYZ_DATA_CFG, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 96 | sprintf (lcdData,"%x",regData); // Note displaying in hexidecimal |
scohennm | 3:53d47a5dbb2c | 97 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 98 | acc.readRegs(REG_WHO_AM_I, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 99 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 100 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 101 | |
scohennm | 1:65b0e488f02a | 102 | while (true) { |
scohennm | 3:53d47a5dbb2c | 103 | |
scohennm | 3:53d47a5dbb2c | 104 | // Read Pulse Source Data and check to see if things have been set |
scohennm | 3:53d47a5dbb2c | 105 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 3:53d47a5dbb2c | 106 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 107 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 108 | |
scohennm | 3:53d47a5dbb2c | 109 | acc.setPulseConfiguration(latchData, axisData); // write the data |
scohennm | 3:53d47a5dbb2c | 110 | acc.readRegs(REG_PULSE_CFG, ®Data, 1); // check it |
scohennm | 3:53d47a5dbb2c | 111 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 112 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 113 | |
scohennm | 3:53d47a5dbb2c | 114 | acc.readRegs(REG_PULSE_SRC, ®Data, 1); |
scohennm | 3:53d47a5dbb2c | 115 | sprintf (lcdData,"%x",regData); |
scohennm | 3:53d47a5dbb2c | 116 | LCDMess(lcdData,BLINKTIME); |
scohennm | 3:53d47a5dbb2c | 117 | |
scohennm | 3:53d47a5dbb2c | 118 | LCDMessNoDwell(LCDMessages[relayState]); |
bcostm | 0:912303e63cbd | 119 | wait(delay); |
bcostm | 0:912303e63cbd | 120 | } |
bcostm | 0:912303e63cbd | 121 | } |