Администрация форума не несёт ответственности за достоверность информации и оставляет за собой право редактировать или в особых случаях даже удалять посты без предупреждения. Спасибо за понимание.

Программирование ATMEL в BASCOM.

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.


Вы здесь » Программирование ATMEL в BASCOM. » Вопросы - ответы » Осваиваем датчик BME280


Осваиваем датчик BME280

Сообщений 31 страница 35 из 35

31

Наконец-то дошли руки до BME280...

Только что-то окромя температуры он ничего не отдает... ;)
Давление и влажность - "0"...
При тесте датчик отдает положенные &H60...

Использую (с адаптацией) работу с датчиком вот из этого кода:

Код:
'*******************************************************************************
'
'                            SSD1289 LCD Test
'
'                            resolution 240x320
'
'*******************************************************************************
$regfile = "usb1286.dat"
$crystal = 8000000
$hwstack = 200
$swstack = 200
$framesize = 200
Config Clockdiv = 1
Mcucr.7 = 1                                                 'Turn OFF JTAG
Mcucr.7 = 1

'--------------------------- Display Configuration -----------------------------
Config Porta = Output : Data_disp_high Alias Porta          'DB8 - DB15
Config Portc = Output : Data_disp_low Alias Portc           'DB0 - DB7
Rs_disp Alias Porte.0 : Config Rs_disp = Output             'Command/Data pin
Wr_disp Alias Porte.1 : Config Wr_disp = Output             'Write pin
Res_disp Alias Portf.6 : Config Res_disp = Output : Res_disp = 1       'Reset pin
Cs_disp Alias Portf.7 : Config Cs_disp = Output : Cs_disp = 0       'Chip Select
Const Portrait = 0                                          '0=Landscape, 1=Portrait
Const Rotate180 = 0                                         '0=Normal, 1=Rotate 180°
Config Submode = New
'$include "SSD1289 16bit library.inc" 'Include library
$include "Ili9325 16bit Library.inc"                        'Include library

'=============================== TWI (I2C) config ==============================
$lib "i2c_twi.lbx"
Config Sda = Portd.1                                        ' I2C Data
Config Scl = Portd.0                                        ' I2C Clock
Config Twi = 400000
Const Slave = &HEC                                          ' I2C Address (5pin=0)
I2cinit

Dim Temp(24) As Byte , Text As String * 16
Dim Temperature As Single , Humidity As Single , Pressure As Single
'================================ Test Program =================================

Display_init                                                ' Initialize Display
Lcd_clear White
Restore Font25x32
Lcd_text "BME280" , 85 , 5 , Blue , White
Restore Font12x16
Lcd_text "DIGITAL HUMIDITY, PRESSURE" , 4 , 45 , Darkred , White
Lcd_text "AND TEMPERATURE SENSOR" , 28 , 65 , Darkred , White


'--------------- Test BME280 exist
Temp(1) = &HD0                                              ' ID register address
I2creceive Slave , Temp(1) , 1 , 1
If Temp(1) = &H60 Then                                      ' ID register must return $60
   Lcd_text "Sensor found" , 88 , 120 , Darkgreen , White
Else
   Lcd_text "Sensor not found" , 64 , 120 , Red , White
   Goto Not_exist
End If

Gosub Bme280_init                                           ' BME280 Initialization

Do
   Gosub Bme280_read
   Text = Fusing(pressure , "#.##") + " mm"
   Lcd_text "   Pressure = " + Text , 1 , 170 , Black , White
   Text = Fusing(temperature , "#.##") + " C"
   Lcd_text "Temperature = " + Text , 1 , 190 , Black , White
   Text = Fusing(humidity , "#.##") + " %"
   Lcd_text "   Humidity = " + Text , 1 , 210 , Black , White
   Wait 1
Loop
Not_exist:
End                                                         'end program
'-------------------------------------------------------------------------------
$include "Font12x16.font"
$include "Font25x32.font"



'============================= BME280 subroutines ==============================

