Zur Übersicht - INFO - Neueste 50 Beiträge - Neuer Beitrag - Suchen - Zum C-Control-I-Forum - Zum C-Control-II-Forum

DS2482 Kategorie: I²C-Bus (von Daneie - 6.11.2017 19:49)
Ich nutze:
C-Control Pro AVR32
Hallo,
hier ein Code für den DS2482.
Hilft vom allem wenn die OneWire-Leitung zu lang ist.
Dieser Code ist jetzt für 3 DS18B20.
 

Dim I, X, I2COK As Integer
Dim vScratchPad(9) As Byte
Dim I2C_Data(10) As Byte
Dim tCRC_table(256) = {_
    000, 094, 188, 226, 097, 063, 221, 131, 194, 156, 126, 032, 163, 253, 031, 065,_
    157, 195, 033, 127, 252, 162, 064, 030, 095, 001, 227, 189, 062, 096, 130, 220,_
    035, 125, 159, 193, 066, 028, 254, 160, 225, 191, 093, 003, 128, 222, 060, 098,_
    190, 224, 002, 092, 223, 129, 099, 061, 124, 034, 192, 158, 029, 067, 161, 255,_
    070, 024, 250, 164, 039, 121, 155, 197, 132, 218, 056, 102, 229, 187, 089, 007,_
    219, 133, 103, 057, 186, 228, 006, 088, 025, 071, 165, 251, 120, 038, 196, 154,_
    101, 059, 217, 135, 004, 090, 184, 230, 167, 249, 027, 069, 198, 152, 122, 036,_
    248, 166, 068, 026, 153, 199, 037, 123, 058, 100, 134, 216, 091, 005, 231, 185,_
    140, 210, 048, 110, 237, 179, 081, 015, 078, 016, 242, 172, 047, 113, 147, 205,_
    017, 079, 173, 243, 112, 046, 204, 146, 211, 141, 111, 049, 178, 236, 014, 080,_
    175, 241, 019, 077, 206, 144, 114, 044, 109, 051, 209, 143, 012, 082, 176, 238,_
    050, 108, 142, 208, 083, 013, 239, 177, 240, 174, 076, 018, 145, 207, 045, 115,_
    202, 148, 118, 040, 171, 245, 023, 073, 008, 086, 180, 234, 105, 055, 213, 139,_
    087, 009, 235, 181, 054, 104, 138, 212, 149, 203, 041, 119, 244, 170, 072, 022,_
    233, 183, 085, 011, 136, 214, 052, 106, 043, 117, 151, 201, 074, 020, 246, 168,_
    116, 042, 200, 150, 021, 075, 169, 247, 182, 232, 010, 084, 215, 137, 107, 053 _
    } As Byte

#define DS2482 0x30

Sub main()
   Dim TEMP As Integer
   I2C_SetSpeed(24000)
   Do While True                                           'Endlosschleife
      DS2482SPUON()
      DS2482SKIPROM()
      CONVERT()

      MATCHROM(40,5,54,215,5,0,0,95)
      TEMP=DS1820GET_TEMP()
      Msg_WriteInt(TEMP):Msg_WriteText(" °Cr")


      MATCHROM(40,229,5,215,5,0,0,188)
      TEMP=DS1820GET_TEMP()
      Msg_WriteInt(TEMP):Msg_WriteText(" °Cr")


      MATCHROM(40,243,143,216,5,0,0,185)
      TEMP=DS1820GET_TEMP()
      Msg_WriteInt(TEMP):Msg_WriteText(" °Cr"):Msg_WriteText(" r")

      Thread_Delay(100)
   End While
End Sub

Sub DS1820GET_TEMP() As Integer
  Dim vScratshpat(9) As Byte
  Dim vChecksum As Byte
  Dim TEMP As Integer
  DS2482WIRE_WRITEBYTE(0xBE)         'read ram
  /*DS2482WIRE_READBYTE()
  TEMPLO=DS2482GET_DATA()
  DS2482WIRE_READBYTE()
  TEMPHI=DS2482GET_DATA() */
  'TEMP=(TEMPHI*256)+TEMPLO

  For I = 0 To 8
     DS2482WIRE_READBYTE()
     vScratshpat(I)=DS2482GET_DATA()
     'Msg_WriteInt(vScratshpat(I)):Msg_WriteText(" - ")
  Next
  Msg_WriteChar(13)
  DS2482WIRERESET()
  vChecksum = 0
  For I = 0 To 8
     vChecksum = tCRC_table(vChecksum Xor vScratshpat(I))
     If vChecksum < 0 Then
        Msg_WriteText("CRC-Fehlerr")
     End If
  Next
  TEMP = vScratshpat(2) * 256
  TEMP = TEMP + vScratshpat(1)
  TEMP = TEMP * 0.6255
  Return TEMP
