Has anyone simulated a DS18B20 temperature sensor with a potentiometer before? I need to do this for a project that has 5 of them running, each with its own I/O and not running on a 1-wire bus though. All of them are running in 12-bit mode too. I'm going to use another PIC to be the fake sensors but I'm not sure how to even start to get this work properly. I know I have to load an array of 9 bytes with the first two being the temperature and the last one being the CRC. The rest I don't care about because I don't look at them anyways. Any help would be greatly appreciated
Code:
owout sensor,1,[$CC,$44] ' Send start temperature conversion command
low RailPullUp ' Turn on transistor between rail and data pin
pause 750 ' Allow enough time to process Tconv
high RailPullUp ' Turn off transistor between rail and data pin
owout sensor,1,[$CC,$BE] ' Send read scratch pad command
owin sensor,0,[STR dq\9] ' Read all 9 bytes and store them in dq array
rawtemp.Byte0 = dq[0] ' Isolate raw temperature from the rest
rawtemp.Byte1 = dq[1]
gosub getcrc ' Calculate the CRC of the data
if sensorerror[sensor] = 1 and y < 3 then ReadDS18B20
gosub converttemp ' Convert raw data into real temps
if sensorerror[sensor] = 1 then
y = 0
tempc = 0
tempf = 320
signc = 3
signf = 3
endif
high portb.7
high portb.6
high portb.5
goto ReadTempDone
ConvertTemp:
if negbit = 1 then belowzero ' If below 0ºC then goto different subroutine
signc = 3 ' Display + symbol for negative temp.
signf = 3 ' Display + symbol for positive temp.
dummy = 625 * rawtemp ' Multiply to load internal register with 32-bit value
tempc = DIV32 1000 ' Divide internal register by 10 to calculate precision ºC
dummy = 0
dummy = 1125 * rawtemp ' Multiply to load internal register with 32-bit value
tempf = DIV32 1000 ' Make it manageable
tempf = tempf + 320 ' Make it into ºF
return
BelowZero:
signc = 2 ' Display - symbol for negative temp.
signf = 3 ' Display + symbol for positive temp.
dummy = 0
rawtemp.byte0 = rawtemp.byte0 ^ 255
rawtemp.Byte1 = rawtemp.byte1 ^ 255
dummy = 625 * rawtemp + 1 ' Multiply inversion to load internal register with 32-bit value
tempc = DIV32 1000 ' Divide internal register by 100 to calculate precision ºC
tempf = (tempc + 5000) * 900 ' Multiply to load interal register with 32-bit value
tempf = DIV32 500 ' Divide internal register by 500
if rawtemp >= 285 then ' Check if temperature is + or - ºF
tempf = tempf - 12200 ' Process if temperature is to be negative
signf = 2 ' Display a - symbol for negative temperature
else
tempf = 12200 - tempf ' Process if temperature is to be positive
signf = 3 ' Display + symbol for a positive temp.
endif
return
'------------------------ Calculate the CRC from Byte 9 ------------------------
GetCRC:
for x = 0 to 7 ' Get CRC for each of the 8 bytes
databyte = dq[x] ' Assign array pointer using value of x
gosub CalcCRC ' Pick which method to use to get CRC value
next x ' Repeat until all bytes are done
if dq[8] <> crccalc then ' Do the CRC values match?
sensorerror[sensor] = 1 ' Set flag indicating a problem with this sensor
y = y + 1
else
sensorerror[sensor] = 0 ' Set flag indicating no problem with this sensor
y = 0
endif
crccalc = 0 ' Reset CRC calculation variable
return
'--------------------- CRC Bit Calcuation Method -------------------------------
CalcCRC:
for i = 0 to 7 ' Do for all 8 bits in data byte
databit = crccalc.0 ^ databyte.0 ' XOR bit0 of data byte and crc
databyte = databyte >> 1 ' Position data byte for next bit test
if databit = 0 then Shift ' If test bit not set, just shift crc_calc
crccalc = crccalc ^ $18 ' If set, account for EXOR feedback
shift: ' Shift right the crc_calc byte
crccalc = crccalc >> 1 ' CRC bit 0 to bit bucket
crccalc.7 = databit ' Data bit rotates into CRC bit 7
next i
return
Bookmarks