'------------------------------ Initialization ---------------------------------
Bme280_init:
Dim T1 As Word , T2 As Integer , T3 As Integer
Dim P1 As Word , P2 As Integer , P3 As Integer
Dim P4 As Integer , P5 As Integer , P6 As Integer
Dim P7 As Integer , P8 As Integer , P9 As Integer
Dim H1 As Word , H2 As Integer , H3 As Word
Dim H4 As Integer , H5 As Integer , H6 As Integer
Dim Dtemp As Dword , Dpress As Dword , Dhum As Dword
Dim Temp1 As Long , Temp2 As Long , Temp3 As Long
Dim Press As Dword , T_mem As Long

Temp(1) = &HF2 : Temp(2) = &H05                             ' Humidity oversampling x16
I2csend Slave , Temp(1) , 2
Temp(1) = &HF4 : Temp(2) = &HB7                             ' Temperature oversampling x16, Pressure oversampling x16, Normal mode
I2csend Slave , Temp(1) , 2
Temp(1) = &HF5 : Temp(2) = &H70                             ' Standby 250ms, Filter x16
I2csend Slave , Temp(1) , 2
Temp(1) = &H88                                              ' Read Temperature and Pressure calibration
I2creceive Slave , Temp(1) , 1 , 24
T1 = Makeint(temp(1) , Temp(2))
T2 = Makeint(temp(3) , Temp(4))
T3 = Makeint(temp(5) , Temp(6))
P1 = Makeint(temp(7) , Temp(8))
P2 = Makeint(temp(9) , Temp(10))
P3 = Makeint(temp(11) , Temp(12))
P4 = Makeint(temp(13) , Temp(14))
P5 = Makeint(temp(15) , Temp(16))
P6 = Makeint(temp(17) , Temp(18))
P7 = Makeint(temp(19) , Temp(20))
P8 = Makeint(temp(21) , Temp(22))
P9 = Makeint(temp(23) , Temp(24))

Temp(1) = &HA1                                              ' Read Humidity calibration
I2creceive Slave , Temp(1) , 1 , 1
H1 = Temp(1)
Temp(1) = &HE1
I2creceive Slave , Temp(1) , 1 , 8
H2 = Makeint(temp(1) , Temp(2))
H3 = Temp(3)
H4 = Temp(4) : Shift H4 , Left , 4
H5 = Temp(5) And &H0F : H4 = H4 + H5
H5 = Temp(5) And &HF0 : Shift H5 , Right , 4
H6 = Temp(6) : Shift H6 , Left , 4 : H5 = H5 + H6
H6 = Temp(7)
Return


'---------------------- Read data and calculate results ------------------------
Bme280_read:
Temp(1) = &HF7                                              ' Data registers address
I2creceive Slave , Temp(1) , 1 , 8
Dpress = Temp(1) : Shift Dpress , Left , 8
Dpress = Dpress + Temp(2) : Shift Dpress , Left , 8
Dpress = Dpress + Temp(3) : Shift Dpress , Right , 4
Dtemp = Temp(4) : Shift Dtemp , Left , 8
Dtemp = Dtemp + Temp(5) : Shift Dtemp , Left , 8
Dtemp = Dtemp + Temp(6) : Shift Dtemp , Right , 4
Dhum = Temp(7) : Shift Dhum , Left , 8
Dhum = Dhum + Temp(8)

'-------------------------- Temperature
Shift Dtemp , Right , 3 : Temp1 = Dtemp - T1 : Temp1 = Temp1 - T1
Temp1 = Temp1 * T2 : Shift Temp1 , Right , 11 , Signed
Shift Dtemp , Right , 1 : Temp2 = Dtemp - T1
Temp2 = Temp2 * Temp2 : Shift Temp2 , Right , 12 , Signed
Temp2 = Temp2 * T3 : Shift Temp2 , Right , 14 , Signed
T_mem = Temp1 + Temp2 : Temp2 = T_mem * 5
Temp2 = Temp2 + 128 : Shift Temp2 , Right , 8 , Signed
Temperature = Temp2 / 100

