PDA

View Full Version : Simulating a DS18B20



CocaColaKid
- 2nd February 2007, 14:35
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



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

Archangel
- 3rd February 2007, 02:09
edit: see next post

http://www.maxim-ic.com/getds.cfm?qv_pk=2812

Archangel
- 3rd February 2007, 02:10
At first glance it appeared as though you thought those were analog sensors, I see what you are trying to do. Here is a link to the datasheet. I believe you need to set up a pic to measure using A/D converters and output binary (serial) like
DS18B20 does. Sounds like a school project. You might even swap the pots for thermisters and really emulate a DS18B20.


http://www.maxim-ic.com/getds.cfm?qv_pk=2812

CocaColaKid
- 5th February 2007, 12:35
I never thought these were analog sensors. I use them specifically because they are digital. I however require for testing and troubleshooting purposes to be able to adjust the temperatures of each of the sensors so I can debug my code a lot easier. Trying to control the temperature of 5 sensors at the same time is way to difficult.

J. Mark Wolf
- 5th February 2007, 22:35
Trying to control the temperature of 5 sensors at the same time is way to difficult.

Wire up five DS18B20 sensors in close proximity (you can plug all of them into the same row on a breadboard), and blow on them with either a blowdryer or freeze mist.

Couldn't be simpler.

CocaColaKid
- 6th February 2007, 13:28
That would be much simpler but would not help or as we need to have different temperatures on each of the sensors to test particular functions.

keithdoxey
- 6th February 2007, 15:24
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.

I would suggest that trying to fake the data that would be returned by the sensors would be a nightmare. You would have to get Pic#2 to behave EXACTLY the same as a DS18B20.

I would use 5 of the real sensors as someone else suggested but strap each one to one of the aluminium clad power resistors. you could then vary the current through the resistor to adjust the temperature sensed by each individual sensor.

Many times in the past I have tried to fake data but it isnt a true test for the real world. Using the method above is still faking, but faking the environment not the hardware/software so should be much closer to a real world scenareo

CocaColaKid
- 6th February 2007, 15:39
Keith,

Thanks for the advise but i need to be able to run them down to -25C and up to 30C. Power resistors wont work well for this type of testing though. I heat pump might work but requires a lot of power though.

keithdoxey
- 6th February 2007, 18:49
Keith,

Thanks for the advise but i need to be able to run them down to -25C and up to 30C. Power resistors wont work well for this type of testing though. I heat pump might work but requires a lot of power though.

Was worth a try :)

How about using the power resistors and sticking the whole assembly in the deep freeze. LOL :)

Archangel
- 6th February 2007, 23:05
wouldn't it be possible to use your PC to output the binary or hex code expected from a 3 wire device? The data sheet has a table of temp to code conversion, why can not you simply send it frrom the serial port?