'----------------------------------------------------------- ' ' Dallas 1820 Temperature Probe ' ' Copyright 2000 by Jon Hylands ' jon@huv.com http://www.huv.com ' '----------------------------------------------------------- public const Dallas1820FamilyID as Byte = &h10 private const Dallas1820ConvertTemperature as Byte = &h44 private const Dallas1820ReadScratchpad as Byte = &hBE private const Dallas1820ReadPowerSupply as Byte = &hB4 '----------------------------------------------------------- ' ParasiticPowered_1820 ' ' Return true if any of the 1820s on the bus are running ' from parasitic power. If there are such devices, the bus ' must be held in a hard pullup for a long enough period ' for the device to perform a conversion. ' ' If there are no 1820s on the bus, just return false. public Function ParasiticPowered_1820 (ByVal busPin as Byte) as Boolean dim deviceCount as Byte, deviceIndex as Byte dim foundParasitic as Boolean dim romArray (1 to 8) as Byte deviceCount = GetDeviceCount_1Wire deviceIndex = 0 foundParasitic = false do call GetROMid_1Wire (deviceIndex, romArray) if romArray (1) = Dallas1820FamilyID then call MatchROM_1Wire (busPin, romArray) call PutByte_1Wire (busPin, Dallas1820ReadPowerSupply) foundParasitic = foundParasitic or (Get1Wire (busPin) = 0) if foundParasitic then exit do end if end if deviceIndex = deviceIndex + 1 loop until deviceIndex >= deviceCount ParasiticPowered_1820 = foundParasitic End Function '----------------------------------------------------------- ' ConvertTemperature_1820 ' ' Start a temperature conversion. ' ' This function assumes the device has been selected already. public Sub ConvertTemperature_1820 (ByVal busPin as Byte) call PutByte_1Wire (busPin, Dallas1820ConvertTemperature) End Sub '----------------------------------------------------------- ' IsDoneConversion_1820 ' ' This function returns true if the device is done ' doing a temperature conversion. This function can ' only be used if the device has an independent power ' supply, and thus is not parasitic. ' ' This function assumes the device has been selected already, ' and must immediately follow the ConvertTemperature_1820 ' command, with no other bus traffic in between. ' This function may be called repeadedly until it returns ' true for the first time. ' public Function IsDoneConversion_1820 (ByVal busPin as Byte) as Boolean IsDoneConversion_1820 = (Get1Wire (busPin) = 1) End Function '----------------------------------------------------------- ' ReadScratchpad_1820 ' ' Return 9 bytes read from the 1820 ' 8 bytes of data and one byte of CRC ' ' This routine assumes the 1820 has already been selected, ' and the temperature conversion is complete. public Sub ReadScratchpad_1820 (byVal busPin as Byte, _ ByRef temperatureData() as Byte) dim index as Integer call PutByte_1Wire (busPin, Dallas1820ReadScratchpad) for index = 1 to 9 temperatureData (index) = InByte_1Wire (busPin) next End Sub '----------------------------------------------------------- ' GetTemperature_1820 ' ' Return the temperature of the device indicated by the given ' device index. ' ' This function assumes a SearchROM has been performed at some ' point since the devices were added to the circuit. public Function GetTemperature_1820 (ByVal busPin as Byte, _ ByVal deviceIndex as Byte) as Single dim romArray (1 to 8) as Byte dim temperatureData (1 to 9) as Byte dim dataLsb as Byte, dataMsb as Byte dim temperature as Single dim parasitic as Boolean parasitic = ParasiticPowered_1820 (busPin) call GetROMid_1Wire (deviceIndex, romArray) call MatchROM_1Wire (busPin, romArray) call ConvertTemperature_1820 (busPin) if parasitic then call StrongPullup_1820 (busPin) else do ' Do some interesting stuff in here, whatever... call DelayUntilClockTick loop until IsDoneConversion_1820 (busPin) end if call MatchROM_1Wire (busPin, romArray) call ReadScratchpad_1820 (busPin, temperatureData) dataLsb = temperatureData (1) dataMsb = temperatureData (2) ' Check for negative temperature if (GetBit (dataMsb, 7) = 1) then temperature = (CSng (dataMsb xor 255) * 256.0) + _ CSng (dataLsb xor 255) + 1.0 else temperature = (CSng (dataMsb) * 256.0) + CSng (dataLsb) end if GetTemperature_1820 = temperature / 2.0 End Function '----------------------------------------------------------- ' StrongPullup_1820 ' ' Pullup the bus pin for 500 ms. This is the time ' required to perform a temperature conversion. public Sub StrongPullup_1820 (ByVal busPin as Byte) call PutPin (busPin, bxOutputHigh) call Sleep (0.5) call PutPin (busPin, bxInputTristate) End Sub