'-------------------------- Pressure
Temp1 = T_mem : Shift Temp1 , Right , 1 , Signed
Temp1 = Temp1 - 64000
Temp2 = Temp1 : Shift Temp2 , Right , 2 , Signed
Temp2 = Temp2 * Temp2 : Shift Temp2 , Right , 11 , Signed
Temp2 = Temp2 * P6
Temp3 = Temp1 * P5 : Shift Temp3 , Left , 1
Temp2 = Temp2 + Temp3
Temp3 = P4 : Shift Temp3 , Left , 16
Shift Temp2 , Right , 2 , Signed
Temp2 = Temp2 + Temp3
Temp3 = Temp1 * P2 : Shift Temp3 , Right , 1 , Signed
Shift Temp1 , Right , 2 , Signed
Temp1 = Temp1 * Temp1 : Shift Temp1 , Right , 13
Temp1 = Temp1 * P3 : Shift Temp1 , Right , 3 , Signed
Temp1 = Temp1 + Temp3 : Shift Temp1 , Right , 18 , Signed
Temp1 = Temp1 + 32768
Temp1 = Temp1 * P1 : Shift Temp1 , Right , 15 , Signed
If Temp1 = 0 Then Goto Press_out                            'To avoid zero division
Press = 1048576 - Dpress : Shift Temp2 , Right , 12 , Signed
Press = Press - Temp2 : Press = Press * 3125
Press = Press / Temp1 : Press = Press + Press
Temp1 = Press : Shift Temp1 , Right , 3
Temp1 = Temp1 * Temp1 : Shift Temp1 , Right , 13
Temp1 = Temp1 * P9 : Shift Temp1 , Right , 12 , Signed
Temp2 = Press : Shift Temp2 , Right , 2
Temp2 = P8 * Temp2 : Shift Temp2 , Right , 13 , Signed
Temp1 = Temp1 + Temp2 : Temp1 = Temp1 + P7
Shift Temp1 , Right , 4 , Signed
Press = Press + Temp1
Press_out:
Pressure = Press * 0.0075006

'-------------------------- Humidity
Temp1 = T_mem - 76800
Temp2 = Dhum : Shift Temp2 , Left , 14
Temp3 = H4 : Shift Temp3 , Left , 20
Temp2 = Temp2 - Temp3 : Temp3 = Temp1 * H5
Temp2 = Temp2 - Temp3 : Temp2 = Temp2 + &H4000
Shift Temp2 , Right , 15 , Signed
Temp3 = Temp1 * H6 : Shift Temp3 , Right , 10 , Signed
T_mem = Temp1 * H3 : Shift T_mem , Right , 11 , Signed
T_mem = T_mem + &H8000 : Temp3 = Temp3 * T_mem
Shift Temp3 , Right , 10 , Signed
Temp3 = Temp3 + &H200000
Temp3 = Temp3 * H2 : Temp3 = Temp3 + &H2000
Shift Temp3 , Right , 14 , Signed
Temp1 = Temp2 * Temp3
Temp3 = Temp1 : Shift Temp3 , Right , 15 , Signed
Temp3 = Temp3 * Temp3 : Shift Temp3 , Right , 7 , Signed
Temp3 = Temp3 * H1 : Shift Temp3 , Right , 4 , Signed
Temp1 = Temp1 - Temp3
If Temp1 < 0 Then Temp1 = 0
If Temp1 > &H19000000 Then Temp1 = &H19000000
Shift Temp1 , Right , 15 , Signed
Humidity = Temp1 / 128
Return

0

32

Всем заранее спасибо, вопрос решен ! ;)
Все работает.
Наступил на грабли - несоответствие типов...  :blush:

0

33

Привестивую! А кто может сказать возникла малость загвоздка сейчас попробовал код из 1 поста. Что BPM что BME выдают страшно низкую ниже -125 температуре и низкое давление. Притом давление при изменении температуры меняется однзначно работает как то.
Вот может быть проблема с оверлеем? Микрик tiny84 может с переменными косяк чёт куда то залезает? Микрик был выбран из-за размеров этот. Половина занята под передачу данных программый модулятор, на остальной работа с датчиком особо не разгуляешься тот код достаточно компактный. Может косяк с I2C баскомовским есть идеи? Влажность сразу убрал, пробовал разные конфики результат одинковый. BMP280 на плате BME280 подключал модуль. Ладно бы вообще не работало! Может нехватка памяти какая то а компилятор это не обнаруживает но не шибко умный ведь.
Кто сталкивался с левыми показаниями? В расчёте давления и тепературы не чего не менял только убрал перевод в мм ртутного столба в любом случае мм ртутного столба мне не надо.
И на сколько я понимаю калибровочные данные надо 1 раз загружать?

Отредактировано RadioHAM-433 (2020-02-10 22:56:53)

0

34

Эх и мутный датчик  :canthearyou:
А есть что нибудь по проще? Просто тут капец сколько переменных и данных. Неужели всё так сложно?

0

35

У меня валяется вот такая библиотечка.
Правда, она заодно обслуживает акселерометр-гироскоп GY-91 (писалась для мозгов ракеты), но можно просто выкинуть лишний код и оставить только BMP-280. И да, датчик с влажностью (BME-280) она не поддерживает.

Код:
$nocompile

'  ========================================================================================================================
'  -- GY91 & BMP280 MODULE --
'  ========================================================================================================================

const GY91_ChipID = &h73                                    ' GY91 Chip ID
const BMP280_ChipID = &h58                                  ' BMP280 Chip ID

const calib_count = 2000                                    ' count of samples for calibrating measure biases
const dt = 0.00015                                          ' time interval beetwen measurements
const k = 0.98 : Const k1 = 1 - K                           ' coeff for mux accel and gyro filter

' -- raw accelerometer data --
dim a_x as integer
dim a_y as integer
dim a_z as integer
dim a_rx(2) as byte at a_x overlay                          ' both bytes of array will be auto-mapped to a_*,
dim a_ry(2) as byte at a_y overlay                          ' because array placed on same place that a_*
dim a_rz(2) as byte at a_z overlay
' -- measurement biases --
dim a_bx as long
dim a_by as long
dim a_bz as long
' -- single values for internal calculations --
dim a_sx as single
dim a_sy as single
dim a_sz as single
' -- calculated aceel angles in degrees --
dim a_ax as single
dim a_ay as single
dim a_az as single

' -- GYRO VALUES --
' -- raw gyro data --
dim w_x as integer
dim w_y as integer
dim w_z as integer
dim w_rx(2) as byte at w_x overlay
dim w_ry(2) as byte at w_y overlay
dim w_rz(2) as byte at w_z overlay
' -- measurement biases --
dim w_bx as long
dim w_by as long
dim w_bz as long
' -- single gyro values for internal calculations in degrees per measure --
dim w_sx as single
dim w_sy as single
dim w_sz as single

' -- ANGLE VALUES --
' -- calculated single euler angles --
dim e_sx as single
dim e_sy as single
dim e_sz as single
dim Ex as single , Ey as single , Ez as single              ' output copy of angles

' -- BMP280 sensor --
dim BMP280_temp(24) as byte                                 ' Text As String * 16
dim curr_temp as single , smth_temp as single               ' used for smoothing
smth_temp = 20                                              ' start temperature
dim Temperature as single                                   ' global returned value

dim T1 as word , T2 as integer , T3 as integer
dim P1 as word , P2 as integer , P3 as integer
dim P4 as integer , P5 as integer , P6 as integer
dim P7 as integer , P8 as integer , P9 as integer
dim Dtemp as dword , Dpress as dword
dim Temp1 as long , Temp2 as long , Temp3 as long
dim Press as dword , T_mem as long
dim curr_press as single , smth_press as single             ' used for smoothing
smth_press = 760
dim Pressure as single                                      ' global returned value

'  -- GY91 REGISTERS --
const GY91_readreg = &HD1
const GY91_writereg = &HD0

' -- BMP280 REGISTERS ---
const BMP280_readreg = &HEC

' -- GY91 ACCEL SENSITIVITY --
'Each  16-bit  accelerometer  measurement  has  a  full  scale  defined  in
'ACCEL_FS (Register  28).  For each full scale setting, the
'accelerometers’ sensitivity per LSB is:
'0    ±2g   16384 LSB/g
'1    ±4g   8192 LSB/g
'2    ±8g   4096 LSB/g
'3    ±16g  2048 LSB/g
const Mpu650_asens_2g = 16384
const Mpu650_asens_4g = 8192
const Mpu650_asens_8g = 4096
const Mpu650_asens_16g = 2048

' -- SUBS DECLARATIONS --
declare sub i2c_write(byval addr as byte , byval reg as byte , byval value as byte)
declare sub GY91_Init()
declare function GY91_GetChipID() as byte
declare sub GY91_Calibrate()
declare sub GY91_GetAccel()
declare sub GY91_GetGyro()
declare sub GY91_Process()
declare function BMP280_GetChipID() as byte
declare sub BMP280_Init()
declare sub BMP280_Read()

' ******************************************************************************
'    SUBROUTINES
' ******************************************************************************

sub i2c_write(addr , reg , value)
   i2cstart
   i2cwbyte addr
   i2cwbyte reg
   i2cwbyte value
   i2cstop
end Sub

Sub GY91_Init()
   call I2c_write(GY91_writereg , 25 , &B00000000)          ' Divider set to 1   (soll)
   call I2c_write(GY91_writereg , 26 , &B00000011)          ' Bits 0..2 = 011 (3) - ACC:44Hz, 4.9ms; Gyro:42Hz, 4.8ms
   call I2c_write(GY91_writereg , 27 , &B00011000)          ' Bits 3+4 = 11 - Full Scale Range: +/-2000°/ss
   call I2c_write(GY91_writereg , 28 , &B00000000)          ' Bits 3+4 = 00 - Full Scale Range: +/-2g / No High Pass Filter
   call I2c_write(GY91_writereg , 107 , &B00001011)         ' No Reset / No Sleep / No Cycle / Temp_Sens: Dis / Clock Source: Z-Gyro
End Sub

sub GY91_Process

   ' -- ACCEL --
   GY91_GetAccel

   ' -- bias correction by bias A_b* from calib proc --
   a_x = a_x - a_bx
   a_y = a_y - a_by
   a_z = a_z - a_bz

   ' -- converting to gravity = 1(g) --
   a_sx = a_x / Mpu650_asens_2g
   a_sy = a_y / Mpu650_asens_2g
   a_sz = a_z / Mpu650_asens_2g

   incr a_sx                                                ' add 1_g to vertical axis

   ' -- calculating accel angles --
   a_ax = atn2(a_sy , a_sz)
   a_ay = atn2(a_sx , a_sz)
   a_az = atn2(a_sx , a_sy)

   ' -- convert radians to degrees --
   a_ax = rad2deg(a_ax)
   a_ay = rad2deg(a_ay)
   a_az = rad2deg(a_az)

   a_ay = - a_ay                                            ' it's not clear why, but Y axis for A and G has different direction

   ' -- GYRO --
   GY91_GetGyro

   ' -- bias correction --
   w_x = w_x - w_bx
   w_y = w_y - w_by
   w_z = w_z - w_bz

   ' -- calculate euler angles --
   '
   '         (3)       (2) (1)   (5)     (4)
   ' e[i] = k * (e[i-1] + w * dt) + (1-K) * a  ; where k ~ 0.98

   ' (1)-- get rotation --
   w_sx = w_x * dt
   w_sy = w_y * dt
   w_sz = w_z * dt

   ' (2) -- integrate --
   w_sx = w_sx + e_sx
   w_sy = w_sy + e_sy
   w_sz = w_sz + e_sz

'   ' (3) -- mul W by weight coeff. k --
   w_sx = k * w_sx
   w_sy = k * w_sy
   w_sz = k * w_sz

   ' (4) -- mul A by weight coeff (1 - k) --
   a_ax = a_ax * k1
   a_ay = a_ay * k1
   a_az = a_az * k1

   ' (5) -- calculate euler angles --
   e_sx = w_sx + a_ax
   e_sy = w_sy + a_ay
   e_sz = w_sz + a_az

   ' (6) -- make new copies of euler angles
   ' (because e_s* used for serial integeations and mustn't be changed outside procedure)
   Ex = e_sx
   Ey = e_sy
   Ez = e_sz

   ' (7) -- fit angles to my board position (x axis is placed verticaly)
   'ex = ? -- not used in this app
   Ey = 180 + Ey
   Ez = 180 - Ez

   ' -- BMP280 --
   BMP280_read                                              ' get temperature and pressure

end sub

