aconno acnsensa project for iOS devices with iBeacon packets support.
Dependencies: LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common
Diff: MPL115A1/MPL115A1.cpp
- Revision:
- 5:4807f549aada
- Parent:
- 4:634796e5b8c3
- Child:
- 6:51745805d8b0
--- a/MPL115A1/MPL115A1.cpp Fri Mar 02 14:32:08 2018 +0000 +++ b/MPL115A1/MPL115A1.cpp Mon Mar 05 09:40:34 2018 +0000 @@ -12,21 +12,124 @@ #define b2FracMask 0x3FFF #define c12FracMask 0x1FFF -float getA0(uint16_t a0); -float getB1(uint16_t b1); -float getC12(uint16_t c12); -float getPressure(); +static float convertA0(uint16_t a0); +static float convertB1(uint16_t b1); +static float convertB2(uint16_t b2); +static float convertC12(uint16_t c12); MPL115A1::MPL115A1(SPI &spi, NRF52_DigitalOut &cs) : spi(spi), cs(cs) {} -float getA0(uint16_t a0){ +float MPL115A1::getA0() +{ + spi.write(0x88); // MSB a0 + uint16_t a0 = spi.write(0x00); + a0 = a0 << 8; + wait_ms(1); + spi.write(0x8A); // LSB a0 + a0 |= spi.write(0x00); + float result = convertA0(a0); + wait_ms(1); + return result; +} + +float MPL115A1::getB1() +{ + spi.write(0x8C); // MSB b1 + uint16_t b1 = spi.write(0x00); + b1 = b1 << 8; + wait_ms(1); + spi.write(0x8E); // LSB b1 + b1 |= spi.write(0x00); + float result = convertB1(b1); + wait_ms(1); + return result; +} + +float MPL115A1::getB2() +{ + spi.write(0x90); // MSB b2 + uint16_t b2 = spi.write(0x00); + b2 = b2 << 8; + wait_ms(1); + spi.write(0x92); // LSB b2 + b2 |= spi.write(0x00); + float result = convertB2(b2); + wait_ms(1); + return result; +} + +float MPL115A1::getC12() +{ + spi.write(0x94); // MSB c12 + uint16_t c12 = spi.write(0x00); + c12 = c12 << 8; + wait_ms(1); + spi.write(0x96); // LSB c12 + c12 |= spi.write(0x00); + float result = convertC12(c12); + wait_ms(1); + return result; +} + +uint16_t MPL115A1::getPadc() +{ + uint16_t padc; + spi.write(0x80); + padc = spi.write(0x00); // MSB Padc + padc = padc << 8; + spi.write(0x82); + padc |= spi.write(0x00); // LSB Padc + padc = padc >> 6; + return padc; +} + +uint16_t MPL115A1::getTadc() +{ + uint16_t tadc; + spi.write(0x84); + tadc = spi.write(0x00); // MSB Padc + tadc = tadc << 8; + spi.write(0x86); + tadc |= spi.write(0x00); // LSB Padc + tadc = tadc >> 6; + return tadc; +} + +float MPL115A1::getPressure(){ + cs = 0; + float a0 = getA0(); + float b1 = getB1(); + float b2 = getB2(); + float c12 = getC12(); + spi.write(0x00); + cs = 1; + + cs = 0; + spi.write(0x24); // Start conversion + spi.write(0x00); + cs = 1; + wait_ms(3); + + cs = 0; + uint16_t padc = getPadc(); + uint16_t tadc = getTadc(); + spi.write(0x00); + cs = 1; + + float pcomp = a0 + (b1 + c12 * tadc) * padc + b2 * tadc; + float pressure = pcomp * ((115-50)/(1023.0)) + 52; + pressure *= 10; // Calculate in hPa + return pressure; +} + +static float convertA0(uint16_t a0){ float tempA0; float tempFrac; int tempA0Int; uint8_t negativeFlag = 0; if(a0 & 0x8000){ - a0 ^= 0xFFFF; // Transform from 2's complement + a0 ^= 0xFFFF; // Transform from 2's complement a0 -= 1; negativeFlag = 1; } @@ -44,13 +147,13 @@ return tempA0; } -float getB1(uint16_t b1){ +static float convertB1(uint16_t b1){ float tempB1; float tempFrac; int tempB1Int; uint8_t negativeFlag = 0; - if (b1 & 0x8000){ + if(b1 & 0x8000){ b1 ^= 0xFFFF; b1 -= 1; negativeFlag = 1; @@ -68,7 +171,7 @@ return tempB1; } -float getB2(uint16_t b2){ +static float convertB2(uint16_t b2){ float tempB2; float tempFrac; int tempB2Int; @@ -92,8 +195,7 @@ return tempB2; } - -float getC12(uint16_t c12){ +static float convertC12(uint16_t c12){ float tempC12; float tempFrac; uint8_t negativeFlag = 0; @@ -114,84 +216,3 @@ return tempC12; } - -float MPL115A1::getPressure(){ - float pcomp = 0x00; - volatile float pressure; - - uint16_t a0 = 0; - uint16_t b1 = 0; - uint16_t b2 = 0; - uint16_t c12 = 0; - uint16_t padc = 0; - uint16_t tadc = 0; - - float a0F, b1F, b2F, c12F; - cs = 0; - spi.write(0x88); // MSB a0 - a0 = spi.write(0x00); - a0 = a0 << 8; - wait_ms(1); - spi.write(0x8A); // LSB a0 - a0 |= spi.write(0x00); - a0F = getA0(a0); - wait_ms(1); - - spi.write(0x8C); // MSB b1 - b1 = spi.write(0x00); - b1 = b1 << 8; - wait_ms(1); - spi.write(0x8E); // LSB b1 - b1 |= spi.write(0x00); - b1F = getB1(b1); - wait_ms(1); - - spi.write(0x90); // MSB b2 - b2 = spi.write(0x00); - b2 = b2 << 8; - wait_ms(1); - spi.write(0x92); // LSB b2 - b2 |= spi.write(0x00); - b2F = getB2(b2); - wait_ms(1); - - spi.write(0x94); // MSB c12 - c12 = spi.write(0x00); - c12 = c12 << 8; - wait_ms(1); - spi.write(0x96); // LSB c12 - c12 |= spi.write(0x00); - c12F = getC12(c12); - wait_ms(1); - spi.write(0x00); - cs = 1; - - cs = 0; - spi.write(0x24); // Start conversion - spi.write(0x00); - cs = 1; - wait_ms(3); - - cs = 0; - spi.write(0x80); - padc = spi.write(0x00); // MSB Padc - padc = padc << 8; - spi.write(0x82); - padc |= spi.write(0x00); // LSB Padc - padc = padc >> 6; - - spi.write(0x84); - tadc = spi.write(0x00); // MSB Padc - tadc = tadc << 8; - spi.write(0x86); - tadc |= spi.write(0x00); // LSB Padc - tadc = tadc >> 6; - - spi.write(0x00); - cs = 1; - - pcomp = a0F + (b1F + c12F * tadc) * padc + b2F * tadc; - pressure = pcomp * ((115-50)/(1023.0)) + 52; - pressure *= 10; // Calculate in hPa - return pressure; -}