Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@2:e32b4313502d, 2019-01-17 (annotated)
- Committer:
- jschilling22
- Date:
- Thu Jan 17 00:10:52 2019 +0000
- Revision:
- 2:e32b4313502d
- Parent:
- 1:eccb59d79d6a
work; ; ;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jschilling22 | 1:eccb59d79d6a | 1 | #include "mbed.h" |
| jschilling22 | 1:eccb59d79d6a | 2 | |
| jschilling22 | 1:eccb59d79d6a | 3 | I2C tapI2C(p9, p10); //SDA, SCL |
| jschilling22 | 1:eccb59d79d6a | 4 | Serial pc(USBTX, USBRX); |
| jschilling22 | 1:eccb59d79d6a | 5 | void tapsHappened(void); |
| jschilling22 | 1:eccb59d79d6a | 6 | DigitalOut led1(LED1); |
| jschilling22 | 1:eccb59d79d6a | 7 | DigitalOut led2(LED2); |
| jschilling22 | 1:eccb59d79d6a | 8 | DigitalOut led4(LED4); |
| jschilling22 | 1:eccb59d79d6a | 9 | InterruptIn tapInterrupt(p5); |
| jschilling22 | 1:eccb59d79d6a | 10 | Timeout singleTimeout; |
| jschilling22 | 1:eccb59d79d6a | 11 | Timeout doubleTimeout; |
| jschilling22 | 1:eccb59d79d6a | 12 | void singleOff(void); |
| jschilling22 | 1:eccb59d79d6a | 13 | void doubleOff(void); |
| jschilling22 | 1:eccb59d79d6a | 14 | char buff[3]; |
| jschilling22 | 1:eccb59d79d6a | 15 | const int accelAddr = 0x53 << 1; |
| jschilling22 | 1:eccb59d79d6a | 16 | const int tempAddr = 0x90; |
| jschilling22 | 1:eccb59d79d6a | 17 | |
| jschilling22 | 1:eccb59d79d6a | 18 | |
| jschilling22 | 1:eccb59d79d6a | 19 | int main() { |
| jschilling22 | 1:eccb59d79d6a | 20 | buff[0] = 0x1D; |
| jschilling22 | 1:eccb59d79d6a | 21 | buff[1] = 80; |
| jschilling22 | 1:eccb59d79d6a | 22 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 23 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 24 | buff[0] = 0x21; |
| jschilling22 | 1:eccb59d79d6a | 25 | buff[1] = 0x10; |
| jschilling22 | 1:eccb59d79d6a | 26 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 27 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 28 | buff[0] = 0x22; |
| jschilling22 | 1:eccb59d79d6a | 29 | buff[1] = 0x05; |
| jschilling22 | 1:eccb59d79d6a | 30 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 31 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 32 | buff[0] = 0x23; |
| jschilling22 | 1:eccb59d79d6a | 33 | buff[1] = 0xFF; |
| jschilling22 | 1:eccb59d79d6a | 34 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 35 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 36 | buff[0] = 0x2A; |
| jschilling22 | 1:eccb59d79d6a | 37 | buff[1] = 0x07; |
| jschilling22 | 1:eccb59d79d6a | 38 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 39 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 40 | buff[0] = 0x2E; |
| jschilling22 | 1:eccb59d79d6a | 41 | buff[1] = 0x60; |
| jschilling22 | 1:eccb59d79d6a | 42 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 43 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 44 | buff[1] = 0x2F; |
| jschilling22 | 1:eccb59d79d6a | 45 | buff[0] = 0x60; |
| jschilling22 | 1:eccb59d79d6a | 46 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 47 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 48 | buff[0] = 0x2D; |
| jschilling22 | 1:eccb59d79d6a | 49 | buff[1] = 0x08; |
| jschilling22 | 1:eccb59d79d6a | 50 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 51 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 52 | |
| jschilling22 | 1:eccb59d79d6a | 53 | tapInterrupt.rise(&tapsHappened); |
| jschilling22 | 2:e32b4313502d | 54 | tapsHappened(); |
| jschilling22 | 1:eccb59d79d6a | 55 | while(1){ |
| jschilling22 | 2:e32b4313502d | 56 | pc.printf("%d\r\n", tapInterrupt.read()); |
| jschilling22 | 2:e32b4313502d | 57 | tapsHappened(); |
| jschilling22 | 2:e32b4313502d | 58 | wait(0.5); |
| jschilling22 | 1:eccb59d79d6a | 59 | } |
| jschilling22 | 1:eccb59d79d6a | 60 | // Holds bytes for I2C reads/writes |
| jschilling22 | 1:eccb59d79d6a | 61 | short rawTemp; // Holder for temperature bits |
| jschilling22 | 1:eccb59d79d6a | 62 | float temp; // temperature, in deg C |
| jschilling22 | 1:eccb59d79d6a | 63 | // Load buffer with configuration info |
| jschilling22 | 1:eccb59d79d6a | 64 | buff[0] = 0x01; // Address of config register |
| jschilling22 | 1:eccb59d79d6a | 65 | buff[1] = 0x60; // Config Byte 1 |
| jschilling22 | 1:eccb59d79d6a | 66 | buff[2] = 0xA0; // Config Byte 2 |
| jschilling22 | 1:eccb59d79d6a | 67 | tapI2C.write(tempAddr, buff, 3); |
| jschilling22 | 1:eccb59d79d6a | 68 | buff[0] = 0x00; // Address of temperature reading register |
| jschilling22 | 1:eccb59d79d6a | 69 | tapI2C.write(tempAddr, buff, 1); // Only one byte (byte 0) is sent |
| jschilling22 | 1:eccb59d79d6a | 70 | while (true) { |
| jschilling22 | 1:eccb59d79d6a | 71 | wait(1); |
| jschilling22 | 1:eccb59d79d6a | 72 | tapI2C.read(tempAddr, buff, 2); // Read two-byte temperature data |
| jschilling22 | 1:eccb59d79d6a | 73 | // Buff[0] holds bits 4-11 of temperature |
| jschilling22 | 1:eccb59d79d6a | 74 | // Buff[1] holds bits 0-3 of temperature, followed by 0's as placeholders |
| jschilling22 | 1:eccb59d79d6a | 75 | rawTemp = (buff[0] <<8) + buff[1]; // Place the bits in 16-bit holder |
| jschilling22 | 1:eccb59d79d6a | 76 | rawTemp = rawTemp >> 4; // Shift right to drop placeholders, returning it to 12 bit |
| jschilling22 | 1:eccb59d79d6a | 77 | temp = 0.0625 * rawTemp; |
| jschilling22 | 1:eccb59d79d6a | 78 | pc.printf("%.2f deg C\r\n", temp); |
| jschilling22 | 1:eccb59d79d6a | 79 | } |
| jschilling22 | 1:eccb59d79d6a | 80 | } |
| jschilling22 | 1:eccb59d79d6a | 81 | // the ADXL345 is turned on for each transmission (and off after each transmission) to adjust its settings |
| jschilling22 | 1:eccb59d79d6a | 82 | /* for all of the setting adjustments, the first line is the address of the register. |
| jschilling22 | 1:eccb59d79d6a | 83 | The second line adjusts the setting to the desired value */ |
| jschilling22 | 1:eccb59d79d6a | 84 | void tapsHappened(void) { |
| jschilling22 | 1:eccb59d79d6a | 85 | char tapByte; |
| jschilling22 | 1:eccb59d79d6a | 86 | buff[0]=0x30; |
| jschilling22 | 1:eccb59d79d6a | 87 | tapI2C.write(accelAddr,buff,1); // 0x30 is the address, 0x80 means we are reading it |
| jschilling22 | 1:eccb59d79d6a | 88 | tapI2C.read(accelAddr,buff,1); // write zero to get a read response (could send anything). tapByte is set equal to this response |
| jschilling22 | 1:eccb59d79d6a | 89 | tapByte = buff[0]; |
| jschilling22 | 1:eccb59d79d6a | 90 | if (tapByte & 0x40) { |
| jschilling22 | 1:eccb59d79d6a | 91 | /* 0x40 converted to binary is a single tap. If there is a single tap, |
| jschilling22 | 1:eccb59d79d6a | 92 | it turns on LED 1 and activates the singleOff function one second later */ |
| jschilling22 | 1:eccb59d79d6a | 93 | led1 = 1; |
| jschilling22 | 1:eccb59d79d6a | 94 | singleTimeout.attach(&singleOff, 1.0); |
| jschilling22 | 1:eccb59d79d6a | 95 | } |
| jschilling22 | 1:eccb59d79d6a | 96 | if (tapByte & 0x20) { /* 0x20 converted to binary is a double tap. If there is a double tap, |
| jschilling22 | 1:eccb59d79d6a | 97 | it turns on LED 2 and activates the doubleOff function one second later. |
| jschilling22 | 1:eccb59d79d6a | 98 | This will turn on LED 1 and 2 because every double tap starts with a single tap */ |
| jschilling22 | 1:eccb59d79d6a | 99 | led2 = 1; |
| jschilling22 | 1:eccb59d79d6a | 100 | doubleTimeout.attach(&doubleOff, 1.0); |
| jschilling22 | 1:eccb59d79d6a | 101 | } |
| jschilling22 | 1:eccb59d79d6a | 102 | } |
| jschilling22 | 1:eccb59d79d6a | 103 | void singleOff(void) { |
| jschilling22 | 1:eccb59d79d6a | 104 | // first LED off when this function is called by the timeout |
| jschilling22 | 1:eccb59d79d6a | 105 | led1 = 0; |
| jschilling22 | 1:eccb59d79d6a | 106 | } |
| jschilling22 | 1:eccb59d79d6a | 107 | void doubleOff(void) { |
| jschilling22 | 1:eccb59d79d6a | 108 | // first LED off when this function is called by the timeout |
| jschilling22 | 1:eccb59d79d6a | 109 | led2 = 0; |
| jschilling22 | 1:eccb59d79d6a | 110 | } |