sub GY91_GetAccel()
   i2cstart                                                 '  запуск i2c
   i2cwbyte GY91_writereg                                   '  передача адреса устройства, режим записи
   i2cwbyte &H3B                                            '  передача адреса памяти
   i2cstop                                                  '  остановка i2c
   i2cstart                                                 '  запуск i2c
   i2cwbyte GY91_readreg                                    '  передача адреса устройства, режим чтения
   i2crbyte A_rx(2) , ack                                   '  читаем данные HI
   i2crbyte A_rx(1) , ack                                   '  LOW
   i2crbyte A_ry(2) , ack
   i2crbyte A_ry(1) , ack
   i2crbyte A_rz(2) , ack
   i2crbyte A_rz(1) , nack
   i2cstop                                                  '  остановка i2c
end sub

sub GY91_GetGyro()
   i2cstart                                                 '  запуск i2c
   i2cwbyte GY91_writereg                                   '  передача адреса устройства, режим записи
   i2cwbyte &H43                                            '  передача адреса памяти
   i2cstop                                                  '  остановка i2c
   i2cstart                                                 '  запуск i2c
   i2cwbyte GY91_readreg                                    '  передача адреса устройства, режим чтения
   i2crbyte W_rx(2) , ack                                   '  читаем данные? сначала старший байт
   i2crbyte W_rx(1) , ack                                   '  затем младший
   i2crbyte W_ry(2) , ack
   i2crbyte W_ry(1) , ack
   i2crbyte W_rz(2) , ack
   i2crbyte W_rz(1) , nack
   i2cstop                                                  '  остановка i2c
end sub

sub GY91_Calibrate()                                        ' get some mesures and calculate average (bias) value
   dim i as integer

   ' compute average biases
   for i = 1 to calib_count
      GY91_GetAccel
      a_bx = a_bx + a_x
      a_by = a_by + a_y
      a_bz = a_bz + a_z
   next

   a_bx = a_bx / calib_count
   a_by = a_by / calib_count
   a_bz = a_bz / calib_count

   for i = 1 to calib_count
      GY91_GetGyro
      w_bx = w_bx + w_x
      w_by = w_by + w_y
      w_bz = w_bz + w_z
    next

   w_bx = w_bx / calib_count
   w_by = w_by / calib_count
   w_bz = w_bz / calib_count

end sub

function GY91_GetChipID()
   dim chipID as byte
   i2cstart                                                 '  запуск i2c
   i2cwbyte GY91_writereg                                   '  передача адреса устройства, режим записи
   i2cwbyte &H75                                            '  передача адреса памяти
   i2cstop                                                  '  остановка i2c
   i2cstart                                                 '  запуск i2c
   i2cwbyte GY91_readreg                                    '  передача адреса устройства, режим чтения
   i2crbyte chipID , Nack
   i2cstop                                                  '  остановка i2c
   GY91_GetChipID = chipID
end function

function BMP280_GetChipID()
   BMP280_temp(1) = &HD0                                    ' ID register address
   i2creceive BMP280_readreg , BMP280_temp(1) , 1 , 1
   BMP280_GetChipID = BMP280_temp(1)
end function

sub BMP280_Init()
  BMP280_temp(1) = &HF4 : BMP280_temp(2) = &HB7             ' Temperature oversampling x16, Pressure oversampling x16, Normal mode
  i2csend BMP280_readreg , BMP280_temp(1) , 2
  BMP280_temp(1) = &HF5 : BMP280_temp(2) = &H70             ' Standby 250ms, Filter x16
  i2csend BMP280_readreg , BMP280_temp(1) , 2
  BMP280_temp(1) = &H88                                     ' Read Temperature and Pressure calibration
  i2creceive BMP280_readreg , BMP280_temp(1) , 1 , 24
  T1 = makeint(BMP280_temp(1) , BMP280_temp(2))
  T2 = makeint(BMP280_temp(3) , BMP280_temp(4))
  T3 = makeint(BMP280_temp(5) , BMP280_temp(6))
  P1 = makeint(BMP280_temp(7) , BMP280_temp(8))
  P2 = makeint(BMP280_temp(9) , BMP280_temp(10))
  P3 = makeint(BMP280_temp(11) , BMP280_temp(12))
  P4 = makeint(BMP280_temp(13) , BMP280_temp(14))
  P5 = makeint(BMP280_temp(15) , BMP280_temp(16))
  P6 = makeint(BMP280_temp(17) , BMP280_temp(18))
  P7 = makeint(BMP280_temp(19) , BMP280_temp(20))
  P8 = makeint(BMP280_temp(21) , BMP280_temp(22))
  P9 = makeint(BMP280_temp(23) , BMP280_temp(24))
