• AVR Freaks

Hot!PIC18 & BMP280 Sensor data calculations

Page: 12 > Showing page 1 of 2
Author
Kabak
Junior Member
  • Total Posts : 105
  • Reward points : 0
  • Joined: 2014/07/30 03:47:55
  • Location: 0
  • Status: offline
2020/10/30 13:14:08 (permalink)
0

PIC18 & BMP280 Sensor data calculations

Hi
 
Anybody made conversion of the BMP280 sensor RAW data into real Temperature & Pressure values using PIC18-33 MCU ?
Datasheet recommend to made calculations using 64-bits data types. They provide the 32-bit code version, but even there data exceed PIC18 int32_t - 0xff ff ff ff value after shifting, so values are incorrect after calculations.
 
Maybe someone have another formulas to calculate Temperature & Pressure ?
 
Original calc. formulas form BMP datasheet
// Returns temperature in DegC, resolution is 0.01 DegC. Output value of “5123” equals 51.23 DegC.
// t_fine carries fine temperature as global value
BMP280_S32_t t_fine;
BMP280_S32_t bmp280_compensate_T_int32(BMP280_S32_t adc_T)
{
BMP280_S32_t var1, var2, T;

var1 = ((((adc_T>>3) – ((BMP280_S32_t)dig_T1<<1))) * ((BMP280_S32_t)dig_T2)) >> 11;
var2 = (((((adc_T>>4) – ((BMP280_S32_t)dig_T1)) * ((adc_T>>4) – ((BMP280_S32_t)dig_T1))) >> 12) *
((BMP280_S32_t)dig_T3)) >> 14;
t_fine = var1 + var2;
T = (t_fine * 5 + 128) >> 8;
return T;
}


// Returns pressure in Pa as unsigned 32 bit integer. Output value of “96386” equals 96386 Pa = 963.86 hPa
BMP280_U32_t bmp280_compensate_P_int32(BMP280_S32_t adc_P)
{
BMP280_S32_t var1, var2;
BMP280_U32_t p;
var1 = (((BMP280_S32_t)t_fine)>>1) – (BMP280_S32_t)64000;
var2 = (((var1>>2) * (var1>>2)) >> 11 ) * ((BMP280_S32_t)dig_P6);
var2 = var2 + ((var1*((BMP280_S32_t)dig_P5))<<1);
var2 = (var2>>2)+(((BMP280_S32_t)dig_P4)<<16);
var1 = (((dig_P3 * (((var1>>2) * (var1>>2)) >> 13 )) >> 3) + ((((BMP280_S32_t)dig_P2) * var1)>>1))>>18;
var1 =((((32768+var1))*((BMP280_S32_t)dig_P1))>>15);
if (var1 == 0)
{
return 0; // avoid exception caused by division by zero
}
p = (((BMP280_U32_t)(((BMP280_S32_t)1048576)-adc_P)-(var2>>12)))*3125;
if (p < 0x80000000)
{
p = (p << 1) / ((BMP280_U32_t)var1);
}
else
{
p = (p / (BMP280_U32_t)var1) * 2;
}
var1 = (((BMP280_S32_t)dig_P9) * ((BMP280_S32_t)(((p>>3) * (p>>3))>>13)))>>12;
var2 = (((BMP280_S32_t)(p>>2)) * ((BMP280_S32_t)dig_P8))>>13;
p = (BMP280_U32_t)((BMP280_S32_t)p + ((var1 + var2 + dig_P7) >> 4));
return p;
}

post edited by Kabak - 2020/10/30 13:17:25
#1

