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

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

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

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


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


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

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

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


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