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.
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
sensors.cpp@9:b0a1535b8ef2, 2018-12-15 (annotated)
- Committer:
- pathfindr
- Date:
- Sat Dec 15 13:45:28 2018 +0000
- Revision:
- 9:b0a1535b8ef2
- Parent:
- 7:e9a19750700d
- Child:
- 13:29f67f256709
sat
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pathfindr | 7:e9a19750700d | 1 | #include "sensors.h" |
| pathfindr | 7:e9a19750700d | 2 | |
| pathfindr | 7:e9a19750700d | 3 | //------------------------------------------------------------------------------ |
| pathfindr | 7:e9a19750700d | 4 | // ACCELEROMETER |
| pathfindr | 7:e9a19750700d | 5 | //------------------------------------------------------------------------------ |
| pathfindr | 7:e9a19750700d | 6 | |
| pathfindr | 7:e9a19750700d | 7 | DigitalIn lis3dh_int1(PN_ACC_INT1, PullNone); //DISTUBANCE |
| pathfindr | 7:e9a19750700d | 8 | DigitalIn lis3dh_int2(PN_ACC_INT2, PullNone); //IMPACT |
| pathfindr | 7:e9a19750700d | 9 | |
| pathfindr | 9:b0a1535b8ef2 | 10 | void lis3dh_configureForSleep(uint8_t motion_threshold, uint8_t impact_threshold) { |
| pathfindr | 7:e9a19750700d | 11 | //init |
| pathfindr | 7:e9a19750700d | 12 | LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK); |
| pathfindr | 7:e9a19750700d | 13 | requireSoftReset = true; //WE HAVE STARTED SPI SO NEED THIS |
| pathfindr | 9:b0a1535b8ef2 | 14 | |
| pathfindr | 7:e9a19750700d | 15 | //Mode |
| pathfindr | 9:b0a1535b8ef2 | 16 | lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG1, 0b00111111); // 00111111 25hz low power mode all axis. 50hz = 6ua / 25hz = 4ua / 10hz = 3ua |
| pathfindr | 9:b0a1535b8ef2 | 17 | lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG2, 0b11001011); // HP Filter with 0.2hz@25hz for both INT1 and INT2 and Data //http://www.st.com/content/ccc/resource/technical/document/application_note/77/ed/e7/e1/28/5a/45/d6/CD00290365.pdf/files/CD00290365.pdf/jcr:content/translations/en.CD00290365.pdf |
| pathfindr | 9:b0a1535b8ef2 | 18 | |
| pathfindr | 9:b0a1535b8ef2 | 19 | //g and Resolution |
| pathfindr | 9:b0a1535b8ef2 | 20 | //lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG4, 0b00110000); // disable highresoluton , set 16g |
| pathfindr | 9:b0a1535b8ef2 | 21 | lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG4, 0b00100000); // disable highresoluton , set 8g |
| pathfindr | 9:b0a1535b8ef2 | 22 | //lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG4, 0b00010000); // disable highresoluton , set 4g |
| pathfindr | 9:b0a1535b8ef2 | 23 | //lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG4, 0b00000000); // disable highresoluton , set 2g |
| pathfindr | 9:b0a1535b8ef2 | 24 | |
| pathfindr | 9:b0a1535b8ef2 | 25 | //interrupt setup |
| pathfindr | 9:b0a1535b8ef2 | 26 | lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG5, 0b00001010); // Latch INT1 and INT2 |
| pathfindr | 9:b0a1535b8ef2 | 27 | |
| pathfindr | 9:b0a1535b8ef2 | 28 | //impact interrupt |
| pathfindr | 9:b0a1535b8ef2 | 29 | if (impact_threshold > 10) { //10 is a reasonable minimum value |
| pathfindr | 9:b0a1535b8ef2 | 30 | lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG3, 0b01000000); // IA1 on INT1 |
| pathfindr | 9:b0a1535b8ef2 | 31 | lis3dh.LIS3DH_WriteReg(LIS3DH_INT1_CFG, 0b00101010); // Enable X,Y,Z interrupt generation |
| pathfindr | 9:b0a1535b8ef2 | 32 | lis3dh.LIS3DH_WriteReg(LIS3DH_INT1_THS, impact_threshold); |
| pathfindr | 9:b0a1535b8ef2 | 33 | lis3dh.LIS3DH_WriteReg(LIS3DH_INT1_DURATION, 0x00); // Set duration to 0 cycle |
| pathfindr | 9:b0a1535b8ef2 | 34 | } else { |
| pathfindr | 9:b0a1535b8ef2 | 35 | lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG3, 0b00000000); // No IA1 on INT1 |
| pathfindr | 9:b0a1535b8ef2 | 36 | lis3dh.LIS3DH_WriteReg(LIS3DH_INT1_CFG, 0b00000000); // Enable X,Y,Z interrupt generation |
| pathfindr | 9:b0a1535b8ef2 | 37 | } |
| pathfindr | 9:b0a1535b8ef2 | 38 | |
| pathfindr | 9:b0a1535b8ef2 | 39 | //awake interrupt |
| pathfindr | 9:b0a1535b8ef2 | 40 | lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG6, 0b00100000); // IA2 on INT2 |
| pathfindr | 9:b0a1535b8ef2 | 41 | lis3dh.LIS3DH_WriteReg(LIS3DH_INT2_CFG, 0b00101010); // Enable X,Y,Z interrupt generation |
| pathfindr | 9:b0a1535b8ef2 | 42 | lis3dh.LIS3DH_WriteReg(LIS3DH_INT2_THS, motion_threshold); // Set threshold |
| pathfindr | 9:b0a1535b8ef2 | 43 | lis3dh.LIS3DH_WriteReg(LIS3DH_INT2_DURATION, 0x00); // Set duration to 0 cycle |
| pathfindr | 9:b0a1535b8ef2 | 44 | |
| pathfindr | 9:b0a1535b8ef2 | 45 | //Clear interrupts |
| pathfindr | 7:e9a19750700d | 46 | lis3dh.LIS3DH_ResetInt1Latch(); |
| pathfindr | 9:b0a1535b8ef2 | 47 | lis3dh.LIS3DH_ResetInt2Latch(); |
| pathfindr | 7:e9a19750700d | 48 | } |