[BASCOM] TMP100 Termometr na I2C

Gotowe kody obsługi układów elektronicznych udostępnione przez użytkowników forum.
ODPOWIEDZ
Awatar użytkownika
niveasoft
Posty: 1213
Rejestracja: 17 sie 2015, 12:13
Kontakt:

[BASCOM] TMP100 Termometr na I2C

Post autor: niveasoft » 18 lis 2017, 1:17

Taki maluszek na sześc pinów. Ma ciekawe funkcje.
Możliwość włączenia ciągłej konwersji, ale tez domyślnie dokonuje tylko jednej konwersji i przechodzi w tryb oszczędzania energii.
Ma też możliwość ustawienia progów dolnego i górnego i reakcji po ich przekroczeniu. Dokładność do 12 bit.
Napisałem kilka podstawowych linijek które mogą ułatwić start z tym układem.
Można by jeszcze przesunąć przecinek i pozbyć się obliczeń na liczbach zmiennoprzecinkowych (tutaj Single).
  1. $nocompile
  2.  
  3. '----------UNMARK ONLY ONE-----------WRITE < READ-------------THAT MATCH HARDWARE-----
  4.  Const Tp100_addr = &B1001_0000                             'ADD1=0 ADD0 = 0
  5. 'Const Tp100_addr = &B1001_0010                                'ADD1=0 ADD0=Float
  6. 'Const Tp100_addr = &B1001_0100                                'ADD1=0 ADD0=1
  7. 'Const Tp100_addr = &B1001_1000                                'ADD1=1 ADD0=0
  8. 'Const Tp100_addr = &B1001_1010                                'ADD1=1 ADD0=Float
  9. 'Const Tp100_addr = &B1001_1100                                'ADD1=1 ADD0=1
  10. 'Const Tp100_addr = &B1001_0110                                'ADD1=Float ADD0=0
  11. 'Const Tp100_addr = &B1001_1110                                'ADD1=Float ADD0=1
  12.  
  13. Const Temp_reg = 0
  14. Const Conf_reg = 1
  15. Const Min_temp = 2
  16. Const Max_temp = 3
  17.  
  18. Const Shuttdown_mode = 0                                    'bit 0 default 1=Shhuttdown ON
  19. Const Thermostat_mode = 1                                   'bit 1
  20. Const Polarisation = 2
  21. Const Fault_bits0 = 3
  22. Const Fault_bits1 = 4
  23. Const Resolution_bits0 = 5
  24. Const Resolution_bits1 = 6
  25. Const Start_conv = 7
  26.  
  27. Const 9bit = 0.5
  28. Const 10bit_res = 0.25
  29. Const 11bit_res = 0.125
  30. Const 12bit_res = 0.0625
  31.  
  32. 'R1=0,R0=0 ->9bits 40ms
  33. 'R1=0.R0=1 ->10bits 80ms
  34. 'R1=1,R0=0 ->11bits 160ms
  35. 'R1=1,R0=1 ->12bits 320ms
  36.  
  37.  
  38. Dim Conf_byte As Byte , Tempb(2) As Byte , Error As Byte , Sum As Long , Count As Byte
  39. Dim Raw As Integer At Tempb(1) Overlay
  40. Dim Temperature As String * 10 , Temps As Single
  41.  
  42. 'Conf_byte = Bits(resolution_bits0 , Resolution_bits1 , Start_conv)       'continuos read 12bits
  43. Conf_byte = Bits(shuttdown_mode , Resolution_bits0 , Resolution_bits1 , Start_conv)       'single shot 12bits
  44.  
  45. Sub Start_conversion()
  46.  
  47.  I2cstart
  48.   I2cwbyte Tp100_addr                                       'write address
  49.   I2cwbyte Conf_reg                                         'chose Reg1 (config or start conversion)
  50.   I2cwbyte Conf_byte                                        'start conversion by setting bit 7
  51.  I2cstop
  52.  
  53. End Sub
  54.  
  55. Function Read_t100() As Byte
  56.  
  57.  I2cstart
  58.   I2cwbyte Tp100_addr
  59.   If Err = 0 Then
  60.     I2cwbyte Temp_reg                                       'we will be reading first
  61.    I2cstart
  62.     I2cwbyte Tp100_addr + 1                                 ' address for read
  63.     I2crbyte Tempb(2) , Ack
  64.     I2crbyte Tempb(1) , Nack
  65.    I2cstop
  66.   Else
  67.    'no acknowledge from the sensor
  68.    Read_t100 = 1
  69.     Exit Function                                           'will skip the math
  70.   End If
  71.  
  72.    Shift Raw , Right , 4 , Signed
  73.  
  74.    Read_t100 = 0                                            'no error
  75. End Function
Poniżej przykład użycia
  1. $regfile = "m32u4def.dat"
  2. $crystal = 16000000
  3. $swstack = 64
  4. $hwstack = 64
  5. $framesize = 64
  6.  
  7. Config Submode = New
  8.  
  9. Config Timer1 = Timer , Prescale = 8 , Compare_a = Disconnect , Compare_b = Disconnect , Clear_timer = 1
  10.  Compare1a = 39999                                          '20ms @16MHz/8
  11.    Dim Miliseconds As Byte
  12.  
  13. $lib "i2c_twi.lbx"
  14.  Config Scl = Portd.0
  15.  Config Sda = Portd.1
  16.  Config Twi = 100000
  17. I2cinit
  18.  
  19. $include "TMP100_lib.inc"                                   ' <<-- dołączenie funkcji
  20.  
  21. 'SUBS READY
  22. ' MAIN LOOP STARTS HERE
  23.  
  24.  
  25. Do
  26.  
  27.  'every 20ms
  28.  If Tifr1.ocf1a = 1 Then
  29.   Tifr1.ocf1a = 1
  30.  
  31.    'every320ms
  32.   If Miliseconds < 15 Then                                  '16x20ms=320ms
  33.    Incr Miliseconds
  34.   Else
  35.    Miliseconds = 0
  36.  
  37.  
  38.    If Read_t100() = 0 Then                                  'if no error
  39.  
  40.      If Count < 3 Then                                      'count to 4
  41.       Incr Count
  42.        Sum = Sum + Raw
  43.      Else
  44.        Count = 0
  45.  
  46.        Sum = Sum + Raw
  47.        Shift Sum , Right , 2 , Signed                       'divide by four
  48.         Temps = Sum * 12bit_res                             '0.0625
  49.         Temperature = Fusing(temps , "#.#")
  50.         Locate 1 , 1 : Lcd Temperature ; "        "
  51.        Sum = 0
  52.  
  53.      End If
  54.  
  55.    Else
  56.     Count = 0 : Sum = 0                                     'error so we must start from the begining
  57.      Lcd "No Answear"
  58.  
  59.    End If
  60.  
  61.    'start of next conversion
  62.     Call Start_conversion()
  63.  
  64.   End If
  65.  
  66.  End If
  67.  
  68. Loop
  69. End
ODPOWIEDZ