BMP180 Code Examples

marks

Senior Member
#1
Digital Pressure Sensor- With Temperature.
Although the ds18b20 is probaly more accurate the Bosch sensor does well and is highly responsive.
For best accuracy a linear supply @ 3.3v is recommended and check calibration yearly 'not likely lol.
The Bureau of Meteorology usually report the mean sea level pressure at 0m.
A mobile phone shows my altitude to be 60m higher than this so my measured pressure will be lower.
The data sheet shows for every 10m corresponds to 1.2hPa pressure change at sea level (7.2hPa)
however this is not accurate.
luckily the internet has data from Hillarys Harbour West of me (I'm sure that huge vessel docked there has westaust55 on its sides...)
and Perth that are recorded every half hour. I decided to match mine with Perth I need too add 4.4hPa
so at the top SYMBOL Calibrate_MSL_hPa = 44 and seems to track their recorded results quite well.
Code:
#picaxe 18m2         'marks 
#no_data
pause 4000
#terminal 4800
SETFREQ M4 
SYMBOL Calibrate_MSL_hPa = 44 ' Adjust to suit your elevation (4.4 hPa) 
SYMBOL index = b0  SYMBOL sign = b0    SYMBOL  Pa = b0
SYMBOL AC5 = W1    SYMBOL   X1 = W1    SYMBOL AC3 = W1    SYMBOL B7. = W1
SYMBOL AC6 = W2    SYMBOL   X2 = W2    SYMBOL B1. = W2    SYMBOL AC4 = W2    SYMBOL B4. = W2
SYMBOL MC  = W3    SYMBOL   X3 = W3    SYMBOL B2. = W3    SYMBOL   R = b6    SYMBOL  rr = b7
SYMBOL MD  = W4    SYMBOL inX2 = W4    SYMBOL B6. = W4
SYMBOL B5. = W5    SYMBOL  UT  = W5    SYMBOL Temp = W5   SYMBOL   T = W5    SYMBOL AC2 = W5    SYMBOL  UP = W5    SYMBOL   P = W5
SYMBOL B3. = W6    SYMBOL  AC1 = W6

HI2CSETUP I2CMASTER, $EE, I2Cslow_4, I2CBYTE    ' set to 100kbps BMP180 @ 3.3v
main:
  Hi2Cin  $B2,(b3,b2)           '       AC5 calibration data from EEPROM
  Hi2Cin  $B4,(b5,b4)           '       AC6 calibration data from EEPROM
  Hi2Cin  $BE,(b9,b8)           '       MD  calibration data from EEPROM
  Hi2Cout $F4,($2E)             '       UT start conversion
    pause 100                   '       wait 4.5ms
  Hi2Cin  $F6,(B11,b10)         '       UT 16 bit data
  'sertxd ("  UT=",#UT)
  pause 20
  ' ac5=32757
  ' ac6=23153
  ' md=2868   
  ' ut=27898 
 X1 = UT -AC6 *2 **AC5          
 X2 =  X1 +MD 
    inX2 = $FFFF /X2 : Temp = $FFFF //X2 +1 
     rr = X2 //10 : X2 = X2 /10
      FOR index = 1 TO 3
       inX2 = inX2 *10 : inX2 = Temp / X2 +inX2
       r = inX2 //10 *rr
       Temp = Temp //X2 *10 -r
      NEXT index

 Hi2Cin  $BC,(b7,b6)             '      MC calibration data from EEPROM
 pause 20
 ' mc=56825 ' -8711  
 MC = -MC *3 **44740
 X2 = inX2 **MC 
  sertxd("  Temperature ")
  B5. = X1 -X2 : sign=" " : IF B5. >32768 THEN : B5. = -B5. : sign="-" : ENDIF
  T = B5. /16 : r = T //10 : T = T /10 : sertxd(sign,#T,".",#r,"'C " )
  
  B5. = X1 -X2 +8800 **7373 -670 : sign=" " : IF B5. >32768 THEN : B5. = -B5. : sign="-" : ENDIF
  r = T //10 : T = T /10 : sertxd(sign,#T,".",#r,"'F"   )
  
 Hi2Cin  $AA,(b13,b12)           '       AC1 calibration data from EEPROM
 Hi2Cin  $AC,(b11,b10)           '       AC2 calibration data from EEPROM
 Hi2Cin  $B8,(b7,b6)             '       B2. calibration data from EEPROM
 pause 20
 ' ac1=408
 ' ac2=65464 ' -72
 ' b2.=4
 B6. = X1 -X2 -4000 : sign="+" : IF B6. > 32768 THEN : B6. = -B6. : sign="-" : ENDIF 
 B6. = B6. *4
 X1  = B2. *80 **B6. **B6.          
 X2  = -AC2 *20 **B6. +X1
 X2  =  X2 /10 : IF sign="+" THEN : X2 = - X2 : ENDIF
 B3. =  AC1 +X2
																
 Hi2Cin  $AE,(b3,b2)            '      AC3 calibration data from EEPROM
 Hi2Cin  $B6,(b5,b4)            '      B1. calibration data from EEPROM
 pause 20
 ' ac3=51153 ' -14383
 ' b1.=6190
 X1 = -AC3 *3 **54614 **B6. *2 
 X2 =  B1. *3 **54614 **B6. **B6. +X1
 X3 =  X2 /10 : IF sign="+" THEN : X3 = -X3 -2 : ENDIF 

 Hi2Cin  $B0,(b5,b4)            '     AC4. calibration data from EEPROM 
 pause 20
 ' ac4=32741
  B4.= AC4 +X3 
    inX2 = $FFFF /X2 : Temp = $FFFF //X2 +1 
     rr = X2 //10 : X2 = X2 /10
      FOR index = 1 TO 4
       inX2 = inX2 *10 : inX2 = Temp /X2 +inX2
       r = inX2 //10 *rr
       Temp = Temp //X2 *10 -r 
     NEXT index                                     

 Hi2Cout $F4,($34)             '     UP start conversion 
   pause 100                   '     wait 4.5ms
 Hi2Cin  $F6,(B11,B10)         '     UP 16 bit data
 ' sertxd("   UP=",#UP) 
 pause 20
 ' up=23843
B7. = UP -B3.                   
P = inX2 **X1 *5                                
P = inX2 **5 +P                                
X1 = P **P **6076                               
X2 = 7357 **P                                    
X3 = 1896 +X1 -X2 : sign="+" :IF X3 > 32768 THEN : X3 = -X3 : sign="-" : ENDIF                                                
X3 = X3 /16 
IF sign="+" THEN : P = P + X3 /5 : ELSE P = P -X3 /5 : ENDIF :p = p +Calibrate_MSL_hPa
Pa = P //10 : P = P /10
sertxd("  Pressure ",#P,".",#Pa," hPa",cr,lf)
pause 4000
goto main
 
Top