aconno acnsensa project for iOS devices with iBeacon packets support.
Dependencies: LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common
MPL115A1/MPL115A1.cpp@4:634796e5b8c3, 2018-03-02 (annotated)
- Committer:
- Dautor
- Date:
- Fri Mar 02 14:32:08 2018 +0000
- Revision:
- 4:634796e5b8c3
- Parent:
- 3:78ceda8ef565
- Child:
- 5:4807f549aada
Added battery percentage to the MSD
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 | 3:78ceda8ef565 | 15 | float getA0(uint16_t a0); |
Dautor | 3:78ceda8ef565 | 16 | float getB1(uint16_t b1); |
Dautor | 3:78ceda8ef565 | 17 | float getC12(uint16_t c12); |
Dautor | 1:326ce5e200fb | 18 | float getPressure(); |
Dautor | 1:326ce5e200fb | 19 | |
Dautor | 1:326ce5e200fb | 20 | MPL115A1::MPL115A1(SPI &spi, NRF52_DigitalOut &cs) : spi(spi), cs(cs) {} |
Dautor | 1:326ce5e200fb | 21 | |
Dautor | 3:78ceda8ef565 | 22 | float getA0(uint16_t a0){ |
Dautor | 3:78ceda8ef565 | 23 | float tempA0; |
Dautor | 3:78ceda8ef565 | 24 | float tempFrac; |
Dautor | 3:78ceda8ef565 | 25 | int tempA0Int; |
Dautor | 3:78ceda8ef565 | 26 | uint8_t negativeFlag = 0; |
Dautor | 1:326ce5e200fb | 27 | |
Dautor | 1:326ce5e200fb | 28 | if(a0 & 0x8000){ |
Dautor | 1:326ce5e200fb | 29 | a0 ^= 0xFFFF; // Transform from 2's complement |
Dautor | 1:326ce5e200fb | 30 | a0 -= 1; |
Dautor | 3:78ceda8ef565 | 31 | negativeFlag = 1; |
Dautor | 1:326ce5e200fb | 32 | } |
Dautor | 1:326ce5e200fb | 33 | |
Dautor | 3:78ceda8ef565 | 34 | tempA0Int = a0 & 0x7FF8; |
Dautor | 3:78ceda8ef565 | 35 | tempA0Int = tempA0Int >> a0FracBits; |
Dautor | 1:326ce5e200fb | 36 | |
Dautor | 3:78ceda8ef565 | 37 | tempA0 = tempA0Int * 1.0; // Int part |
Dautor | 3:78ceda8ef565 | 38 | tempFrac = (1.0/(1<<3)); |
Dautor | 3:78ceda8ef565 | 39 | tempFrac *= (a0 & a0FracMask); |
Dautor | 3:78ceda8ef565 | 40 | tempA0 = tempA0 + tempFrac; |
Dautor | 1:326ce5e200fb | 41 | |
Dautor | 3:78ceda8ef565 | 42 | if(negativeFlag) tempA0 = -tempA0; |
Dautor | 1:326ce5e200fb | 43 | |
Dautor | 3:78ceda8ef565 | 44 | return tempA0; |
Dautor | 1:326ce5e200fb | 45 | } |
Dautor | 1:326ce5e200fb | 46 | |
Dautor | 3:78ceda8ef565 | 47 | float getB1(uint16_t b1){ |
Dautor | 3:78ceda8ef565 | 48 | float tempB1; |
Dautor | 3:78ceda8ef565 | 49 | float tempFrac; |
Dautor | 3:78ceda8ef565 | 50 | int tempB1Int; |
Dautor | 3:78ceda8ef565 | 51 | uint8_t negativeFlag = 0; |
Dautor | 1:326ce5e200fb | 52 | |
Dautor | 1:326ce5e200fb | 53 | if (b1 & 0x8000){ |
Dautor | 1:326ce5e200fb | 54 | b1 ^= 0xFFFF; |
Dautor | 1:326ce5e200fb | 55 | b1 -= 1; |
Dautor | 3:78ceda8ef565 | 56 | negativeFlag = 1; |
Dautor | 1:326ce5e200fb | 57 | } |
Dautor | 1:326ce5e200fb | 58 | |
Dautor | 3:78ceda8ef565 | 59 | tempB1Int = b1 & 0x6000; |
Dautor | 3:78ceda8ef565 | 60 | tempB1Int = tempB1Int >> b1FracBits; |
Dautor | 1:326ce5e200fb | 61 | |
Dautor | 3:78ceda8ef565 | 62 | tempB1 = tempB1Int * 1.0; |
Dautor | 3:78ceda8ef565 | 63 | tempFrac = (b1 & b1FracMask) * (1.0/(1<<b1FracBits)); |
Dautor | 3:78ceda8ef565 | 64 | tempB1 = tempB1 + tempFrac; |
Dautor | 1:326ce5e200fb | 65 | |
Dautor | 3:78ceda8ef565 | 66 | if(negativeFlag) tempB1 = -tempB1; |
Dautor | 1:326ce5e200fb | 67 | |
Dautor | 3:78ceda8ef565 | 68 | return tempB1; |
Dautor | 1:326ce5e200fb | 69 | } |
Dautor | 1:326ce5e200fb | 70 | |
Dautor | 3:78ceda8ef565 | 71 | float getB2(uint16_t b2){ |
Dautor | 3:78ceda8ef565 | 72 | float tempB2; |
Dautor | 3:78ceda8ef565 | 73 | float tempFrac; |
Dautor | 3:78ceda8ef565 | 74 | int tempB2Int; |
Dautor | 3:78ceda8ef565 | 75 | uint8_t negativeFlag = 0; |
Dautor | 1:326ce5e200fb | 76 | |
Dautor | 1:326ce5e200fb | 77 | if (b2 & 0x8000){ |
Dautor | 1:326ce5e200fb | 78 | b2 ^= 0xFFFF; |
Dautor | 1:326ce5e200fb | 79 | b2 -= 1; |
Dautor | 3:78ceda8ef565 | 80 | negativeFlag = 1; |
Dautor | 1:326ce5e200fb | 81 | } |
Dautor | 1:326ce5e200fb | 82 | |
Dautor | 3:78ceda8ef565 | 83 | tempB2Int = b2 & 0x4000; |
Dautor | 3:78ceda8ef565 | 84 | tempB2Int = tempB2Int >> b2FracBits; |
Dautor | 1:326ce5e200fb | 85 | |
Dautor | 3:78ceda8ef565 | 86 | tempB2 = tempB2Int * 1.0; |
Dautor | 3:78ceda8ef565 | 87 | tempFrac = (b2 & b2FracMask) * (1.0/(1<<b2FracBits)); |
Dautor | 3:78ceda8ef565 | 88 | tempB2 = tempB2 + tempFrac; |
Dautor | 1:326ce5e200fb | 89 | |
Dautor | 3:78ceda8ef565 | 90 | if(negativeFlag) tempB2 = -tempB2; |
Dautor | 1:326ce5e200fb | 91 | |
Dautor | 3:78ceda8ef565 | 92 | return tempB2; |
Dautor | 1:326ce5e200fb | 93 | } |
Dautor | 1:326ce5e200fb | 94 | |
Dautor | 1:326ce5e200fb | 95 | |
Dautor | 3:78ceda8ef565 | 96 | float getC12(uint16_t c12){ |
Dautor | 3:78ceda8ef565 | 97 | float tempC12; |
Dautor | 3:78ceda8ef565 | 98 | float tempFrac; |
Dautor | 3:78ceda8ef565 | 99 | uint8_t negativeFlag = 0; |
Dautor | 1:326ce5e200fb | 100 | |
Dautor | 1:326ce5e200fb | 101 | c12 = c12 >> 2; |
Dautor | 1:326ce5e200fb | 102 | |
Dautor | 1:326ce5e200fb | 103 | if (c12 & 0x2000){ |
Dautor | 1:326ce5e200fb | 104 | c12 ^= 0xFFFF; |
Dautor | 1:326ce5e200fb | 105 | c12 -= 1; |
Dautor | 3:78ceda8ef565 | 106 | negativeFlag = 1; |
Dautor | 1:326ce5e200fb | 107 | } |
Dautor | 1:326ce5e200fb | 108 | |
Dautor | 3:78ceda8ef565 | 109 | tempC12 = 0.000000000; |
Dautor | 3:78ceda8ef565 | 110 | tempFrac = (c12 & c12FracMask) * (1.0/(1<<c12FracBits)); |
Dautor | 3:78ceda8ef565 | 111 | tempC12 = tempC12 + tempFrac; |
Dautor | 1:326ce5e200fb | 112 | |
Dautor | 3:78ceda8ef565 | 113 | if(negativeFlag) tempC12 = -tempC12; |
Dautor | 1:326ce5e200fb | 114 | |
Dautor | 3:78ceda8ef565 | 115 | return tempC12; |
Dautor | 1:326ce5e200fb | 116 | } |
Dautor | 1:326ce5e200fb | 117 | |
Dautor | 1:326ce5e200fb | 118 | float MPL115A1::getPressure(){ |
Dautor | 1:326ce5e200fb | 119 | float pcomp = 0x00; |
Dautor | 1:326ce5e200fb | 120 | volatile float pressure; |
Dautor | 1:326ce5e200fb | 121 | |
Dautor | 1:326ce5e200fb | 122 | uint16_t a0 = 0; |
Dautor | 1:326ce5e200fb | 123 | uint16_t b1 = 0; |
Dautor | 1:326ce5e200fb | 124 | uint16_t b2 = 0; |
Dautor | 1:326ce5e200fb | 125 | uint16_t c12 = 0; |
Dautor | 1:326ce5e200fb | 126 | uint16_t padc = 0; |
Dautor | 1:326ce5e200fb | 127 | uint16_t tadc = 0; |
Dautor | 1:326ce5e200fb | 128 | |
Dautor | 3:78ceda8ef565 | 129 | float a0F, b1F, b2F, c12F; |
Dautor | 1:326ce5e200fb | 130 | cs = 0; |
Dautor | 1:326ce5e200fb | 131 | spi.write(0x88); // MSB a0 |
Dautor | 1:326ce5e200fb | 132 | a0 = spi.write(0x00); |
Dautor | 1:326ce5e200fb | 133 | a0 = a0 << 8; |
Dautor | 1:326ce5e200fb | 134 | wait_ms(1); |
Dautor | 1:326ce5e200fb | 135 | spi.write(0x8A); // LSB a0 |
Dautor | 1:326ce5e200fb | 136 | a0 |= spi.write(0x00); |
Dautor | 4:634796e5b8c3 | 137 | a0F = getA0(a0); |
Dautor | 1:326ce5e200fb | 138 | wait_ms(1); |
Dautor | 1:326ce5e200fb | 139 | |
Dautor | 1:326ce5e200fb | 140 | spi.write(0x8C); // MSB b1 |
Dautor | 1:326ce5e200fb | 141 | b1 = spi.write(0x00); |
Dautor | 1:326ce5e200fb | 142 | b1 = b1 << 8; |
Dautor | 1:326ce5e200fb | 143 | wait_ms(1); |
Dautor | 1:326ce5e200fb | 144 | spi.write(0x8E); // LSB b1 |
Dautor | 1:326ce5e200fb | 145 | b1 |= spi.write(0x00); |
Dautor | 4:634796e5b8c3 | 146 | b1F = getB1(b1); |
Dautor | 1:326ce5e200fb | 147 | wait_ms(1); |
Dautor | 1:326ce5e200fb | 148 | |
Dautor | 1:326ce5e200fb | 149 | spi.write(0x90); // MSB b2 |
Dautor | 1:326ce5e200fb | 150 | b2 = spi.write(0x00); |
Dautor | 1:326ce5e200fb | 151 | b2 = b2 << 8; |
Dautor | 1:326ce5e200fb | 152 | wait_ms(1); |
Dautor | 1:326ce5e200fb | 153 | spi.write(0x92); // LSB b2 |
Dautor | 1:326ce5e200fb | 154 | b2 |= spi.write(0x00); |
Dautor | 4:634796e5b8c3 | 155 | b2F = getB2(b2); |
Dautor | 1:326ce5e200fb | 156 | wait_ms(1); |
Dautor | 1:326ce5e200fb | 157 | |
Dautor | 1:326ce5e200fb | 158 | spi.write(0x94); // MSB c12 |
Dautor | 1:326ce5e200fb | 159 | c12 = spi.write(0x00); |
Dautor | 1:326ce5e200fb | 160 | c12 = c12 << 8; |
Dautor | 1:326ce5e200fb | 161 | wait_ms(1); |
Dautor | 1:326ce5e200fb | 162 | spi.write(0x96); // LSB c12 |
Dautor | 1:326ce5e200fb | 163 | c12 |= spi.write(0x00); |
Dautor | 4:634796e5b8c3 | 164 | c12F = getC12(c12); |
Dautor | 1:326ce5e200fb | 165 | wait_ms(1); |
Dautor | 1:326ce5e200fb | 166 | spi.write(0x00); |
Dautor | 1:326ce5e200fb | 167 | cs = 1; |
Dautor | 1:326ce5e200fb | 168 | |
Dautor | 1:326ce5e200fb | 169 | cs = 0; |
Dautor | 1:326ce5e200fb | 170 | spi.write(0x24); // Start conversion |
Dautor | 1:326ce5e200fb | 171 | spi.write(0x00); |
Dautor | 1:326ce5e200fb | 172 | cs = 1; |
Dautor | 1:326ce5e200fb | 173 | wait_ms(3); |
Dautor | 1:326ce5e200fb | 174 | |
Dautor | 1:326ce5e200fb | 175 | cs = 0; |
Dautor | 1:326ce5e200fb | 176 | spi.write(0x80); |
Dautor | 1:326ce5e200fb | 177 | padc = spi.write(0x00); // MSB Padc |
Dautor | 1:326ce5e200fb | 178 | padc = padc << 8; |
Dautor | 1:326ce5e200fb | 179 | spi.write(0x82); |
Dautor | 1:326ce5e200fb | 180 | padc |= spi.write(0x00); // LSB Padc |
Dautor | 1:326ce5e200fb | 181 | padc = padc >> 6; |
Dautor | 1:326ce5e200fb | 182 | |
Dautor | 1:326ce5e200fb | 183 | spi.write(0x84); |
Dautor | 1:326ce5e200fb | 184 | tadc = spi.write(0x00); // MSB Padc |
Dautor | 1:326ce5e200fb | 185 | tadc = tadc << 8; |
Dautor | 1:326ce5e200fb | 186 | spi.write(0x86); |
Dautor | 1:326ce5e200fb | 187 | tadc |= spi.write(0x00); // LSB Padc |
Dautor | 1:326ce5e200fb | 188 | tadc = tadc >> 6; |
Dautor | 1:326ce5e200fb | 189 | |
Dautor | 1:326ce5e200fb | 190 | spi.write(0x00); |
Dautor | 1:326ce5e200fb | 191 | cs = 1; |
Dautor | 1:326ce5e200fb | 192 | |
Dautor | 3:78ceda8ef565 | 193 | pcomp = a0F + (b1F + c12F * tadc) * padc + b2F * tadc; |
Dautor | 4:634796e5b8c3 | 194 | pressure = pcomp * ((115-50)/(1023.0)) + 52; |
Dautor | 1:326ce5e200fb | 195 | pressure *= 10; // Calculate in hPa |
Dautor | 1:326ce5e200fb | 196 | return pressure; |
Dautor | 1:326ce5e200fb | 197 | } |