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