Simulating a DS18B20


Closed Thread
Results 1 to 10 of 10
  1. #1

    Default Simulating a DS18B20

    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

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    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
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    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.

  5. #5
    Join Date
    Oct 2005
    Location
    Pinckney, Michigan
    Posts
    91


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by CocaColaKid View Post
    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.

  6. #6


    Did you find this post helpful? Yes | No

    Default

    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.

  7. #7
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by CocaColaKid View Post
    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
    Keith

    www.diyha.co.uk
    www.kat5.tv

  8. #8


    Did you find this post helpful? Yes | No

    Default

    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.

  9. #9
    Join Date
    Feb 2003
    Posts
    432


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by CocaColaKid View Post
    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
    Keith

    www.diyha.co.uk
    www.kat5.tv

  10. #10
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Computer String

    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?
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. Please help with code for DS18B20
    By fratello in forum mel PIC BASIC Pro
    Replies: 109
    Last Post: - 28th April 2013, 21:12
  2. Help with DS18B20 program
    By presario1425 in forum mel PIC BASIC Pro
    Replies: 38
    Last Post: - 22nd August 2012, 00:50
  3. Ds18b20 + 16f628a
    By Max Power in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 16th July 2009, 11:29
  4. DS18B20 interfers with Timer 2 interrupts
    By John_Mac in forum General
    Replies: 7
    Last Post: - 23rd February 2009, 23:52
  5. DS18B20 error reading
    By Gaetano in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th August 2007, 16:21

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts