Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:58 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : BMP280 Baro Sensor

Author Message
JackRabbit
Newbie

Joined: 21/07/2018
Location: United States
Posts: 14
Posted: 01:12am 20 Jan 2019
Copy link to clipboard 
Print this post

Does anyone have code to read and convert the Bosch BMP280 Baro and Temp sensor output? I did search and found some circuit discussion but didn't find any code.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 01:57am 20 Jan 2019
Copy link to clipboard 
Print this post

I think the BMP280 can use the same code as the BMP180 which can use the same code as the BMP085
I am using my BMP085 code for a BMP180 on the MMEdit test server without issues.

This assumes you use I2C and set the address jumper to suit.

You should be able to find plenty of BMP085 code on the forum.

I can post my code if required.

Jim
VK7JH
MMedit
 
JackRabbit
Newbie

Joined: 21/07/2018
Location: United States
Posts: 14
Posted: 02:54am 20 Jan 2019
Copy link to clipboard 
Print this post

I looked at the BMP180 datasheet and unfortunately it appears the BMP280 interface is somewhat different, especially in the value correction/conversion math.
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 1003
Posted: 05:54am 20 Jan 2019
Copy link to clipboard 
Print this post

The code below tests for both a BMP280 and BME280 on either of the I2C addresses used. It is based on code on the forum here somewhere (Lewis weather station maybe)
If you ignore the humidity part it should give you a starting point.



  Quote  
option explicit
option default none
'
' BME280 routines and test harness
'
DIM INTEGER BME280_ADDRESS = &H76
const BME280_REGISTER_RESET = &HE0
const BME280_REGISTER_T1 = &H88
const BME280_REGISTER_P1 = &H8E
const BME280_REGISTER_H1 = &HA1
const BME280_REGISTER_H2 = &HE1
const BME280_REGISTER_CHIPID = &HD0
const BME280_REGISTER_CONTROLHUMID = &HF2
const BME280_REGISTER_CONTROL = &HF4
const BME280_REGISTER_PRESSUREDATA = &HF7
const BME280_REGISTER_TEMPDATA = &HFA
const BME280_REGISTER_HUMIDDATA = &HFD
'
DIM INTEGER s16=&HFFFFFFFFFFFF0000 , s16b=&H8000
DIM INTEGER s12=&HFFFFFFFFFFFFF000 , s12b=&H800
DIM INTEGER s8= &HFFFFFFFFFFFFFF00 , s8b=&H80
'
DIM INTEGER T1,T2,T3 'uint16_t, int16_t, int16_t
DIM INTEGER P1,P2,P3,P4,P5,P6,P7,P8,P9 'uint16_t, 8 x int16_t
DIM INTEGER H1,H2,H3,H4,H5,H6 'uint8_t, int16_t , uint8_t, int16_t, int16_t, int8_t
'
DIM INTEGER t_fine 'used to store accurate temp reading from temp conversion for use in pressure and humidity conversions
DIM FLOAT mypress


'
' Test program
'
bme280_init
do
print bme280_read_temp() 'must be run before pressure or humidity
mypress= bme280_read_pressure()
? mypress
? myAlt(mypress,
1013.25)
'http://www.bom.gov.au/nsw/observations/sydney.shtml
print bme280_read_humidity()
pause 2000
loop
'
end
'
'***************************************************************************************************
'
function bme280_read_temp() as FLOAT
local INTEGER var1,var2,adc_T
local adc%(2)
i2c write BME280_ADDRESS,1,1,BME280_REGISTER_TEMPDATA
i2c read BME280_ADDRESS,0,3,adc%()
adc_T=((adc%(
0)<<16) OR (adc%(1)<<8) OR adc%(2))>>4
var1 = ((((adc_T>>
3) - (T1 <<1))) * T2) \ q(11)
var2 = (((((adc_T>>
4) - (T1)) * ((adc_T\ q(4)) - (T1))) \ q(12)) * (T3)) \ q(14)
t_fine = var1 + var2
bme280_read_temp = ((t_fine *
5 + 128) \ q(8))/100.0
end function

function bme280_read_pressure() as FLOAT
local INTEGER var1, var2, adc_P, p
local adc%(2)
i2c write BME280_ADDRESS,1,1,BME280_REGISTER_PRESSUREDATA
i2c read BME280_ADDRESS,0,3,adc%()
adc_P=((adc%(
0)<<16) OR (adc%(1)<<8) OR adc%(2))>>4
var1 = t_fine -
128000
var2 = var1 * var1 * P6
var2 = var2 + ((var1 * P5)<<
17)
var2 = var2 + (P4 <<
35)
var1 = ((var1 * var1 * P3)\ q(
8)) + ((var1 * P2)<<12)
var1 = ((
1<<47)+var1)*P1\ q(33)
if var1 = 0 THEN
bme280_read_pressure =
0' avoid exception caused by division by zero
exit function
endif
p =
1048576 - adc_P
p = (((p<<
31) - var2)*3125) \ var1
var1 = (P9 * (p\ q(
13)) * (p\ q(13))) \ q(25)
var2 = (P8 * p) \ q(
19)
p = ((p + var1 + var2) \ q(
8)) + (P7<<4)
bme280_read_pressure = p/
25600.0
end function
'
function bme280_read_humidity() as FLOAT
local INTEGER v_x1,adc_H
local adc%(1)
i2c write BME280_ADDRESS,1,1,BME280_REGISTER_HUMIDDATA
i2c read BME280_ADDRESS,0,2,adc%()
adc_H=(adc%(
0)<<8) OR adc%(1)
v_x1 = t_fine -
76800
v_x1=(((((adc_H<<
14)-((H4)<<20)-(H5*v_x1))+16384)\ q(15))*(((((((v_x1*H6)\ q(10))*(((v_x1*H3)\ q(11))+32768))\ q(10))+2097152)*H2+8192)\ q(14)))
v_x1 = (v_x1 - (((((v_x1 \ q(
15)) * (v_x1 \ q(15))) \ q(7)) * (H1)) \ q(4)))
if v_x1< 0 THEN v_x1 = 0
if v_x1 > 419430400 THEN v_x1= 419430400
bme280_read_humidity = (v_x1\ q(
12)) / 1024.0
end function