end sub

sub BMP280_Read()
   ' Fills Pressure and Temperature values
  BMP280_temp(1) = &HF7                                     ' Data registers address
  i2creceive BMP280_readreg , BMP280_temp(1) , 1 , 8
  dpress = BMP280_temp(1) : shift dpress , left , 8
  dpress = dpress + BMP280_temp(2) : shift dpress , left , 8
  dpress = dpress + BMP280_temp(3) : shift dpress , right , 4
  dtemp = BMP280_temp(4) : shift dtemp , left , 8
  dtemp = dtemp + BMP280_temp(5) : shift dtemp , left , 8
  dtemp = dtemp + BMP280_temp(6) : shift dtemp , right , 4
  '-------------------------- Temperature
  shift dtemp , right , 3 : temp1 = dtemp - t1 : temp1 = temp1 - t1
  temp1 = temp1 * t2 : shift temp1 , right , 11 , signed
  shift dtemp , right , 1 : temp2 = dtemp - t1
  temp2 = temp2 * temp2 : shift temp2 , right , 12 , signed
  temp2 = temp2 * t3 : shift temp2 , right , 14 , signed
  t_mem = temp1 + temp2 : temp2 = t_mem * 5
  temp2 = temp2 + 128 : shift temp2 , right , 8 , signed
  'Temperature = temp2 / 100
  curr_temp = temp2 / 140                                   ' must be 100 but internal heat or smth else get high temp than realy
  curr_temp = curr_temp * 0.002
  smth_temp = smth_temp * 0.998
  Temperature = smth_temp + curr_temp                       ' smoothed output
  smth_temp = Temperature

  '-------------------------- Pressure
  temp1 = t_mem : shift temp1 , right , 1 , signed
  temp1 = temp1 - 64000
  temp2 = temp1 : shift temp2 , right , 2 , signed
  temp2 = temp2 * temp2 : shift temp2 , right , 11 , signed
  temp2 = temp2 * p6
  temp3 = temp1 * p5 : shift temp3 , left , 1
  temp2 = temp2 + temp3
  temp3 = p4 : shift temp3 , left , 16
  shift temp2 , right , 2 , signed
  temp2 = temp2 + temp3
  temp3 = temp1 * p2 : shift temp3 , right , 1 , signed
  shift temp1 , right , 2 , signed
  temp1 = temp1 * temp1 : shift temp1 , right , 13
  temp1 = temp1 * p3 : shift temp1 , right , 3 , signed
  temp1 = temp1 + temp3 : shift temp1 , right , 18 , signed
  temp1 = temp1 + 32768
  temp1 = temp1 * p1 : shift temp1 , right , 15 , signed
  if temp1 = 0 then goto press_out                          'To avoid zero division
  press = 1048576 - dpress : shift temp2 , right , 12 , signed
  press = press - temp2 : press = press * 3125
  press = press / temp1 : press = press + press
  temp1 = press : shift temp1 , right , 3
  temp1 = temp1 * temp1 : shift temp1 , right , 13
  temp1 = temp1 * p9 : shift temp1 , right , 12 , signed
  temp2 = press : shift temp2 , right , 2
  temp2 = p8 * temp2 : shift temp2 , right , 13 , signed
  temp1 = temp1 + temp2 : temp1 = temp1 + p7
  shift temp1 , right , 4 , signed
  press = press + temp1
  press_out:
  'Pressure = Press                                          ' In Pa
  'Pressure = Press * 0.0075006                              ' In Mm Hg
  curr_press = press * 0.0075006                            ' In Mm Hg
  curr_press = curr_press * 0.01
  smth_press = smth_press * 0.99
  smth_press = smth_press + curr_press                      ' smoothed output
  Pressure = smth_press
end sub

+2


Вы здесь » Программирование ATMEL в BASCOM. » Вопросы - ответы » Осваиваем датчик BME280