29 Replies Related Threads

    upand_at_them
    Super Member
    • Total Posts : 739
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: online
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 13:17:15 (permalink)
    0
    Do whatever the Arduino library does?
    #2
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 13:19:53 (permalink)
    -1 (1)
    I need to drive that using PIC18
    #3
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 14:05:09 (permalink)
    0
    Arduino lib use 64-bit values
     
    https://github.com/adafru...er/Adafruit_BMP280.cpp
     
     
    #4
    1and0
    Access is Denied
    • Total Posts : 11501
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 14:39:48 (permalink)
    0
    Kabak
    Anybody made conversion of the BMP280 sensor RAW data into real Temperature & Pressure values using PIC18-33 MCU ?
    Datasheet recommend to made calculations using 64-bits data types. They provide the 32-bit code version, but even there data exceed PIC18 int32_t - 0xff ff ff ff value after shifting, so values are incorrect after calculations.
     
    Maybe someone have another formulas to calculate Temperature & Pressure ?

    If their datasheet provides 32-bit code, then I'd think 32-bit math should work. Anyway, XC8 in C99 mode support 64-bit integers at the expense of more resources like memories and cycle time.
    #5
    ric
    Super Member
    • Total Posts : 28942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 15:10:55 (permalink)
    +1 (1)
    That code only uses 32 bit variables.
    Where are you getting an overflow, and what input data triggers it?

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    1and0
    Access is Denied
    • Total Posts : 11501
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 17:54:00 (permalink)
    +1 (1)
    Using the sample values from page 23 of the BMP280 datasheet, I tested the 32-bit functions and they return temperature of 2508 and pressure of 100656, which are pretty good comparing to the datasheet listed value of 2508 and 100653, respectively, calculated with double data type.
     
    #7
    davea
    Super Member
    • Total Posts : 500
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 19:14:43 (permalink)
    0
    are you sure Table 17 dig_T and dig_P
    is setup correctly and the values are reasonable
    and t_fine is global
    as I can't see the 2 year old BOSCH data sheet having a error..
     
    #8
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 23:24:14 (permalink)
    0
    I use C89 XC8 1.33 
     
    I'll try C99 XC8 version 2.10
     
    Thank you
    #9
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 23:30:15 (permalink)
    -1 (1)
    the value is out of range int32_t before the final shift is performed
    var1 = ((((adc_T>>3) – ((BMP280_S32_t)dig_T1<<1))) * ((BMP280_S32_t)dig_T2)) >> 11;

    #10
    ric
    Super Member
    • Total Posts : 28942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 23:38:49 (permalink)
    +1 (1)
    How do you know?
    It's really hard to help when you make statements with no backing reasons.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 23:45:30 (permalink)
    0
    But, I step through values calculations in debugger and compare BMP280 temperature value with Si7021-A20 sensor, even with home thermometer.
    Temperature value from Si7021-A20 sensor is more adequate to reality
     
    BMP280 RAW data seems behave not bad, but if i put all sensors outside the home window, after calculations temperature value measured by BMP280 is completely lie.
    post edited by Kabak - 2020/10/30 23:57:58
    #12
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 23:47:35 (permalink)
    0
    Thank you for information
    Can you point me on correct datasheet and formulas ?
    #13
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/30 23:54:30 (permalink)
    0
    Example  ( values are real ) :  
     
     ((((adc_T>>3) – ((BMP280_S32_t)dig_T1<<1)))  = 0xF6942
    ((BMP280_S32_t)dig_T2)) = 0x6741

    var1 =  ( 0xF6942 *  0x6741 ) >> 11 
    So,    0xF6942 * 0x6741 = 0x6374347C2 - outside bounds int32_t   6 37 43 47 C2


    var1 = ((((adc_T>>3) – ((BMP280_S32_t)dig_T1<<1))) * ((BMP280_S32_t)dig_T2)) >> 11;

    post edited by Kabak - 2020/10/31 00:06:19
    #14
    ric
    Super Member
    • Total Posts : 28942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/31 00:20:07 (permalink)
    0
    How many ways do I have to ask, what raw data did you start with?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #15
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/31 00:30:08 (permalink)
    0
       uint32_t dig_T1 = 0x6E78;
        int32_t dig_T2 = 0x6741;
        int32_t dig_T3 = 0x0032;
        
       uint32_t dig_P1 = 0x951B;
        int32_t dig_P2 = 0xD66E;
        int32_t dig_P3 = 0x0BD0;
        int32_t dig_P4 = 0x2140;
        int32_t dig_P5 = 0xFF2B;
        int32_t dig_P6 = 0xFFF9;
        int32_t dig_P7 = 0x3C8C;
        int32_t dig_P8 = 0xC6F8;
        int32_t dig_P9 = 0x1770;

    int32_t TemperatureRAW = 0x0083A470;
    int32_t PressureRAW = 0x007E9230;

    #16
    davea
    Super Member
    • Total Posts : 500
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/31 11:37:18 (permalink)
    0
    some of your values are way off
    post edited by davea - 2020/10/31 11:54:42

    Attached Image(s)

    #17
    davea
    Super Member
    • Total Posts : 500
    • Reward points : 0
    • Joined: 2016/01/28 13:12:13
    • Location: Tampa Bay FL USA
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/31 12:21:43 (permalink)
    0
    Kabak
       uint32_t dig_T1 = 0x6E78;
        int32_t dig_T2 = 0x6741;
        int32_t dig_T3 = 0x0032;   <<<<<<<<<<<<<< 0xFCxx
        
       uint32_t dig_P1 = 0x951B;
        int32_t dig_P2 = 0xD66E;
        int32_t dig_P3 = 0x0BD0;
        int32_t dig_P4 = 0x2140;  <<<<<<<<<<<<< ??
        int32_t dig_P5 = 0xFF2B;  <<<<<<<<<<<<< 0x00xx
        int32_t dig_P6 = 0xFFF9;
        int32_t dig_P7 = 0x3C8C;
        int32_t dig_P8 = 0xC6F8;
        int32_t dig_P9 = 0x1770;

    int32_t TemperatureRAW = 0x0083A470;   <<<<< 4 bits to large
    int32_t PressureRAW = 0x007E9230;  <<<<< 4 bits to large



    the RAW data is big Indian the XLSB is padded with 4 zeros
    RAW>>=4; 
    post edited by davea - 2020/10/31 13:02:27
    #18
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/31 13:12:59 (permalink)
    0
    Values i have provided was read from BMP280 sensor. ( programed during manufacturing )
     
    Except TemperatureRAW and PressureRAW
    post edited by Kabak - 2020/10/31 13:15:35
    #19
    Kabak
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2014/07/30 03:47:55
    • Location: 0
    • Status: offline
    Re: PIC18 & BMP280 Sensor data calculations 2020/10/31 13:14:24 (permalink)
    +1 (1)
    I should swap bytes order ?
     
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5