Jurica Resetar / Mbed OS iBeacon acnsensa

Dependencies:   LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPL115A1.cpp Source File

MPL115A1.cpp

00001 #include "MPL115A1.h"
00002 
00003 // These values correspond to the values described here:
00004 // https://www.nxp.com/docs/en/data-sheet/MPL115A1.pdf
00005 
00006 #define a0FracBits  3
00007 #define b1FracBits  13
00008 #define b2FracBits  14
00009 #define c12FracBits 22
00010 #define a0FracMask  0x0007
00011 #define b1FracMask  0x1FFF
00012 #define b2FracMask  0x3FFF
00013 #define c12FracMask 0x1FFF
00014 
00015 static float convertA0(uint16_t a0);
00016 static float convertB1(uint16_t b1);
00017 static float convertB2(uint16_t b2);
00018 static float convertC12(uint16_t c12);
00019 
00020 MPL115A1::MPL115A1(SPI &spi, NRF52_DigitalOut &cs) : spi(spi), cs(cs) {}
00021 
00022 float MPL115A1::getA0()
00023 {
00024     spi.write(0x88); // MSB a0
00025     uint16_t a0 = spi.write(0x00);   
00026     a0 = a0 << 8;
00027     wait_ms(1); 
00028     spi.write(0x8A); // LSB a0
00029     a0 |= spi.write(0x00);
00030     float result = convertA0(a0);
00031     wait_ms(1);
00032     return result;
00033 }
00034 
00035 float MPL115A1::getB1()
00036 {
00037     spi.write(0x8C); // MSB b1
00038     uint16_t b1 = spi.write(0x00);   
00039     b1 = b1 << 8;
00040     wait_ms(1);
00041     spi.write(0x8E); // LSB b1
00042     b1 |= spi.write(0x00);
00043     float result = convertB1(b1);
00044     wait_ms(1);
00045     return result;
00046 }
00047 
00048 float MPL115A1::getB2()
00049 {
00050     spi.write(0x90); // MSB b2
00051     uint16_t b2 = spi.write(0x00);   
00052     b2 = b2 << 8;
00053     wait_ms(1);
00054     spi.write(0x92); // LSB b2
00055     b2 |= spi.write(0x00);
00056     float result = convertB2(b2);
00057     wait_ms(1);
00058     return result;
00059 }
00060 
00061 float MPL115A1::getC12()
00062 {
00063     spi.write(0x94);        // MSB c12
00064     uint16_t c12 = spi.write(0x00);  
00065     c12 = c12 << 8;
00066     wait_ms(1);
00067     spi.write(0x96);        // LSB c12
00068     c12 |= spi.write(0x00);
00069     float result = convertC12(c12);
00070     wait_ms(1);
00071     return result;
00072 }
00073 
00074 uint16_t MPL115A1::getPadc()
00075 {
00076     uint16_t padc;
00077     spi.write(0x80);
00078     padc = spi.write(0x00);  // MSB Padc
00079     padc = padc << 8;
00080     spi.write(0x82);
00081     padc |= spi.write(0x00); // LSB Padc
00082     padc = padc >> 6;
00083     return padc;
00084 }
00085 
00086 uint16_t MPL115A1::getTadc()
00087 {
00088     uint16_t tadc;
00089     spi.write(0x84);                
00090     tadc = spi.write(0x00);  // MSB Padc
00091     tadc = tadc << 8;
00092     spi.write(0x86);
00093     tadc |= spi.write(0x00); // LSB Padc
00094     tadc = tadc >> 6;
00095     return tadc;
00096 }
00097 
00098 float MPL115A1::getPressure(){
00099     cs = 0;
00100     float a0  = getA0();
00101     float b1  = getB1();
00102     float b2  = getB2();
00103     float c12 = getC12();
00104     spi.write(0x00);
00105     cs = 1;
00106     
00107     cs = 0;
00108     spi.write(0x24);        // Start conversion
00109     spi.write(0x00);
00110     cs = 1;
00111     wait_ms(3);
00112     
00113     cs = 0;
00114     uint16_t padc = getPadc();
00115     uint16_t tadc = getTadc();
00116     spi.write(0x00);
00117     cs = 1;
00118     
00119     float pcomp = a0 + (b1 + c12 * tadc) * padc + b2 * tadc;
00120     float pressure = pcomp * ((115-50)/(1023.0)) + 52;
00121     pressure *= 10; // Calculate in hPa
00122     return pressure;
00123 }
00124 
00125 static float convertA0(uint16_t a0){
00126     float tempA0;
00127     float tempFrac;
00128     int tempA0Int;
00129     uint8_t negativeFlag = 0;
00130     
00131     if(a0 & 0x8000){
00132         a0 ^= 0xFFFF; // Transform from 2's complement
00133         a0 -= 1;
00134         negativeFlag = 1;
00135     }
00136     
00137     tempA0Int = a0 & 0x7FF8;
00138     tempA0Int = tempA0Int >> a0FracBits;
00139     
00140     tempA0 = tempA0Int * 1.0;    // Int part
00141     tempFrac = (1.0/(1<<3));
00142     tempFrac *= (a0 & a0FracMask);
00143     tempA0 = tempA0 + tempFrac;
00144     
00145     if(negativeFlag) tempA0 = -tempA0;
00146     
00147     return tempA0;
00148 }
00149 
00150 static float convertB1(uint16_t b1){
00151     float tempB1;
00152     float tempFrac;
00153     int tempB1Int;
00154     uint8_t negativeFlag = 0;
00155     
00156     if(b1 & 0x8000){
00157         b1 ^= 0xFFFF;
00158         b1 -= 1;
00159         negativeFlag = 1;
00160     }
00161     
00162     tempB1Int = b1 & 0x6000;
00163     tempB1Int = tempB1Int >> b1FracBits;
00164     
00165     tempB1 = tempB1Int * 1.0;
00166     tempFrac = (b1 & b1FracMask) * (1.0/(1<<b1FracBits));
00167     tempB1 = tempB1 + tempFrac;
00168     
00169     if(negativeFlag) tempB1 = -tempB1;
00170     
00171     return tempB1;
00172 }
00173 
00174 static float convertB2(uint16_t b2){
00175     float tempB2;
00176     float tempFrac;
00177     int tempB2Int;
00178     uint8_t negativeFlag = 0;
00179     
00180     if (b2 & 0x8000){
00181         b2 ^= 0xFFFF;
00182         b2 -= 1;
00183         negativeFlag = 1;
00184     }
00185     
00186     tempB2Int = b2 & 0x4000;
00187     tempB2Int = tempB2Int >> b2FracBits;
00188     
00189     tempB2 = tempB2Int * 1.0;
00190     tempFrac = (b2 & b2FracMask) * (1.0/(1<<b2FracBits));
00191     tempB2 = tempB2 + tempFrac;
00192     
00193     if(negativeFlag) tempB2 = -tempB2;
00194     
00195     return tempB2;
00196 }
00197 
00198 static float convertC12(uint16_t c12){
00199     float tempC12;
00200     float tempFrac;
00201     uint8_t negativeFlag = 0;
00202     
00203     c12 = c12 >> 2;
00204     
00205     if (c12 & 0x2000){
00206         c12 ^= 0xFFFF;
00207         c12 -= 1;
00208         negativeFlag = 1;
00209     }
00210     
00211     tempC12 = 0.000000000;
00212     tempFrac = (c12 & c12FracMask) * (1.0/(1<<c12FracBits));
00213     tempC12 = tempC12 + tempFrac;
00214     
00215     if(negativeFlag) tempC12 = -tempC12;
00216     
00217     return tempC12;
00218 }