News:

;) This forum is the property of Proton software developers

Main Menu

BMP280 pressure, temperature & altitude example code .

Started by Bob (G8GFA), Feb 01, 2021, 11:42 AM

Previous topic - Next topic

Bob (G8GFA)

Example code for the BMP280



'****************************************************************
'*  Name    : BMP280.BAS                                        *
'*  Author  : Bob Marshall                                      *
'*  Date    : 08/07/2016                                        *
'*  Version : 1.1                                               *
'*  Notes   : This program uses a BMP280 pressure sensor        *
'*          : to measure current pressure, temperature &        *
'*          : altitude.                                         *
'****************************************************************


  Include "Amicus18_mk2.inc"

  Declare Float_Display_Type = Fast
  Declare Hbus_Bitrate 400

  Symbol BMP280 = $ec  'BMP280 I2C address
  Symbol CalDatStAd = $88 'Start address for cal. data.
  Symbol BMP280ContReg = $F4 'BMP280 control Register
  Symbol BMP280ConfigReg = $F5 'BMP280 configuration register
  Symbol BMP280DataReg = $F7 'Uncompensated Data start register

'Values of 0-3 acceptable but see data sheet. 3 = Normal Mode
  Symbol PowerMode = 3

'Values of 0-5 acceptable but see data sheet. 5 = 20 bit res.
  Symbol TempRes =  5

'Values of 0-5 acceptable but see data sheet. 5 = 20 bit res.
  Symbol PresRes = 5

'Values of 0-7 acceptable. See data sheet before changing.
  Symbol Filter = 4

'Values of 0-7 acceptable but see data sheet. 4 = 500ms.
  Symbol Standby =  4


'Temperature related variables.
  Dim T1 As Word
  Dim T2 As SWord
  Dim T3 As SWord

  Dim UT As Word  'Uncompensated temperature
  Dim XLSB_t As Byte
  Dim UTLong As SDword
  Dim Temperature As Float
  Dim T_fine As Float

'Pressure related variables
  Dim P1 As Word
  Dim P2 As SWord
  Dim P3 As SWord
  Dim P4 As SWord
  Dim P5 As SWord
  Dim P6 As SWord
  Dim P7 As SWord
  Dim P8 As SWord
  Dim P9 As SWord

  Dim UP As Word 'Uncompensated pressure.
  Dim UPLong As SDword
  Dim XLSB_p As Byte
  Dim P As Float
 
 
'BMP280 related variables.
  Dim BMP280Config As Byte
  Dim BMP280Control As Byte

'General variables
  Dim var1 As Float
  Dim var2 As Float
  Dim Altitude As Float


  Main:

  DelayMS 1000 'Let things settle down.
  Clear

  'Setup BMP280 Control and configuration registers.
  BMP280Config = (Standby <<5) + (Filter <<2) ' Combine configuration bits
  HBusOut BMP280,BMP280ConfigReg,[BMP280Config]
 
  BMP280Control = (TempRes <<5) + (PresRes <<2) + PowerMode ' Combine BMP280Control bits.
  HBusOut BMP280,BMP280ContReg,[BMP280Control]


' Read calibration data
  HBusIn BMP280,CalDatStAd,[T1,T2,T3,P1,P2,P3,P4,P5,P6,P7,P8,P9]

  Swap T1.Byte0,T1.Byte1 : Swap T2.Byte0,T2.Byte1 : Swap T3.Byte0,T3.Byte1
  Swap P1.Byte0,P1.Byte1 : Swap P2.Byte0,P2.Byte1 : Swap P3.Byte0,P3.Byte1
  Swap P4.Byte0,P4.Byte1 : Swap P5.Byte0,P5.Byte1 : Swap P6.Byte0,P6.Byte1
  Swap P7.Byte0,P7.Byte1 : Swap P8.Byte0,P8.Byte1 : Swap P9.Byte0,P9.Byte1


  While     
'Read the uncompensated temperature and pressure values.     
      HBusIn BMP280,BMP280DataReg,[UP,XLSB_p,UT,XLSB_t]
      UTLong = UT * 16 + (XLSB_t >> 4)
      UPLong = UP * 16 + (XLSB_p >> 4)
     
' Test data from the Bosch datasheet. Used to check calculations.
'         T1=27504  : T2=26435  : T3=-1000
'         P1=36477  : P2=-10685 : P3=3024
'         P4=2855   : P5=140    : P6=-7
'         P7=15500  : P8=-14600 : P9=6000
'         UTlong=519888 : UPlong=415148
' Expected results: Temperature = 25.08, Pressure =  100656.26

      var1=(UTLong/16384-T1/1024)*T2
      var2=(UTLong/131072-T1/8192)*(UTLong/131072-T1/8192)*T3
      Temperature=(var1+var2)/5120
      T_fine=(var1+var2)


' $B0 is the hex value for the degree symbol. Not correctly displayed on some terminals.
      HSerOut ["Current temperature is: ",Dec2 Temperature,$B0,"C",13]

' Pressure calculations start here.

        var1=T_fine/2-64000
        var2=var1*var1*P6/32768
        var2=var2+var1*P5 * 2
        var2=(var2/4)+ P4 * 65536
        var1=(P3*var1*var1/524288+P2*var1)/524288
        var1=(1+var1/32768)*P1
        P=1048576-UPLong
        P=(P-var2/4096)*6250/var1
       
' The *-1 is needed here to negate the value of var1
' See p23 of the Bosch data sheet (example calculation).       
        var1=(P9*P*P/2147483648)*-1
       
        var2=P*P8/32768
        P=P+(var1+var2+P7)/16

        HSerOut ["Current pressure: ",Dec2 P,"Pa",13]


' Note: The altitude calculation takes about 1.5k of program memory.
'     : Remove if not needed!

' Altitude = 44330*((1-(p/101325)) ^ (1/5.255))
' 101325 represents the 'standard' pressure at sea level. Adjust to suit.

      var1 = Pow((P/101000),0.190295)
      Altitude =  44330 *(1-var1)
      HSerOut ["Current altitude: ",Dec2 Altitude ,"m",13,10,13]

      DelayMS 2000

  Wend

' End of program