Jurica Resetar / aconnoMPL115A1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MPL115A1.cpp Source File

MPL115A1.cpp

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