aconno acnsensa project for iOS devices with iBeacon packets support.
Dependencies: LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common
MPL115A1/MPL115A1.cpp@6:51745805d8b0, 2018-03-20 (annotated)
- Committer:
- Dautor
- Date:
- Tue Mar 20 08:40:21 2018 +0000
- Revision:
- 6:51745805d8b0
- Parent:
- 5:4807f549aada
Fixed data read
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dautor | 1:326ce5e200fb | 1 | #include "MPL115A1.h" |
Dautor | 1:326ce5e200fb | 2 | |
Dautor | 4:634796e5b8c3 | 3 | // These values correspond to the values described here: |
Dautor | 4:634796e5b8c3 | 4 | // https://www.nxp.com/docs/en/data-sheet/MPL115A1.pdf |
Dautor | 4:634796e5b8c3 | 5 | |
Dautor | 3:78ceda8ef565 | 6 | #define a0FracBits 3 |
Dautor | 3:78ceda8ef565 | 7 | #define b1FracBits 13 |
Dautor | 3:78ceda8ef565 | 8 | #define b2FracBits 14 |
Dautor | 3:78ceda8ef565 | 9 | #define c12FracBits 22 |
Dautor | 3:78ceda8ef565 | 10 | #define a0FracMask 0x0007 |
Dautor | 3:78ceda8ef565 | 11 | #define b1FracMask 0x1FFF |
Dautor | 3:78ceda8ef565 | 12 | #define b2FracMask 0x3FFF |
Dautor | 3:78ceda8ef565 | 13 | #define c12FracMask 0x1FFF |
Dautor | 3:78ceda8ef565 | 14 | |
Dautor | 5:4807f549aada | 15 | static float convertA0(uint16_t a0); |
Dautor | 5:4807f549aada | 16 | static float convertB1(uint16_t b1); |
Dautor | 5:4807f549aada | 17 | static float convertB2(uint16_t b2); |
Dautor | 5:4807f549aada | 18 | static float convertC12(uint16_t c12); |
Dautor | 1:326ce5e200fb | 19 | |
Dautor | 1:326ce5e200fb | 20 | MPL115A1::MPL115A1(SPI &spi, NRF52_DigitalOut &cs) : spi(spi), cs(cs) {} |
Dautor | 1:326ce5e200fb | 21 | |
Dautor | 5:4807f549aada | 22 | float MPL115A1::getA0() |
Dautor | 5:4807f549aada | 23 | { |
Dautor | 5:4807f549aada | 24 | spi.write(0x88); // MSB a0 |
Dautor | 5:4807f549aada | 25 | uint16_t a0 = spi.write(0x00); |
Dautor | 5:4807f549aada | 26 | a0 = a0 << 8; |
Dautor | 5:4807f549aada | 27 | wait_ms(1); |
Dautor | 5:4807f549aada | 28 | spi.write(0x8A); // LSB a0 |
Dautor | 5:4807f549aada | 29 | a0 |= spi.write(0x00); |
Dautor | 5:4807f549aada | 30 | float result = convertA0(a0); |
Dautor | 5:4807f549aada | 31 | wait_ms(1); |
Dautor | 5:4807f549aada | 32 | return result; |
Dautor | 5:4807f549aada | 33 | } |
Dautor | 5:4807f549aada | 34 | |
Dautor | 5:4807f549aada | 35 | float MPL115A1::getB1() |
Dautor | 5:4807f549aada | 36 | { |
Dautor | 5:4807f549aada | 37 | spi.write(0x8C); // MSB b1 |
Dautor | 5:4807f549aada | 38 | uint16_t b1 = spi.write(0x00); |
Dautor | 5:4807f549aada | 39 | b1 = b1 << 8; |
Dautor | 5:4807f549aada | 40 | wait_ms(1); |
Dautor | 5:4807f549aada | 41 | spi.write(0x8E); // LSB b1 |
Dautor | 5:4807f549aada | 42 | b1 |= spi.write(0x00); |
Dautor | 5:4807f549aada | 43 | float result = convertB1(b1); |
Dautor | 5:4807f549aada | 44 | wait_ms(1); |
Dautor | 5:4807f549aada | 45 | return result; |
Dautor | 5:4807f549aada | 46 | } |
Dautor | 5:4807f549aada | 47 | |
Dautor | 5:4807f549aada | 48 | float MPL115A1::getB2() |
Dautor | 5:4807f549aada | 49 | { |
Dautor | 6:51745805d8b0 | 50 | spi.write(0x90); // MSB b2 |
Dautor | 5:4807f549aada | 51 | uint16_t b2 = spi.write(0x00); |
Dautor | 5:4807f549aada | 52 | b2 = b2 << 8; |
Dautor | 5:4807f549aada | 53 | wait_ms(1); |
Dautor | 6:51745805d8b0 | 54 | spi.write(0x92); // LSB b2 |
Dautor | 5:4807f549aada | 55 | b2 |= spi.write(0x00); |
Dautor | 5:4807f549aada | 56 | float result = convertB2(b2); |
Dautor | 5:4807f549aada | 57 | wait_ms(1); |
Dautor | 5:4807f549aada | 58 | return result; |
Dautor | 5:4807f549aada | 59 | } |
Dautor | 5:4807f549aada | 60 | |
Dautor | 5:4807f549aada | 61 | float MPL115A1::getC12() |
Dautor | 5:4807f549aada | 62 | { |
Dautor | 5:4807f549aada | 63 | spi.write(0x94); // MSB c12 |
Dautor | 5:4807f549aada | 64 | uint16_t c12 = spi.write(0x00); |
Dautor | 5:4807f549aada | 65 | c12 = c12 << 8; |
Dautor | 5:4807f549aada | 66 | wait_ms(1); |
Dautor | 5:4807f549aada | 67 | spi.write(0x96); // LSB c12 |
Dautor | 5:4807f549aada | 68 | c12 |= spi.write(0x00); |
Dautor | 5:4807f549aada | 69 | float result = convertC12(c12); |
Dautor | 5:4807f549aada | 70 | wait_ms(1); |
Dautor | 5:4807f549aada | 71 | return result; |
Dautor | 5:4807f549aada | 72 | } |
Dautor | 5:4807f549aada | 73 | |
Dautor | 5:4807f549aada | 74 | uint16_t MPL115A1::getPadc() |
Dautor | 5:4807f549aada | 75 | { |
Dautor | 5:4807f549aada | 76 | uint16_t padc; |
Dautor | 5:4807f549aada | 77 | spi.write(0x80); |
Dautor | 5:4807f549aada | 78 | padc = spi.write(0x00); // MSB Padc |
Dautor | 5:4807f549aada | 79 | padc = padc << 8; |
Dautor | 5:4807f549aada | 80 | spi.write(0x82); |
Dautor | 5:4807f549aada | 81 | padc |= spi.write(0x00); // LSB Padc |
Dautor | 5:4807f549aada | 82 | padc = padc >> 6; |
Dautor | 5:4807f549aada | 83 | return padc; |
Dautor | 5:4807f549aada | 84 | } |
Dautor | 5:4807f549aada | 85 | |
Dautor | 5:4807f549aada | 86 | uint16_t MPL115A1::getTadc() |
Dautor | 5:4807f549aada | 87 | { |
Dautor | 5:4807f549aada | 88 | uint16_t tadc; |
Dautor | 5:4807f549aada | 89 | spi.write(0x84); |
Dautor | 5:4807f549aada | 90 | tadc = spi.write(0x00); // MSB Padc |
Dautor | 5:4807f549aada | 91 | tadc = tadc << 8; |
Dautor | 5:4807f549aada | 92 | spi.write(0x86); |
Dautor | 5:4807f549aada | 93 | tadc |= spi.write(0x00); // LSB Padc |
Dautor | 5:4807f549aada | 94 | tadc = tadc >> 6; |
Dautor | 5:4807f549aada | 95 | return tadc; |
Dautor | 5:4807f549aada | 96 | } |
Dautor | 5:4807f549aada | 97 | |
Dautor | 5:4807f549aada | 98 | float MPL115A1::getPressure(){ |
Dautor | 5:4807f549aada | 99 | cs = 0; |
Dautor | 5:4807f549aada | 100 | float a0 = getA0(); |
Dautor | 5:4807f549aada | 101 | float b1 = getB1(); |
Dautor | 5:4807f549aada | 102 | float b2 = getB2(); |
Dautor | 5:4807f549aada | 103 | float c12 = getC12(); |
Dautor | 5:4807f549aada | 104 | spi.write(0x00); |
Dautor | 5:4807f549aada | 105 | cs = 1; |
Dautor | 5:4807f549aada | 106 | |
Dautor | 5:4807f549aada | 107 | cs = 0; |
Dautor | 5:4807f549aada | 108 | spi.write(0x24); // Start conversion |
Dautor | 5:4807f549aada | 109 | spi.write(0x00); |
Dautor | 5:4807f549aada | 110 | cs = 1; |
Dautor | 5:4807f549aada | 111 | wait_ms(3); |
Dautor | 5:4807f549aada | 112 | |
Dautor | 5:4807f549aada | 113 | cs = 0; |
Dautor | 5:4807f549aada | 114 | uint16_t padc = getPadc(); |
Dautor | 5:4807f549aada | 115 | uint16_t tadc = getTadc(); |
Dautor | 5:4807f549aada | 116 | spi.write(0x00); |
Dautor | 5:4807f549aada | 117 | cs = 1; |
Dautor | 5:4807f549aada | 118 | |
Dautor | 5:4807f549aada | 119 | float pcomp = a0 + (b1 + c12 * tadc) * padc + b2 * tadc; |
Dautor | 5:4807f549aada | 120 | float pressure = pcomp * ((115-50)/(1023.0)) + 52; |
Dautor | 5:4807f549aada | 121 | pressure *= 10; // Calculate in hPa |
Dautor | 5:4807f549aada | 122 | return pressure; |
Dautor | 5:4807f549aada | 123 | } |
Dautor | 5:4807f549aada | 124 | |
Dautor | 5:4807f549aada | 125 | static float convertA0(uint16_t a0){ |
Dautor | 3:78ceda8ef565 | 126 | float tempA0; |
Dautor | 3:78ceda8ef565 | 127 | float tempFrac; |
Dautor | 3:78ceda8ef565 | 128 | int tempA0Int; |
Dautor | 3:78ceda8ef565 | 129 | uint8_t negativeFlag = 0; |
Dautor | 1:326ce5e200fb | 130 | |
Dautor | 1:326ce5e200fb | 131 | if(a0 & 0x8000){ |
Dautor | 5:4807f549aada | 132 | a0 ^= 0xFFFF; // Transform from 2's complement |
Dautor | 1:326ce5e200fb | 133 | a0 -= 1; |
Dautor | 3:78ceda8ef565 | 134 | negativeFlag = 1; |
Dautor | 1:326ce5e200fb | 135 | } |
Dautor | 1:326ce5e200fb | 136 | |
Dautor | 3:78ceda8ef565 | 137 | tempA0Int = a0 & 0x7FF8; |
Dautor | 3:78ceda8ef565 | 138 | tempA0Int = tempA0Int >> a0FracBits; |
Dautor | 1:326ce5e200fb | 139 | |
Dautor | 3:78ceda8ef565 | 140 | tempA0 = tempA0Int * 1.0; // Int part |
Dautor | 3:78ceda8ef565 | 141 | tempFrac = (1.0/(1<<3)); |
Dautor | 3:78ceda8ef565 | 142 | tempFrac *= (a0 & a0FracMask); |
Dautor | 3:78ceda8ef565 | 143 | tempA0 = tempA0 + tempFrac; |
Dautor | 1:326ce5e200fb | 144 | |
Dautor | 3:78ceda8ef565 | 145 | if(negativeFlag) tempA0 = -tempA0; |
Dautor | 1:326ce5e200fb | 146 | |
Dautor | 3:78ceda8ef565 | 147 | return tempA0; |
Dautor | 1:326ce5e200fb | 148 | } |
Dautor | 1:326ce5e200fb | 149 | |
Dautor | 5:4807f549aada | 150 | static float convertB1(uint16_t b1){ |
Dautor | 3:78ceda8ef565 | 151 | float tempB1; |
Dautor | 3:78ceda8ef565 | 152 | float tempFrac; |
Dautor | 3:78ceda8ef565 | 153 | int tempB1Int; |
Dautor | 3:78ceda8ef565 | 154 | uint8_t negativeFlag = 0; |
Dautor | 1:326ce5e200fb | 155 | |
Dautor | 5:4807f549aada | 156 | if(b1 & 0x8000){ |
Dautor | 1:326ce5e200fb | 157 | b1 ^= 0xFFFF; |
Dautor | 1:326ce5e200fb | 158 | b1 -= 1; |
Dautor | 3:78ceda8ef565 | 159 | negativeFlag = 1; |
Dautor | 1:326ce5e200fb | 160 | } |
Dautor | 1:326ce5e200fb | 161 | |
Dautor | 3:78ceda8ef565 | 162 | tempB1Int = b1 & 0x6000; |
Dautor | 3:78ceda8ef565 | 163 | tempB1Int = tempB1Int >> b1FracBits; |
Dautor | 1:326ce5e200fb | 164 | |
Dautor | 3:78ceda8ef565 | 165 | tempB1 = tempB1Int * 1.0; |
Dautor | 3:78ceda8ef565 | 166 | tempFrac = (b1 & b1FracMask) * (1.0/(1<<b1FracBits)); |
Dautor | 3:78ceda8ef565 | 167 | tempB1 = tempB1 + tempFrac; |
Dautor | 1:326ce5e200fb | 168 | |
Dautor | 3:78ceda8ef565 | 169 | if(negativeFlag) tempB1 = -tempB1; |
Dautor | 1:326ce5e200fb | 170 | |
Dautor | 3:78ceda8ef565 | 171 | return tempB1; |
Dautor | 1:326ce5e200fb | 172 | } |
Dautor | 1:326ce5e200fb | 173 | |
Dautor | 5:4807f549aada | 174 | static float convertB2(uint16_t b2){ |
Dautor | 3:78ceda8ef565 | 175 | float tempB2; |
Dautor | 3:78ceda8ef565 | 176 | float tempFrac; |
Dautor | 3:78ceda8ef565 | 177 | int tempB2Int; |
Dautor | 3:78ceda8ef565 | 178 | uint8_t negativeFlag = 0; |
Dautor | 1:326ce5e200fb | 179 | |
Dautor | 1:326ce5e200fb | 180 | if (b2 & 0x8000){ |
Dautor | 1:326ce5e200fb | 181 | b2 ^= 0xFFFF; |
Dautor | 1:326ce5e200fb | 182 | b2 -= 1; |
Dautor | 3:78ceda8ef565 | 183 | negativeFlag = 1; |
Dautor | 1:326ce5e200fb | 184 | } |
Dautor | 1:326ce5e200fb | 185 | |
Dautor | 3:78ceda8ef565 | 186 | tempB2Int = b2 & 0x4000; |
Dautor | 3:78ceda8ef565 | 187 | tempB2Int = tempB2Int >> b2FracBits; |
Dautor | 1:326ce5e200fb | 188 | |
Dautor | 3:78ceda8ef565 | 189 | tempB2 = tempB2Int * 1.0; |
Dautor | 3:78ceda8ef565 | 190 | tempFrac = (b2 & b2FracMask) * (1.0/(1<<b2FracBits)); |
Dautor | 3:78ceda8ef565 | 191 | tempB2 = tempB2 + tempFrac; |
Dautor | 1:326ce5e200fb | 192 | |
Dautor | 3:78ceda8ef565 | 193 | if(negativeFlag) tempB2 = -tempB2; |
Dautor | 1:326ce5e200fb | 194 | |
Dautor | 3:78ceda8ef565 | 195 | return tempB2; |
Dautor | 1:326ce5e200fb | 196 | } |
Dautor | 1:326ce5e200fb | 197 | |
Dautor | 5:4807f549aada | 198 | static float convertC12(uint16_t c12){ |
Dautor | 3:78ceda8ef565 | 199 | float tempC12; |
Dautor | 3:78ceda8ef565 | 200 | float tempFrac; |
Dautor | 3:78ceda8ef565 | 201 | uint8_t negativeFlag = 0; |
Dautor | 1:326ce5e200fb | 202 | |
Dautor | 1:326ce5e200fb | 203 | c12 = c12 >> 2; |
Dautor | 1:326ce5e200fb | 204 | |
Dautor | 1:326ce5e200fb | 205 | if (c12 & 0x2000){ |
Dautor | 1:326ce5e200fb | 206 | c12 ^= 0xFFFF; |
Dautor | 1:326ce5e200fb | 207 | c12 -= 1; |
Dautor | 3:78ceda8ef565 | 208 | negativeFlag = 1; |
Dautor | 1:326ce5e200fb | 209 | } |
Dautor | 1:326ce5e200fb | 210 | |
Dautor | 3:78ceda8ef565 | 211 | tempC12 = 0.000000000; |
Dautor | 3:78ceda8ef565 | 212 | tempFrac = (c12 & c12FracMask) * (1.0/(1<<c12FracBits)); |
Dautor | 3:78ceda8ef565 | 213 | tempC12 = tempC12 + tempFrac; |
Dautor | 1:326ce5e200fb | 214 | |
Dautor | 3:78ceda8ef565 | 215 | if(negativeFlag) tempC12 = -tempC12; |
Dautor | 1:326ce5e200fb | 216 | |
Dautor | 3:78ceda8ef565 | 217 | return tempC12; |
Dautor | 1:326ce5e200fb | 218 | } |