aconno acnsensa project for iOS devices with iBeacon packets support.

Dependencies:   LSM9DS1 Si7006A20 aconno_SEGGER_RTT aconno_bsp adc52832_common

Revision:
1:326ce5e200fb
Child:
3:78ceda8ef565
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MPL115A1/MPL115A1.cpp	Wed Dec 13 09:37:21 2017 +0000
@@ -0,0 +1,196 @@
+#include "MPL115A1.h"
+
+float get_a0(uint16_t a0);
+float get_b1(uint16_t b1);
+float get_c1(uint16_t c1);
+float get_c12(uint16_t c12);
+float getPressure();
+
+MPL115A1::MPL115A1(SPI &spi, NRF52_DigitalOut &cs) : spi(spi), cs(cs) {}
+
+float get_a0(uint16_t a0){
+    float temp_a0;
+    float temp_frac;
+    int temp_a0_int;
+    uint8_t negative_flag = 0;
+    
+    if(a0 & 0x8000){
+        a0 ^= 0xFFFF;   // Transform from 2's complement
+        a0 -= 1;
+        negative_flag = 1;
+    }
+    
+    temp_a0_int = a0 & 0x7FF8;
+    temp_a0_int = temp_a0_int >> a0_frac_bits;
+    
+    temp_a0 = temp_a0_int * 1.0;    // Int part
+    temp_frac = (1.0/(1<<3));
+    temp_frac *= (a0 & a0_frac_mask);
+    temp_a0 = temp_a0 + temp_frac;
+    
+    if(negative_flag) temp_a0 = -temp_a0;
+    
+    return temp_a0;
+}
+
+float get_b1(uint16_t b1){
+    float temp_b1;
+    float temp_frac;
+    int temp_b1_int;
+    uint8_t negative_flag = 0;
+    
+    if (b1 & 0x8000){
+        b1 ^= 0xFFFF;
+        b1 -= 1;
+        negative_flag = 1;
+    }
+    
+    temp_b1_int = b1 & 0x6000;
+    temp_b1_int = temp_b1_int >> b1_frac_bits;
+    
+    temp_b1 = temp_b1_int * 1.0;
+    temp_frac = (b1 & b1_frac_mask) * (1.0/(1<<b1_frac_bits));
+    temp_b1 = temp_b1 + temp_frac;
+    
+    if(negative_flag) temp_b1 = -temp_b1;
+    
+    return temp_b1;
+}
+
+float get_b2(uint16_t b2){
+    float temp_b2;
+    float temp_frac;
+    int temp_b2_int;
+    uint8_t negative_flag = 0;
+    
+    if (b2 & 0x8000){
+        b2 ^= 0xFFFF;
+        b2 -= 1;
+        negative_flag = 1;
+    }
+    
+    temp_b2_int = b2 & 0x4000;
+    temp_b2_int = temp_b2_int >> b2_frac_bits;
+    
+    temp_b2 = temp_b2_int * 1.0;
+    temp_frac = (b2 & b2_frac_mask) * (1.0/(1<<b2_frac_bits));
+    temp_b2 = temp_b2 + temp_frac;
+    
+    if(negative_flag) temp_b2 = -temp_b2;
+    
+    return temp_b2;
+}
+
+
+float get_c12(uint16_t c12){
+    float temp_c12;
+    float temp_frac;
+    uint8_t negative_flag = 0;
+    
+    c12 = c12 >> 2;
+    
+    if (c12 & 0x2000){
+        c12 ^= 0xFFFF;
+        c12 -= 1;
+        negative_flag = 1;
+    }
+    
+    temp_c12 = 0.000000000;
+    temp_frac = (c12 & c12_frac_mask) * (1.0/(1<<c12_frac_bits));
+    temp_c12 = temp_c12 + temp_frac;
+    
+    if(negative_flag) temp_c12 = -temp_c12;
+    
+    return temp_c12;
+}
+
+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 a0_f, b1_f, b2_f, c12_f;
+    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);
+    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);
+    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);
+    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);
+    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;
+    
+    a0_f  = get_a0(a0);
+    b1_f  = get_b1(b1);
+    b2_f  = get_b2(b2);
+    c12_f = get_c12(c12);
+    
+    /*            
+    float c12x2 = c12_f * tadc;
+    float a1_f = b1_f + c12x2;
+    float a1x1 = a1_f * padc;
+    float y1 = a0_f + a1x1;
+    float a2x2 = b2_f * tadc;
+    pcomp = y1 + a2x2;
+    */
+
+    pcomp = a0_f + (b1_f + c12_f * tadc) * padc + b2_f * tadc;
+    pressure = pcomp * ((115-50)/(1023.0)) + 52;    // Was + 50
+    pressure *= 10;     // Calculate in hPa
+    return pressure;
+}