sub bme280_init
local i%,cal%(17)
ON ERROR SKIP
i2c open 400,1000 '400KHz bus speed
ON ERROR CLEAR

i2c write BME280_ADDRESS,1,1,BME280_REGISTER_CHIPID
i2c read BME280_ADDRESS,0,1,i%
if i%=&H60 THEN
print "BME280 found at address &H" ,HEX$(BME280_ADDRESS)," with ID &H",HEX$(i%)
else if i%=&H58 THEN
print "BMP280 found at address &H" ,HEX$(BME280_ADDRESS)," with ID &H",HEX$(i%)
else
BME280_ADDRESS=BME280_ADDRESS+
1
i2c write BME280_ADDRESS,1,1,BME280_REGISTER_CHIPID
i2c read BME280_ADDRESS,0,1,i%
if i%=&H60 THEN
print "BME280 found at address &H" ,HEX$(BME280_ADDRESS)," with ID &H",HEX$(i%)
else if i%=&H58 THEN
print "BMP280 found at address &H" ,HEX$(BME280_ADDRESS)," with ID &H",HEX$(i%)
else
print "Neither BMP280 or BME280 found - ID found is &H" ,HEX$(i%,2)
endif

endif
'
i2c write BME280_ADDRESS,1,1,BME280_REGISTER_T1
i2c read BME280_ADDRESS,0,6,cal%()
T1=cal%(
0) OR (cal%(1)<< 8)
T2=cal%(
2) OR (cal%(3)<< 8): if T2 and s16b THEN T2=T2 OR s16 'sign extend if required
T3=cal%(4) OR (cal%(5)<< 8): if T3 and s16b THEN T3=T3 OR s16
'
i2c write BME280_ADDRESS,1,1,BME280_REGISTER_P1
i2c read BME280_ADDRESS,0,18,cal%()
P1=cal%(
0) OR (cal%(1)<<8)
P2=cal%(
2) OR (cal%(3)<<8): if P2 and s16b THEN P2=P2 OR s16 'sign extend if required
P3=cal%(4) OR (cal%(5)<<8): if P3 and s16b THEN P3=P3 OR s16
P4=cal%(
6) OR (cal%(7)<<8): if P4 and s16b THEN P4=P4 OR s16
P5=cal%(
8) OR (cal%(9)<<8): if P5 and s16b THEN P5=P5 OR s16
P6=cal%(
10) OR (cal%(11)<<8): if P6 and s16b THEN P6=P6 OR s16
P7=cal%(
12) OR (cal%(13)<<8): if P7 and s16b THEN P7=P7 OR s16
P8=cal%(
14) OR (cal%(15)<<8): if P8 and s16b THEN P8=P8 OR s16
P9=cal%(
16) OR (cal%(17)<<8): if P9 and s16b THEN P9=P9 OR s16
'
i2c write BME280_ADDRESS,1,1,BME280_REGISTER_H1
i2c read BME280_ADDRESS,0,1,H1
i2c write BME280_ADDRESS,1,1,BME280_REGISTER_H2
i2c read BME280_ADDRESS,0,7,cal%()
H2=cal%(
0) OR (cal%(1)<< 8): if H2 and s16b THEN H2=H2 OR s16 'sign extend if required
H3=cal%(2)
H6=cal%(
6): if H6 and s8b THEN H6=H6 OR s8 'sign extend if required
H4=(cal%(3)<<4) OR (cal%(4) and &H0F): if H4 and s12b THEN H4=H4 OR s12 'sign extend if required
H5=(cal%(5)<<4) OR (cal%(4)>>4): if H5 and s12b THEN H5=H5 OR s12
'
i2c write BME280_ADDRESS,0,2,BME280_REGISTER_CONTROLHUMID,&H05 '16x oversampling humidity
i2c write BME280_ADDRESS,0,2,BME280_REGISTER_CONTROL,&HB7 '16x oversampling pressure/temp, normal mode
'
end sub
'
function q(x as INTEGER) as INTEGER 'returns 2 raised to the power
q=(1<<x)
end function
function myAlt(p as FLOAT, p0 as FLOAT )as FLOAT
' given local pressure (p) and pressure at sea level (p0)
' returns altitude in metres
myAlt = 44330*(1-(p/p0)^0.1903)
end function



Original code for BME280 here
Edited by disco4now 2019-01-21
Latest F4 Latest H7 FotS
 
JackRabbit
Newbie

Joined: 21/07/2018
Location: United States
Posts: 14
Posted: 06:53pm 20 Jan 2019
Copy link to clipboard 
Print this post

Thank you! That will help a lot :)
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025