cvbxcvxcv
Lis2dh12.cpp@6:b7e3a0c1210b, 2018-03-09 (annotated)
- Committer:
- dbartolovic
- Date:
- Fri Mar 09 17:53:52 2018 +0000
- Revision:
- 6:b7e3a0c1210b
- Parent:
- 5:15d1c73137e8
- Child:
- 7:f1ba533021b8
Bug fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jurica238814 | 0:cc5d477d5dbe | 1 | /* |
jurica238814 | 0:cc5d477d5dbe | 2 | * |
jurica238814 | 0:cc5d477d5dbe | 3 | * LIS2DH12 MEMS digital output motion sensor |
jurica238814 | 0:cc5d477d5dbe | 4 | * More info @ aconno.de |
jurica238814 | 0:cc5d477d5dbe | 5 | * Made by Jurica Resetar |
jurica238814 | 0:cc5d477d5dbe | 6 | * jurica_resetar@yahoo.com |
jurica238814 | 0:cc5d477d5dbe | 7 | * |
jurica238814 | 0:cc5d477d5dbe | 8 | */ |
jurica238814 | 0:cc5d477d5dbe | 9 | |
jurica238814 | 0:cc5d477d5dbe | 10 | #include "Lis2dh12.h" |
jurica238814 | 0:cc5d477d5dbe | 11 | #include "Lis2dh12_regs.h" |
jurica238814 | 0:cc5d477d5dbe | 12 | |
jurica238814 | 0:cc5d477d5dbe | 13 | Lis2dh12::Lis2dh12(I2C *i2c_, char address) : i2c(i2c_, address){ |
jurica238814 | 0:cc5d477d5dbe | 14 | } |
jurica238814 | 0:cc5d477d5dbe | 15 | |
jurica238814 | 0:cc5d477d5dbe | 16 | uint8_t Lis2dh12::whoIAm(){ |
jurica238814 | 0:cc5d477d5dbe | 17 | char regAddr = (char)WHO_AM_I; |
jurica238814 | 0:cc5d477d5dbe | 18 | char regData; |
jurica238814 | 0:cc5d477d5dbe | 19 | i2c.readFromReg(regAddr, ®Data, 1); |
jurica238814 | 0:cc5d477d5dbe | 20 | return (uint8_t)regData; |
jurica238814 | 0:cc5d477d5dbe | 21 | } |
jurica238814 | 0:cc5d477d5dbe | 22 | |
jurica238814 | 0:cc5d477d5dbe | 23 | uint8_t Lis2dh12::setMode(Mode mode){ |
jurica238814 | 0:cc5d477d5dbe | 24 | char ctrl1Copy; |
jurica238814 | 0:cc5d477d5dbe | 25 | char ctrl4Copy; |
jurica238814 | 0:cc5d477d5dbe | 26 | uint8_t success; |
jurica238814 | 0:cc5d477d5dbe | 27 | |
jurica238814 | 0:cc5d477d5dbe | 28 | i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 29 | i2c.readFromReg((char)CTRL_REG4, &ctrl4Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 30 | |
jurica238814 | 0:cc5d477d5dbe | 31 | switch(mode){ |
jurica238814 | 0:cc5d477d5dbe | 32 | case HIGH_RES: |
jurica238814 | 0:cc5d477d5dbe | 33 | ctrl1Copy &= 0xF7; |
jurica238814 | 0:cc5d477d5dbe | 34 | ctrl4Copy |= 0x08; |
jurica238814 | 0:cc5d477d5dbe | 35 | break; |
jurica238814 | 0:cc5d477d5dbe | 36 | case NORMAL: |
jurica238814 | 0:cc5d477d5dbe | 37 | ctrl1Copy &= 0xF7; |
jurica238814 | 0:cc5d477d5dbe | 38 | ctrl4Copy &= 0xF7; |
jurica238814 | 0:cc5d477d5dbe | 39 | break; |
jurica238814 | 0:cc5d477d5dbe | 40 | case LOW_POWER: |
jurica238814 | 0:cc5d477d5dbe | 41 | ctrl1Copy |= 0x08; |
jurica238814 | 0:cc5d477d5dbe | 42 | ctrl4Copy &= 0xF7; |
jurica238814 | 0:cc5d477d5dbe | 43 | break; |
jurica238814 | 0:cc5d477d5dbe | 44 | default: |
jurica238814 | 0:cc5d477d5dbe | 45 | return 0; |
jurica238814 | 0:cc5d477d5dbe | 46 | } |
jurica238814 | 0:cc5d477d5dbe | 47 | i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 48 | success = i2c.writeToReg((char)CTRL_REG4, &ctrl4Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 49 | return success; |
jurica238814 | 0:cc5d477d5dbe | 50 | } |
jurica238814 | 0:cc5d477d5dbe | 51 | |
jurica238814 | 0:cc5d477d5dbe | 52 | uint8_t Lis2dh12::enableAxes(Axis axis){ |
jurica238814 | 0:cc5d477d5dbe | 53 | char ctrl1Copy; |
jurica238814 | 0:cc5d477d5dbe | 54 | i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 55 | ctrl1Copy |= axis; |
jurica238814 | 0:cc5d477d5dbe | 56 | i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 57 | return 0; |
jurica238814 | 0:cc5d477d5dbe | 58 | } |
jurica238814 | 0:cc5d477d5dbe | 59 | |
jurica238814 | 0:cc5d477d5dbe | 60 | uint8_t Lis2dh12::disableAxes(Axis axis){ |
jurica238814 | 0:cc5d477d5dbe | 61 | char ctrl1Copy; |
jurica238814 | 0:cc5d477d5dbe | 62 | i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 63 | ctrl1Copy &= ~(1 << axis); |
jurica238814 | 0:cc5d477d5dbe | 64 | i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 65 | return 0; |
jurica238814 | 0:cc5d477d5dbe | 66 | } |
jurica238814 | 0:cc5d477d5dbe | 67 | |
jurica238814 | 0:cc5d477d5dbe | 68 | int16_t Lis2dh12::readXAxis(){ |
jurica238814 | 0:cc5d477d5dbe | 69 | int16_t rawData; |
jurica238814 | 0:cc5d477d5dbe | 70 | char tempData; |
jurica238814 | 1:d89f4b12116b | 71 | // Make sure new data is ready |
jurica238814 | 1:d89f4b12116b | 72 | do{ |
jurica238814 | 1:d89f4b12116b | 73 | i2c.readFromReg((char)STATUS, &tempData, 1); |
jurica238814 | 1:d89f4b12116b | 74 | }while(!(tempData & 0x08)); |
jurica238814 | 1:d89f4b12116b | 75 | do{ |
jurica238814 | 1:d89f4b12116b | 76 | i2c.readFromReg((char)STATUS, &tempData, 1); |
jurica238814 | 1:d89f4b12116b | 77 | }while(!(tempData & 0x80)); |
jurica238814 | 1:d89f4b12116b | 78 | // Same data have been overwritten |
jurica238814 | 1:d89f4b12116b | 79 | |
jurica238814 | 4:043396f22548 | 80 | //i2c.readFromReg((char)OUT_X_H, &tempData, 1); |
jurica238814 | 4:043396f22548 | 81 | //rawData = (int8_t)tempData << 8; |
dbartolovic | 6:b7e3a0c1210b | 82 | i2c.readFromReg((char)OUT_X_L, (char*)&rawData, 1); |
dbartolovic | 6:b7e3a0c1210b | 83 | i2c.readFromReg((char)OUT_X_H, ((char*)&rawData)+1, 1); |
dbartolovic | 5:15d1c73137e8 | 84 | |
dbartolovic | 5:15d1c73137e8 | 85 | if (rawData >= 0) |
dbartolovic | 5:15d1c73137e8 | 86 | rawData = (rawData>>4); |
dbartolovic | 5:15d1c73137e8 | 87 | else |
dbartolovic | 5:15d1c73137e8 | 88 | rawData = (rawData>>4) | 0xF000; |
dbartolovic | 5:15d1c73137e8 | 89 | |
jurica238814 | 0:cc5d477d5dbe | 90 | return rawData; |
jurica238814 | 0:cc5d477d5dbe | 91 | } |
jurica238814 | 0:cc5d477d5dbe | 92 | |
jurica238814 | 0:cc5d477d5dbe | 93 | int16_t Lis2dh12::readYAxis(){ |
jurica238814 | 0:cc5d477d5dbe | 94 | int16_t rawData; |
jurica238814 | 0:cc5d477d5dbe | 95 | char tempData; |
jurica238814 | 4:043396f22548 | 96 | //i2c.readFromReg((char)OUT_Y_H, &tempData, 1); |
jurica238814 | 4:043396f22548 | 97 | //rawData = (int8_t)tempData << 8; |
dbartolovic | 6:b7e3a0c1210b | 98 | i2c.readFromReg((char)OUT_Y_L, (char*)&rawData, 1); |
dbartolovic | 6:b7e3a0c1210b | 99 | i2c.readFromReg((char)OUT_Y_H, ((char*)&rawData)+1, 1); |
dbartolovic | 5:15d1c73137e8 | 100 | |
dbartolovic | 5:15d1c73137e8 | 101 | if (rawData >= 0) |
dbartolovic | 5:15d1c73137e8 | 102 | rawData = (rawData>>4); |
dbartolovic | 5:15d1c73137e8 | 103 | else |
dbartolovic | 5:15d1c73137e8 | 104 | rawData = (rawData>>4) | 0xF000; |
dbartolovic | 5:15d1c73137e8 | 105 | |
jurica238814 | 0:cc5d477d5dbe | 106 | return rawData; |
jurica238814 | 0:cc5d477d5dbe | 107 | } |
jurica238814 | 0:cc5d477d5dbe | 108 | |
jurica238814 | 0:cc5d477d5dbe | 109 | int16_t Lis2dh12::readZAxis(){ |
jurica238814 | 4:043396f22548 | 110 | int16_t rawData = 0; |
jurica238814 | 0:cc5d477d5dbe | 111 | char tempData; |
jurica238814 | 4:043396f22548 | 112 | //i2c.readFromReg((char)OUT_Z_H, &tempData, 1); |
jurica238814 | 4:043396f22548 | 113 | //rawData = (int8_t)tempData << 8; |
dbartolovic | 6:b7e3a0c1210b | 114 | i2c.readFromReg((char)OUT_Z_L, (char*)&rawData, 1); |
dbartolovic | 6:b7e3a0c1210b | 115 | i2c.readFromReg((char)OUT_Z_H, ((char*)&rawData)+1, 1); |
dbartolovic | 5:15d1c73137e8 | 116 | |
dbartolovic | 5:15d1c73137e8 | 117 | if (rawData >= 0) |
dbartolovic | 5:15d1c73137e8 | 118 | rawData = (rawData>>4); |
dbartolovic | 5:15d1c73137e8 | 119 | else |
dbartolovic | 5:15d1c73137e8 | 120 | rawData = (rawData>>4) | 0xF000; |
dbartolovic | 5:15d1c73137e8 | 121 | |
jurica238814 | 0:cc5d477d5dbe | 122 | return rawData; |
jurica238814 | 0:cc5d477d5dbe | 123 | } |
jurica238814 | 0:cc5d477d5dbe | 124 | |
jurica238814 | 0:cc5d477d5dbe | 125 | uint8_t Lis2dh12::setODR(Odr odr){ |
jurica238814 | 0:cc5d477d5dbe | 126 | char ctrl1Copy; |
jurica238814 | 0:cc5d477d5dbe | 127 | i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 128 | ctrl1Copy |= (odr << 4); |
jurica238814 | 0:cc5d477d5dbe | 129 | i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 130 | return 0; |
jurica238814 | 0:cc5d477d5dbe | 131 | } |
jurica238814 | 0:cc5d477d5dbe | 132 | |
jurica238814 | 0:cc5d477d5dbe | 133 | uint8_t Lis2dh12::setScale(Scale scale){ |
jurica238814 | 0:cc5d477d5dbe | 134 | char ctrl4Copy; |
jurica238814 | 0:cc5d477d5dbe | 135 | i2c.readFromReg((char)CTRL_REG4, &ctrl4Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 136 | ctrl4Copy |= (scale << 4); |
jurica238814 | 0:cc5d477d5dbe | 137 | i2c.writeToReg((char)CTRL_REG4, &ctrl4Copy, 1); |
jurica238814 | 0:cc5d477d5dbe | 138 | return 0; |
jurica238814 | 0:cc5d477d5dbe | 139 | } |
jurica238814 | 0:cc5d477d5dbe | 140 | |
jurica238814 | 2:1fa08c7a2a3c | 141 | /* Interrupt activity 1 driven to INT1 pad */ |
jurica238814 | 2:1fa08c7a2a3c | 142 | // TODO: Napraviti tako da postoji samo jedna metoda int1Setup koja prima gro |
jurica238814 | 2:1fa08c7a2a3c | 143 | // parametara: THS, DUR, Latch... |
jurica238814 | 2:1fa08c7a2a3c | 144 | uint8_t Lis2dh12::int1Setup(uint8_t setup){ |
jurica238814 | 2:1fa08c7a2a3c | 145 | char data = setup; |
jurica238814 | 2:1fa08c7a2a3c | 146 | i2c.writeToReg((char)CTRL_REG3, &data, 1); |
jurica238814 | 2:1fa08c7a2a3c | 147 | return 0; |
jurica238814 | 2:1fa08c7a2a3c | 148 | } |
jurica238814 | 2:1fa08c7a2a3c | 149 | |
jurica238814 | 2:1fa08c7a2a3c | 150 | uint8_t Lis2dh12::int1Latch(uint8_t enable){ |
jurica238814 | 2:1fa08c7a2a3c | 151 | char ctrl5Copy; |
jurica238814 | 2:1fa08c7a2a3c | 152 | i2c.readFromReg((char)CTRL_REG5, &ctrl5Copy, 1); |
jurica238814 | 2:1fa08c7a2a3c | 153 | ctrl5Copy |= enable; |
jurica238814 | 2:1fa08c7a2a3c | 154 | i2c.writeToReg((char)CTRL_REG5, &ctrl5Copy, 1); |
jurica238814 | 3:07ddb07f8b7f | 155 | return 0; |
jurica238814 | 2:1fa08c7a2a3c | 156 | } |
jurica238814 | 2:1fa08c7a2a3c | 157 | |
jurica238814 | 2:1fa08c7a2a3c | 158 | uint8_t Lis2dh12::int1Duration(uint8_t duration){ |
jurica238814 | 2:1fa08c7a2a3c | 159 | char copy = duration; |
jurica238814 | 2:1fa08c7a2a3c | 160 | i2c.writeToReg((char)INT1_DURATION, ©, 1); |
jurica238814 | 2:1fa08c7a2a3c | 161 | return 0; |
jurica238814 | 2:1fa08c7a2a3c | 162 | } |
jurica238814 | 2:1fa08c7a2a3c | 163 | |
jurica238814 | 2:1fa08c7a2a3c | 164 | uint8_t Lis2dh12::int1Threshold(uint8_t threshold){ |
jurica238814 | 2:1fa08c7a2a3c | 165 | char copy = threshold; |
jurica238814 | 2:1fa08c7a2a3c | 166 | i2c.writeToReg((char)INT1_THS, ©, 1); |
jurica238814 | 2:1fa08c7a2a3c | 167 | return 0; |
jurica238814 | 2:1fa08c7a2a3c | 168 | } |
jurica238814 | 2:1fa08c7a2a3c | 169 | |
jurica238814 | 2:1fa08c7a2a3c | 170 | uint8_t Lis2dh12::int1Config(uint8_t config){ |
jurica238814 | 2:1fa08c7a2a3c | 171 | char copy = config; |
jurica238814 | 2:1fa08c7a2a3c | 172 | i2c.writeToReg((char)INT1_CFG, ©, 1); |
jurica238814 | 2:1fa08c7a2a3c | 173 | return 0; |
jurica238814 | 2:1fa08c7a2a3c | 174 | } |
jurica238814 | 2:1fa08c7a2a3c | 175 | |
jurica238814 | 2:1fa08c7a2a3c | 176 | void Lis2dh12::clearIntFlag(){ |
jurica238814 | 2:1fa08c7a2a3c | 177 | char data; |
jurica238814 | 2:1fa08c7a2a3c | 178 | i2c.readFromReg((char)INT1_SRC, (char*)&data, 1); |
jurica238814 | 2:1fa08c7a2a3c | 179 | } |
jurica238814 | 0:cc5d477d5dbe | 180 | |
jurica238814 | 0:cc5d477d5dbe | 181 |