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@1:eccb59d79d6a, 2019-01-16 (annotated)
- Committer:
- jschilling22
- Date:
- Wed Jan 16 23:46:13 2019 +0000
- Revision:
- 1:eccb59d79d6a
- Parent:
- 0:0e0c93f4cd44
- Child:
- 2:e32b4313502d
main function with temp code and tap code (tap code interrupter not working)
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 | tapI2C.frequency(2000000); // 2 MHz clock |
| jschilling22 | 1:eccb59d79d6a | 21 | buff[0] = 0x1D; |
| jschilling22 | 1:eccb59d79d6a | 22 | buff[1] = 80; |
| jschilling22 | 1:eccb59d79d6a | 23 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 24 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 25 | buff[0] = 0x21; |
| jschilling22 | 1:eccb59d79d6a | 26 | buff[1] = 0x10; |
| jschilling22 | 1:eccb59d79d6a | 27 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 28 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 29 | buff[0] = 0x22; |
| jschilling22 | 1:eccb59d79d6a | 30 | buff[1] = 0x05; |
| jschilling22 | 1:eccb59d79d6a | 31 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 32 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 33 | buff[0] = 0x23; |
| jschilling22 | 1:eccb59d79d6a | 34 | buff[1] = 0xFF; |
| jschilling22 | 1:eccb59d79d6a | 35 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 36 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 37 | buff[0] = 0x2A; |
| jschilling22 | 1:eccb59d79d6a | 38 | buff[1] = 0x07; |
| jschilling22 | 1:eccb59d79d6a | 39 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 40 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 41 | buff[0] = 0x2E; |
| jschilling22 | 1:eccb59d79d6a | 42 | buff[1] = 0x60; |
| jschilling22 | 1:eccb59d79d6a | 43 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 44 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 45 | buff[1] = 0x2F; |
| jschilling22 | 1:eccb59d79d6a | 46 | buff[0] = 0x60; |
| jschilling22 | 1:eccb59d79d6a | 47 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 48 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 49 | buff[0] = 0x2D; |
| jschilling22 | 1:eccb59d79d6a | 50 | buff[1] = 0x08; |
| jschilling22 | 1:eccb59d79d6a | 51 | tapI2C.write(accelAddr, buff, 2); |
| jschilling22 | 1:eccb59d79d6a | 52 | wait(0.02); |
| jschilling22 | 1:eccb59d79d6a | 53 | |
| jschilling22 | 1:eccb59d79d6a | 54 | tapInterrupt.rise(&tapsHappened); |
| jschilling22 | 1:eccb59d79d6a | 55 | while(1){ |
| jschilling22 | 1:eccb59d79d6a | 56 | tapsHappened(); |
| jschilling22 | 1:eccb59d79d6a | 57 | } |
| jschilling22 | 1:eccb59d79d6a | 58 | // Holds bytes for I2C reads/writes |
| jschilling22 | 1:eccb59d79d6a | 59 | short rawTemp; // Holder for temperature bits |
| jschilling22 | 1:eccb59d79d6a | 60 | float temp; // temperature, in deg C |
| jschilling22 | 1:eccb59d79d6a | 61 | // Load buffer with configuration info |
| jschilling22 | 1:eccb59d79d6a | 62 | buff[0] = 0x01; // Address of config register |
| jschilling22 | 1:eccb59d79d6a | 63 | buff[1] = 0x60; // Config Byte 1 |
| jschilling22 | 1:eccb59d79d6a | 64 | buff[2] = 0xA0; // Config Byte 2 |
| jschilling22 | 1:eccb59d79d6a | 65 | tapI2C.write(tempAddr, buff, 3); |
| jschilling22 | 1:eccb59d79d6a | 66 | buff[0] = 0x00; // Address of temperature reading register |
| jschilling22 | 1:eccb59d79d6a | 67 | tapI2C.write(tempAddr, buff, 1); // Only one byte (byte 0) is sent |
| jschilling22 | 1:eccb59d79d6a | 68 | while (true) { |
| jschilling22 | 1:eccb59d79d6a | 69 | wait(1); |
| jschilling22 | 1:eccb59d79d6a | 70 | tapI2C.read(tempAddr, buff, 2); // Read two-byte temperature data |
| jschilling22 | 1:eccb59d79d6a | 71 | // Buff[0] holds bits 4-11 of temperature |
| jschilling22 | 1:eccb59d79d6a | 72 | // Buff[1] holds bits 0-3 of temperature, followed by 0's as placeholders |
| jschilling22 | 1:eccb59d79d6a | 73 | rawTemp = (buff[0] <<8) + buff[1]; // Place the bits in 16-bit holder |
| jschilling22 | 1:eccb59d79d6a | 74 | rawTemp = rawTemp >> 4; // Shift right to drop placeholders, returning it to 12 bit |
| jschilling22 | 1:eccb59d79d6a | 75 | temp = 0.0625 * rawTemp; |
| jschilling22 | 1:eccb59d79d6a | 76 | pc.printf("%.2f deg C\r\n", temp); |
| jschilling22 | 1:eccb59d79d6a | 77 | } |
| jschilling22 | 1:eccb59d79d6a | 78 | } |
| jschilling22 | 1:eccb59d79d6a | 79 | // the ADXL345 is turned on for each transmission (and off after each transmission) to adjust its settings |
| jschilling22 | 1:eccb59d79d6a | 80 | /* for all of the setting adjustments, the first line is the address of the register. |
| jschilling22 | 1:eccb59d79d6a | 81 | The second line adjusts the setting to the desired value */ |
| jschilling22 | 1:eccb59d79d6a | 82 | void tapsHappened(void) { |
| jschilling22 | 1:eccb59d79d6a | 83 | char tapByte; |
| jschilling22 | 1:eccb59d79d6a | 84 | buff[0]=0x30; |
| jschilling22 | 1:eccb59d79d6a | 85 | tapI2C.write(accelAddr,buff,1); // 0x30 is the address, 0x80 means we are reading it |
| jschilling22 | 1:eccb59d79d6a | 86 | 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 | 87 | tapByte = buff[0]; |
| jschilling22 | 1:eccb59d79d6a | 88 | if (tapByte & 0x40) { |
| jschilling22 | 1:eccb59d79d6a | 89 | /* 0x40 converted to binary is a single tap. If there is a single tap, |
| jschilling22 | 1:eccb59d79d6a | 90 | it turns on LED 1 and activates the singleOff function one second later */ |
| jschilling22 | 1:eccb59d79d6a | 91 | led1 = 1; |
| jschilling22 | 1:eccb59d79d6a | 92 | singleTimeout.attach(&singleOff, 1.0); |
| jschilling22 | 1:eccb59d79d6a | 93 | } |
| jschilling22 | 1:eccb59d79d6a | 94 | if (tapByte & 0x20) { /* 0x20 converted to binary is a double tap. If there is a double tap, |
| jschilling22 | 1:eccb59d79d6a | 95 | it turns on LED 2 and activates the doubleOff function one second later. |
| jschilling22 | 1:eccb59d79d6a | 96 | This will turn on LED 1 and 2 because every double tap starts with a single tap */ |
| jschilling22 | 1:eccb59d79d6a | 97 | led2 = 1; |
| jschilling22 | 1:eccb59d79d6a | 98 | doubleTimeout.attach(&doubleOff, 1.0); |
| jschilling22 | 1:eccb59d79d6a | 99 | } |
| jschilling22 | 1:eccb59d79d6a | 100 | } |
| jschilling22 | 1:eccb59d79d6a | 101 | void singleOff(void) { |
| jschilling22 | 1:eccb59d79d6a | 102 | // first LED off when this function is called by the timeout |
| jschilling22 | 1:eccb59d79d6a | 103 | led1 = 0; |
| jschilling22 | 1:eccb59d79d6a | 104 | } |
| jschilling22 | 1:eccb59d79d6a | 105 | void doubleOff(void) { |
| jschilling22 | 1:eccb59d79d6a | 106 | // first LED off when this function is called by the timeout |
| jschilling22 | 1:eccb59d79d6a | 107 | led2 = 0; |
| jschilling22 | 1:eccb59d79d6a | 108 | } |