End Sub


Sub DS2482GET_STATUS() As Byte
Dim D(1) As Byte
I2COK = I2C_Read(DS2482,0xE1F0,2,D,1)
If I2COK = 1 Then : Msg_WriteText("I2C-Fehler DS2482GET_STATUS Readr") : End If
Return D(0)
/*IIC.INIT
IIC.START
IIC.SEND DS2482
IIC.SEND SET_READ_POINTER
IIC.SEND STATUS_POINTER
IIC.STOP

IIC.START
IIC.SEND (DS2482+1)
IIC.GET DATA
IIC.STOP
IIC.OFF
RETURN DATA */
End Sub

Sub DS2482WIRE_WRITEBYTE(DATA As Byte)
Dim D(1) As Byte
D(0) = DATA
DS2482WIRE_WAITIDLE()
I2COK = I2C_Write(DS2482,0xA5,1,D,1)
If I2COK = 1 Then : Msg_WriteText("I2C-Fehler DS2482WIRE_WAITIDLEr") : End If
End Sub

Sub DS2482WIRE_WAITIDLE()
Lab WT
If (DS2482GET_STATUS() And 1) > 0 Then
   Goto WT
End If
End Sub

Sub DS2482SPUON()
I2COK = I2C_Write(DS2482,0xD2B4,2,I2C_Data,0)     'B4
If I2COK = 1 Then : Msg_WriteText("I2C-Fehler DS2482SPUONr") : End If
End Sub

Sub DS2482SKIPROM()
DS2482WIRE_WRITEBYTE(0xCC)
End Sub

Sub CONVERT()
Dim DA As Byte

'----------------- WIRE CMD CONVERT TEMP ----------------------
DS2482SPUON()
DS2482WIRE_WRITEBYTE(0x44) 'TEMP CONVERT
'--------------------------------------------------------------
Lab WTC                      'WAIT CONVERTION IN NON-PARASITE POWER MODE
DS2482WIRE_READBYTE()
DA = DS2482GET_DATA()
If (DA And 0x80) = 0 Then
   Goto WTC
End If
  'pause 40
  '----------------- WIRE CMD READ RAM --------------------------
  DS2482WIRERESET()
End Sub

Sub DS2482GET_DATA() As Byte
Dim D(1) As Byte
I2COK = I2C_Read(DS2482,0xE1E1,2,D,1)
If I2COK = 1 Then : Msg_WriteText("I2C-Fehler DS2482GET_DATA E1E1r") : End If
Return D(0)
/*  define DATA as byte
  IIC.INIT
  IIC.START
  IIC.SEND DS2482
  IIC.SEND SET_READ_POINTER
  IIC.SEND DATA_POINTER
  IIC.STOP

  IIC.START
  IIC.SEND (DS2482+1)
  IIC.GET DATA
  IIC.STOP
  IIC.OFF
  RETURN DATA */
End Sub

Sub DS2482WIRE_READBYTE()
I2COK = I2C_Write(DS2482,0x96,1,I2C_Data,0)
If I2COK = 1 Then : Msg_WriteText("I2C-Fehler DS2482WIRE_READBYTEr") : End If

/*  DS2482WIRE_WAITIDLE()
  IIC.INIT
  IIC.START
  IIC.SEND DS2482
  IIC.SEND WIRE_READBYTE
  IIC.STOP
  IIC.OFF  */
End Sub

Sub DS2482WIRERESET()
I2COK = I2C_Write(DS2482,0xB4,1,I2C_Data,0)
If I2COK = 1 Then : Msg_WriteText("I2C-Fehler DS2482WIRERESETr") : End If
  /*IIC.INIT
  IIC.START
  IIC.SEND DS2482
  IIC.SEND WIRE_RESET
  IIC.STOP
  IIC.OFF*/
End Sub

Sub MATCHROM(d0 As Byte,d1 As Byte, d2 As Byte,d3 As Byte,d4 As Byte,d5 As Byte,d6 As Byte,d7 As Byte)
  DS2482WIRE_WRITEBYTE(0x55)
  DS2482WIRE_WRITEBYTE(d0)
  DS2482WIRE_WRITEBYTE(d1)
  DS2482WIRE_WRITEBYTE(d2)
  DS2482WIRE_WRITEBYTE(d3)
  DS2482WIRE_WRITEBYTE(d4)
  DS2482WIRE_WRITEBYTE(d5)
  DS2482WIRE_WRITEBYTE(d6)
  DS2482WIRE_WRITEBYTE(d7)
End Sub


    Antwort schreiben


Antworten: