сделал себе библиотеку  для датчика давления и температуры   BMP180 -  BMP180lib.bas

Код:
$nocompile



Dim Bmp180_precision As Byte
Dim Bmp180_shift As Byte
Dim Bmp180_i2c_send_buf(5) As Byte
Dim Bmp180_i2c_receive_buf(5) As Byte
Dim Bmp180_calibration_data(11) As Integer
Dim Bmp180_ac1 As Integer At Bmp180_calibration_data(1) Overlay
Dim Bmp180_ac2 As Integer At Bmp180_calibration_data(1) + 2 Overlay
Dim Bmp180_ac3 As Integer At Bmp180_calibration_data(1) + 4 Overlay
Dim Bmp180_ac4 As Word At Bmp180_calibration_data(1) + 6 Overlay
Dim Bmp180_ac5 As Word At Bmp180_calibration_data(1) + 8 Overlay
Dim Bmp180_ac6 As Word At Bmp180_calibration_data(1) + 10 Overlay
Dim Bmp180_b1 As Integer At Bmp180_calibration_data(1) + 12 Overlay
Dim Bmp180_b2 As Integer At Bmp180_calibration_data(1) + 14 Overlay
Dim Bmp180_mb As Integer At Bmp180_calibration_data(1) + 16 Overlay
Dim Bmp180_mc As Integer At Bmp180_calibration_data(1) + 18 Overlay
Dim Bmp180_md As Integer At Bmp180_calibration_data(1) + 20 Overlay
Dim Bmp180_u_temperatur As Long
Dim Bmp180_u_temp_low As Byte At Bmp180_u_temperatur Overlay
Dim Bmp180_u_temp_high As Byte At Bmp180_u_temperatur + 1 Overlay
Dim Bmp180_u_pressure As Long
Dim Bmp180_u_pressure_low As Byte At Bmp180_u_pressure Overlay
Dim Bmp180_u_pressure_mid As Byte At Bmp180_u_pressure + 1 Overlay
Dim Bmp180_u_pressure_high As Byte At Bmp180_u_pressure + 2 Overlay

Dim Bmp180_x1 As Long , Bmp180_x2 As Long , Bmp180_x3 As Long
Dim Bmp180_b3 As Long , Bmp180_b4 As Long , Bmp180_b5 As Long , Bmp180_b6 As Long
Dim Bmp180_b6_2 As Long , Bmp180_b7 As Long
Dim Bmp180_p As Long
Dim Bmp180_delay As Byte , Bmp180_addr As Byte

Dim Bmp180_temp_long As Long
Dim Bmp180_byte As Byte

Goto Bmp_end

'============================= Pressure BMP180 =================================
Bmp180_read_calibration_data:                               'ñ÷èòûâàþòñÿ äàííûå êàëèáðîâêè  èç äàò÷èêà (11õ16-áèòíûõ çíà÷åíèé)

   Bmp180_calibration_data(1) = &HAA
   I2creceive &HEE , Bmp180_calibration_data(1) , 1 , 22

   For Bmp180_byte = 1 To 11
      Swap Bmp180_calibration_data(bmp180_byte)
   Next

Return
'=============================================================================================================

Bmp180_config:                                              'òî÷íîñòü oò 0 äî 3

   Bmp180_precision = 0
   Bmp180_shift = 8 - Bmp180_precision
   Select Case Bmp180_precision
      Case 0
         Bmp180_delay = 5
         Bmp180_addr = &H34
      Case 1
         Bmp180_delay = 8
         Bmp180_addr = &H74
      Case 2
         Bmp180_delay = 14
         Bmp180_addr = &HB4
      Case 3
         Bmp180_delay = 28
         Bmp180_addr = &HF4
   End Select

Return

'===================================================================================================
Bmp180:
   Disable Interrupts
   Thermo = 0
   Pressure = 0

'------------------------ Temperature
   I2cstart
   I2cwbyte &HEE
   I2cwbyte &HF4
   I2cwbyte &H2E
   I2cstop
   If Err = 0 Then
      Waitms 5
      I2cstart                                              'Read uncompensated Temperature
      I2cwbyte &HEE
      I2cwbyte &HF6
      I2crepstart
      I2cwbyte &HEF
      I2crbyte Bmp180_u_temp_high , Ack
      I2crbyte Bmp180_u_temp_low , Nack
      I2cstop

      Bmp180_x1 = Bmp180_u_temperatur - Bmp180_ac6
      Bmp180_x1 = Bmp180_x1 * Bmp180_ac5                    ' Calculate true Temperature
      Shift Bmp180_x1 , Right , 15 , Signed
      Bmp180_x2 = Bmp180_mc : Shift Bmp180_x2 , Left , 11
      Bmp180_x3 = Bmp180_x1 + Bmp180_md
      Bmp180_x2 = Bmp180_x2 \ Bmp180_x3
      Bmp180_b5 = Bmp180_x1 + Bmp180_x2
      Bmp180_temp_long = Bmp180_b5 + 8

      Shift Bmp180_temp_long , Right , 4 , Signed
      Thermo = Bmp180_temp_long

   '------------------------- Pressure
      I2cstart                                              'Read uncompensated Pressure
      I2cwbyte &HEE
      I2cwbyte &HF4
      I2cwbyte Bmp180_addr
      I2cstop
      Waitms Bmp180_delay                                   'wait for conversion
      Bmp180_i2c_receive_buf(1) = &HF6
      I2creceive &HEE , Bmp180_i2c_receive_buf(1) , 1 , 3
      Bmp180_u_pressure_high = Bmp180_i2c_receive_buf(1)
      Bmp180_u_pressure_mid = Bmp180_i2c_receive_buf(2)
      Bmp180_u_pressure_low = Bmp180_i2c_receive_buf(3)
      Shift Bmp180_u_pressure , Right , Bmp180_shift
      Bmp180_b6 = Bmp180_b5 - 4000
      Bmp180_b6_2 = Bmp180_b6 * Bmp180_b6
      Shift Bmp180_b6_2 , Right , 12 , Signed
      Bmp180_x1 = Bmp180_b6_2 * Bmp180_b2
      Shift Bmp180_x1 , Right , 11 , Signed
      Bmp180_x2 = Bmp180_ac2 * Bmp180_b6
      Shift Bmp180_x2 , Right , 11 , Signed
      Bmp180_x3 = Bmp180_x1 + Bmp180_x2
      Bmp180_x1 = Bmp180_ac1 * 4
      Bmp180_x1 = Bmp180_x1 + Bmp180_x3
      Shift Bmp180_x1 , Left , Bmp180_precision
      Bmp180_b3 = Bmp180_x1 + 2
      Shift Bmp180_b3 , Right , 2 , Signed
      Bmp180_x1 = Bmp180_ac3 * Bmp180_b6
      Shift Bmp180_x1 , Right , 13 , Signed
      Bmp180_x2 = Bmp180_b1 * Bmp180_b6_2
      Shift Bmp180_x2 , Right , 16 , Signed
      Bmp180_x3 = Bmp180_x1 + Bmp180_x2
      Bmp180_x3 = Bmp180_x3 + 2
      Shift Bmp180_x3 , Right , 2 , Signed
      Bmp180_b4 = Bmp180_x3 + 32768
      Bmp180_b4 = Bmp180_b4 * Bmp180_ac4
      Shift Bmp180_b4 , Right , 15 , Signed
      Bmp180_b4 = Abs(bmp180_b4)
      Bmp180_x1 = 50000
      Shift Bmp180_x1 , Right , Bmp180_precision
      Bmp180_b7 = Bmp180_u_pressure - Bmp180_b3
      Bmp180_b7 = Bmp180_b7 * Bmp180_x1
      Bmp180_b7 = Abs(bmp180_b7)
      If Bmp180_b7 < &H80000000 Then
         Bmp180_p = Bmp180_b7 * 2

         Bmp180_p = Bmp180_p / Bmp180_b4
      Else
         Bmp180_p = Bmp180_b7 / Bmp180_b4
       ' Shift Bmp180_p , Left , 2 , 1                   '
         Bmp180_p = Bmp180_p * 2
      End If
      Bmp180_x1 = Bmp180_p
      Shift Bmp180_x1 , Right , 8 , Signed
      Bmp180_x1 = Bmp180_x1 * Bmp180_x1
      Bmp180_x1 = Bmp180_x1 * 3038
      Shift Bmp180_x1 , Right , 16 , Signed
      Bmp180_x2 = -7357 * Bmp180_p
      Shift Bmp180_x2 , Right , 16 , Signed
      Bmp180_x1 = Bmp180_x1 + Bmp180_x2
      Bmp180_x1 = Bmp180_x1 + 3791
      Shift Bmp180_x1 , Right , 4 , Signed
    '------------------------------------------------------------------------------
      Pressure = Bmp180_p + Bmp180_x1


   End If
   Enable Interrupts
Return

'===============================================================================
Bmp_end:


объявление переменных и конфигурирование портов:

Код:
Dim Pressure As Long
Dim Thermo As Long
'---конфигурируем шину I2C
Config Scl = Port....
Config Sda = Port....

где Pressure возвращаемое значение давления в кПа
Thermo - температура

пример работы с библиотекой:

Код:


$regfile = "m8adef.dat"
$crystal = 8000000
$baud = 9600
$hwstack = 32
$swstack = 32
$framesize = 40


'---обьявляем переменные для ВМР180
Dim Pressure As Long
Dim Thermo As Long
'---конфигурируем шину I2C
Config Scl = Portc.5
Config Sda = Portc.4


Dim Mmrt As Long
Dim T_lcd1 As String * 30
Dim T_lcd2 As String * 5
Dim T_lcd3 As String * 7


 '============================= Pressure BMP180 =================================
 $include "bmp180lib.bas"
 '=============================================================================================================





Print "Text to print."

'============================= Pressure BMP180 =================================
Gosub Bmp180_config                                         ' --- BMP180.
Gosub Bmp180_read_calibration_data
'=============================================================================================================



Do
'============================= Pressure BMP180 =================================
Gosub Bmp180                                                'Опрос  датчика
'=============================================================================================================

'  Mmrt = Pressure * 0.750063
Mmrt = Pressure * 10000                                     'переводим в
Mmrt = Mmrt / 13332                                         'мм ртутного столба
Mmrt = Mmrt / 10
T_lcd3 = Str(pressure)
T_lcd3 = Format(t_lcd3 , "0000.00")
T_lcd2 = Str(mmrt)
T_lcd2 = Format(t_lcd2 , "000.0")
T_lcd1 = Str(thermo)
T_lcd1 = Format(t_lcd1 , "00.0")
T_lcd1 = T_lcd3 + " hPa; " + T_lcd2 + " mm; " + T_lcd1 + " C"
Print T_lcd1

Wait 2

Loop


Тут исходники + проект протеуса

Отредактировано Penumbra (2017-06-26 17:36:57)