Shop OBEX P1 Docs P2 Docs Learn Events
Ds2760 — Parallax Forums

Ds2760

dberkstresserdberkstresser Posts: 5
edited 2007-04-01 18:29 in Propeller 1
Hi, I am trying to read a DS2760 thermocouple using the propeller chip. I have figured some of it out but translating code in a language I don't know into a language I just started learning is tough. I have the chip data-sheet from Maxim and the example program written for the BS2p.

I am reading the cold junction temp fine. (I think)
I am getting the data for the TC volts but I am having trouble formatting it.
Also, I can't figure out how the table lookup is working, this will probably be client-side code so I just need pseudo-code or an explanation. I have the tables, I just need to know how to interpret them.

TC Volts: I read 2 bytes using the 1-wire module, then reverse them for some reason (thats how the bs2p one works?), bit-shift them right 3, and print as an integer?

Anyway, here is the relevant section of my hacked OW-SearchDemo file. Any help would be greatly appreciated.

Thanks,

Dan

PUB main | n, i, p
  term.start(12)             ' start TV terminal     
  Serial.start(1,0,0,9600)  ' Rx,Tx, Mode, Baud
  ow.start(OW_DATA)                                     ' start 1-wire object, pin 0
  repeat 
    setColor(6)
    displayString(0, 0, string("      Temperature Monitor Test       "))
    setColor(0)
    
    ow.reset                                             
    ow.writeByte($CC)
    ow.writeByte($69)
    ow.writeByte($18)
     
    temp1 := ow.readByte
    temp2 :=  ow.readByte

    ow.reset
    ow.writeByte($CC)
    ow.writeByte($69)
    ow.writeByte($0E)
    tc_volt1:=ow.readByte
    tc_volt2:=ow.readByte
    tc_volt.byte[noparse][[/noparse] 1]:=tc_volt1
    tc_volt.byte[noparse][[/noparse]0]:=tc_volt2
    tc_volt:=tc_volt >> 3

    displayString(5, 13, Num.ToStr(tc_volt1, Num#BIN))
    displayString(6, 13, Num.ToStr(tc_volt2, Num#BIN))
    displayString(7, 13, Num.ToStr(tc_volt, Num#BIN))
    displayString(8, 13, Num.ToStr(tc_volt, Num#DEC))
    
    tempF := f.FAdd(f.FMul(f.FFloat(temp1), 1.8), 32.0)
     
    displayString(5, 3, string("CJ Temp"))
    displayString(6, 3, string("Deg C:")) 
    displayString(7, 3, Num.ToStr(temp1, Num#DEC))
     
    displayString(8, 3, string("Deg F:"))  
    displayString(9, 3, fp.FloatToString(tempF))

    Serial.Str(string("!NB0W00:"))     'Assign Pink Variable 01: tempF
    Serial.Str(fp.FloatToString(tempF))
    Serial.tx(CLS)
    waitcnt(12_000_000 + cnt)


Post Edited (dberkstresser) : 3/29/2007 9:05:11 PM GMT

Comments

  • T ChapT Chap Posts: 4,223
    edited 2007-03-29 20:57
    Use a space before the number in brackets when it doesn't come out right.
  • scottascotta Posts: 168
    edited 2007-03-29 21:35
    Dan,

    The following might work for you

    Scott




    ''=============================================================================
    '' @file     ThermocoupleIC_DS2760
    '' @target   Propeller1/Spin1
    '' @author   Scott Alcock
    '' @version  V1.0 - Dec 29, 2006
    '' @modes    Forground and Background
    ''=============================================================================
    ''             ┌──────────┐                           
    ''  VSS ─────│1  DS2760 │      VSS = GNS
    ''             │          │  
    ''  VDD ─────│2         │      VDD = +5V
    ''             │          │                          
    ''  Dq  ─────│3         │                 
    ''             └──────────┘
    
     
    CON
      CELSIUS       = $00
      FAHRENHEIT    = $01
      TYPEK         = $00
      TYPEJ         = $01
      TYPET         = $02
      
    OBJ
      ow            : "OneWire"
     
    VAR
      long Stack[noparse][[/noparse]48]'STACK IS NOT TUNED
      byte background
      byte units,thermocouple_type,sample_count,data_pin
      Word sign                                     ' TC sign bit
     
              
    PUB Setup(background,units_,thermocouple_type_,sample_count_,data_pin_)
      units             := units_ 
      thermocouple_type := thermocouple_type_ 
      sample_count      := sample_count_
      data_pin          := data_pin_
    
      if background==0
        InitializeCom
        background:=false
      else
        background:=true
        cognew(MainCog(temperature_), @Stack)
     
    PUB SetUnits(unit)
      units:=unit    
    
    PUB Temperature:temp | c
      c:=Sample
      if units==CELSIUS
        temp:=c
      else        
        temp:=toF(c)
    
    
    
    
    PRI MainCog(temperature_)|c 
      InitializeCom
      repeat
        c:=Sample
        if units==CELSIUS
          word[noparse][[/noparse]temperature_]:=c 
        else        
          word[noparse][[/noparse]temperature_]:=toF(c)
        Pause(10000) 
     
    PRI InitializeCom
      ow.start(data_pin)                                     ' start 1-wire object, pin 0
      
    PRI toF(c_):retv
      return ((c_ * 9) / 5 + 32 )      
    
        
    PRI Sample : c
        c:=0
        repeat sample_count
           c+=doSample(Calibrate(Sample_TC_Volts,Sample_CJ_Temp)) 
        c:=c/sample_count
      
    PRI doSample(cjComp): c | p,plo,phi,val
      c     := 22                                           ' default to room temp
      plo   := 0                                            ' low entry of table
      phi   := lookupz(thermocouple_type:1023, 1023, 400)    
      val   := KLookup(phi)                                 ' check max temp   * 2
    
      IF (cjComp > val) 
        'error:= 1                                   ' out of range
      ELSE
        repeat
          p   := (plo + phi) / 2              ' midpoint of search span
          val := KLookup(p)              ' read value from midpoint  * 2
          IF (cjComp == val) 
            quit                                    ' found it!
          ELSEIF (cjComp < val) 
            phi:= p                          ' search lower half
          ELSE
            plo:= p                          ' search upper half
          IF ((phi - plo) < 2)              ' span at minimum
            p:= plo
            quit
        c:= p
    
    
    PRI Calibrate(tCuV,tmpCJ):cjComp
        cjComp:=KLookup(tmpCJ )
        IF sign
          'TC below cold junction 
          IF (tCuV < cjComp) 
            cjComp:= cjComp - tCuV
          ELSE
            cjComp:= 0                              ' limit to 0C
        ELSE
          'TC above cold junction  
          cjComp:= cjComp + tCuV                            
    
     
    
    PRI Sample_TC_Volts:tCuV 
      tCuV:=  SWReadWord($0E) 'read tc volts
      sign:= (tCuV & %0100000000000000)>>14
      tCuV:= (tCuV >> 3)
      IF sign 
        tCuV:= tCuV | $F000
      tCuV:= ABSv (tCuV *$0F + ((tCuV*$A0)/$FF))    ' tCuV = ABS tCuV */ 4000   =  15.625 uV
      
      
    PRI Sample_CJ_Temp:tmpCJ 
      tmpCJ:=  SWReadWord($18)'read temp
      IF ((tmpCJ & %0100000000000000)>>14)          ' check sign
        tmpCJ:= 0                                   ' disallow negative
      ELSE
        tmpCJ:= tmpCJ >> 8               ' >> 5 x 0.125 (>> 3)
    
    
    PRI Sample_VsupplyV:retv ' returns supply voltage in millivolts  
      retv:=  Sample_VsupplymV/1000
                                     
      
    PRI Sample_VsupplymV:retv ' returns supply voltage in millivolts  
      retv:=  SWReadWord($0C) 'read vin
      IF ((Sample_CJ_Temp & %0100000000000000)>>14)          ' check sign
        retv:= 0                                     ' disallow negative
      ELSE
        retv:= (retv >>5) *$04 + ((retv*$E1)/$FF)
    
    
    PRI SWReadWord(command):retv | MSB,LSB
      retv:= 0
      ow.reset
      ow.writeByte($CC)'SKIP_ROM
      ow.writeByte($69)'read 
      ow.writeByte(command)
      MSB:= ow.readByte
      LSB:= ow.readByte
      retv:=  (MSB << 8) | LSB
      
    
    PRI absv(abv):retv
        if (abv < 0)
            return -abv
        else
            return abv
    
          
    PRI KLookup(index):retv
      return lookupz(index:00000, 00039, 00079, 00119, 00158, 00198, 00238, 00277, 00317, 00357, 00397, 00437, 00477, 00517, 00557, 00597, 00637, 00677, 00718, 00758, 00798, 00838, 00879, 00919, 00960, 01000, 01040, 01080, 01122, 01163, 01203, 01244, 01284, 01326, 01366, 01407, 01448, 01489, 01530, 01570, 01612, 01653, 01694, 01735, 01776, 01816, 01858, 01899, 01941, 01982, 02023, 02064, 02105, 02146, 02188, 02230, 02270, 02311, 02354, 02395, 02436, 02478, 02519, 02560, 02601, 02644, 02685, 02726, 02767, 02810, 02850, 02892, 02934, 02976, 03016, 03059, 03100, 03141, 03184, 03225, 03266, 03307, 03350, 03391, 03432, 03474, 03516, 03557, 03599, 03640, 03681, 03722, 03765, 03806, 03847, 03888, 03931, 03972, 04012, 04054, 04096, 04137, 04179, 04219, 04261, 04303, 04344, 04384, 04426, 04468, 04509, 04549, 04591, 04633, 04674, 04714, 04756, 04796, 04838, 04878, 04919, 04961, 05001, 05043, 05083, 05123, 05165, 05206, 05246, 05288, 05328, 05368, 05410, 05450, 05490, 05532, 05572, 05613, 05652, 05693, 05735, 05775, 05815, 05865, 05895, 05937, 05977, 06017, 06057, 06097, 06137, 06179, 06219, 06259, 06299, 06339, 06379, 06419, 06459, 06500, 06540, 06580, 06620, 06660, 06700, 06740, 06780, 06820, 06860, 06900, 06940, 06980, 07020, 07059, 07099, 07139, 07179, 07219, 07259, 07299, 07339, 07379, 07420, 07459, 07500, 07540, 07578, 07618, 07658, 07698, 07738, 07778, 07819, 07859, 07899, 07939, 07979, 08019, 08058, 08099, 08137, 08178, 08217, 08257, 08298, 08337, 08378, 08417, 08458, 08499, 08538, 08579, 08618, 08659, 08698, 08739, 08778, 08819, 08859, 08900, 08939, 08980, 09019, 09060, 09101, 09141, 09180, 09221, 09262, 09301, 09343, 09382, 09423, 09464, 09503, 09544, 09585, 09625, 09666, 09707, 09746, 09788, 09827, 09868, 09909, 09949, 09990, 10031, 10071, 10112, 10153, 10194, 10234, 10275, 10316, 10356, 10397, 10439, 10480, 10519, 10560, 10602, 10643, 10683, 10724, 10766, 10807, 10848, 10888, 10929, 10971, 11012, 11053, 11093, 11134, 11176, 11217, 11259, 11300, 11340, 11381, 11423, 11464, 11506, 11547, 11587, 11630, 11670, 11711, 11753, 11794, 11836, 11877, 11919, 11960, 12001, 12043, 12084, 12126, 12167, 12208, 12250, 12291, 12333, 12374, 12416, 12457, 12499, 12539, 12582, 12624, 12664, 12707, 12747, 12789, 12830, 12872, 12914, 12955, 12997, 13039, 13060, 13122, 13164, 13205, 13247, 13289, 13330, 13372, 13414, 13457, 13497, 13539, 13582, 13624, 13664, 13707, 13749, 13791, 13833, 13874, 13916, 13958, 14000, 14041, 14083, 14125, 14166, 14208, 14250, 14292, 14335, 14377, 14419, 14461, 14503, 14545, 14586, 14628, 14670, 14712, 14755, 14797, 14839, 14881, 14923, 14964, 15006, 15048, 15090, 15132, 15175, 15217, 15259, 15301, 15343, 15384, 15426, 15468, 15510, 15554, 15596, 15637, 15679, 15721, 15763, 15805, 15849, 15891, 15932, 15974, 16016, 16059, 16102, 16143, 16185, 16228, 16269, 16312, 16355, 16396, 16439, 16481, 16524, 16565, 16608, 16650, 16693, 16734, 16777, 16820, 16861, 16903, 16946, 16989, 17030, 17074, 17115, 17158, 17201, 17242, 17285, 17327, 17370, 17413, 17454, 17496, 17539, 17582, 17623, 17667, 17708, 17751, 17794, 17836, 17879, 17920, 17963, 18006, 18048, 18091, 18134, 18176, 18217, 18260, 18303, 18346, 18388, 18431, 18472, 18515, 18557, 18600, 18643, 18686, 18728, 18771, 18812, 18856, 18897, 18940, 18983, 19025, 19068, 19111, 19153, 19196, 19239, 19280, 19324, 19365, 19408, 19451, 19493, 19536, 19579, 19621, 19664, 19707, 19750, 19792, 19835, 19876, 19920, 19961, 20004, 20047, 20089, 20132, 20175, 20218, 20260, 20303, 20346, 20388, 20431, 20474, 20515, 20559, 20602, 20643, 20687, 20730, 20771, 20815, 20856, 20899, 20943, 20984, 21027, 21071, 21112, 21155, 21199, 21240, 21283, 21326, 21368, 21411, 21454, 21497, 21540, 21582, 21625, 21668, 21710, 21753, 21795, 21838, 21881, 21923, 21966, 22009, 22051, 22094, 22137, 22178, 22222, 22265, 22306, 22350, 22393, 22434, 22478, 22521, 22562, 22606, 22649, 22690, 22734, 22775, 22818, 22861, 22903, 22946, 22989, 23032, 23074, 23117, 23160, 23202, 23245, 23288, 23330, 23373, 23416, 23457, 23501, 23544, 23585, 23629, 23670, 23713, 23757, 23798, 23841, 23884, 23926, 23969, 24012, 24054, 24097, 24140, 24181, 24225, 24266, 24309, 24353, 24394, 24437, 24480, 24523, 24565, 24608, 24650, 24693, 24735, 24777, 24820, 24863, 24905, 24948, 24990, 25033, 25075, 25118, 25160, 25203, 25245, 25288, 25329, 25373, 25414, 25457, 25500, 25542, 25585, 25626, 25670, 25711, 25755, 25797, 25840, 25882, 25924, 25967, 26009, 26052, 26094, 26136, 26178, 26221, 26263, 26306, 26347, 26390, 26432, 26475, 26516, 26559, 26602, 26643, 26687, 26728, 26771, 26814, 26856, 26897, 26940, 26983, 27024, 27067, 27109, 27152, 27193, 27236, 27277, 27320, 27362, 27405, 27447, 27489, 27531, 27574, 27616, 27658, 27700, 27742, 27784, 27826, 27868, 27911, 27952, 27995, 28036, 28079, 28120, 28163, 28204, 28246, 28289, 28332, 28373, 28416, 28416, 28457, 28500, 28583, 28626, 28667, 28710, 28752, 28794, 28835, 28877, 28919, 28961, 29003, 29045, 29087, 29129, 29170, 29213, 29254, 29297, 29338, 29379, 29422, 29463, 29506, 29548, 29589, 29631, 29673, 29715, 29757, 29798, 29840, 29882, 29923, 29964, 30007, 30048, 30089, 30132, 30173, 30214, 30257, 30298, 30341, 30382, 30423, 30466, 30507, 30548, 30589, 30632, 30673, 30714, 30757, 30797, 30839, 30881, 30922, 30963, 31006, 31047, 31088, 31129, 31172, 31213, 31254, 31295, 31338, 31379, 31420, 31461, 31504, 31545, 31585, 31628, 31669, 31710, 31751, 31792, 31833, 31876, 31917, 31957, 32000, 32040, 32082, 32124, 32164, 32206, 32246, 32289, 32329, 32371, 32411, 32453, 32495, 32536, 32577, 32618, 32659, 32700, 32742, 32783, 32824, 32865, 32905, 32947, 32987, 33029, 33070, 33110, 33152, 33192, 33234, 33274, 33316, 33356, 33398, 33439, 33479, 33521, 33561, 33603, 33643, 33685, 33725, 33767, 33807, 33847, 33889, 33929, 33970, 34012, 34052, 34093, 34134, 34174, 34216, 34256, 34296, 34338, 34378, 34420, 34460, 34500, 34542, 34582, 34622, 34664, 34704, 34744, 34786, 34826, 34866, 34908, 34948, 34999, 35029, 35070, 35109, 35151, 35192, 35231, 35273, 35313, 35353, 35393, 35435, 35475, 35515, 35555, 35595, 35637, 35676, 35718, 35758, 35798, 35839, 35879, 35920, 35960, 36000, 36041, 36081, 36121, 36162, 36202, 36242, 36282, 36323, 36363, 36403, 36443, 36484, 36524, 36564, 36603, 36643, 36685, 36725, 36765, 36804, 36844, 36886, 36924, 36965, 37006, 37045, 37085, 37125, 37165, 37206, 37246, 37286, 37326, 37366, 37406, 37446, 37486, 37526, 37566, 37606, 37646, 37686, 37725, 37765, 37805, 37845, 37885, 37925, 37965, 38005, 38044, 38084, 38124, 38164, 38204, 38243, 38283, 38323, 38363, 38402, 38442, 38482, 38521, 38561, 38600, 38640, 38679, 38719, 38759, 38798, 38838, 38878, 38917, 38957, 38996, 39036, 39076, 39115, 39164, 39195, 39234, 39274, 39314, 39353, 39393, 39432, 39470, 39511, 39549, 39590, 39628, 39668, 39707, 39746, 39786, 39826, 39865, 39905, 39944, 39984, 40023, 40061, 40100, 40140, 40179, 40219, 40259, 40298, 40337, 40375, 40414, 40454, 40493, 40533, 40572, 40610, 40651, 40689, 40728, 40765, 40807, 40846, 40885, 40924, 40963, 41002, 41042, 41081, 41119, 41158, 41198, 41237, 41276, 41315, 41354, 41393, 41431, 41470, 41509, 41548, 41587, 41626, 41665, 41704, 41743, 41781, 41820, 41859, 41898, 41937, 41976, 42014, 42053, 42092, 42131, 42169)
    
      
    PRI Pause(Period)    'Pause Period = uS
        waitcnt(clkfreq/1_000_000 * Period + cnt)
      
    
    
  • dberkstresserdberkstresser Posts: 5
    edited 2007-03-30 16:36
    Thanks so much, this is perfect.

    FYI: I had to change
    PUB Setup(background,units_,thermocouple_type_,sample_count_,data_pin_)

    to
    PUB Setup(background_,units_,thermocouple_type_,sample_count_,data_pin_)

    and comment out this:
    else
    background:=true
    cognew(MainCog(temperature_), @Stack)

    ..to get it to compile. You may want to merge the first change at least into your code.
  • dberkstresserdberkstresser Posts: 5
    edited 2007-03-30 16:41
    I also added the following to Temperature to return the cj_temp instead of 0 when cold junction > thermocouple. This way it always returns a result.

    PUB Temperature:temp | c
    c:=Sample
    if c == 0
    c:=Sample_CJ_Temp
  • Tracy AllenTracy Allen Posts: 6,660
    edited 2007-03-30 17:00
    Hi Scott,

    I noticed that the contents of the lookup table got lost in the [noparse][[/noparse] code] box. Maybe that was just my system, Firefox on a Mac? It did come through in part in the email version, so I know it is (I think) a table for the typeK thermocouple.

    A different table would be required for typeT or typeJ. Simply declaring a different type won't work, although typeT is quite close to typeK near room temperature. Dan, you asked about the table lookup and how to translate it to the server side. The steps in any software based algorithm for thermocouples are:
    1) Measure the temperature of the reference junction. (Usually a simple translation from voltage)
    2) Look up the voltage that your thermocouple would produce at that temperature in relation to an ice reference.
    This can either be a table lookup, or a polynomial calculation. Interpolate if necessary.
    The table Scott provided and the one in the Parallax docs give the voltage output as a function of Celsius temperature, for example, 39 microvolts output at +1 degree Celsius, and so on up to around 1000 degrees Celsius.
    3) Read the voltage produced by the thermocouple, which will be the temperature with the reference junction at the temperature determined in step 1.
    4) Add the voltage from step 2 to the voltage in step 3. That will give the voltage that the thermocouple would have produced if its reference junction had actually been at zero Celsius.
    5) Look up the temperature that corresponds to that voltage. For that, you either need a separate table of temperatures as a function of voltage, or you need to do a reverse lookup in the same table you used in step 2. The latter is the usual way to do it, and the algorithm is a binary search to find the value in the table that is closest to the calculated voltage. Or interpolate between two values. This can also be done by calculation using an inverse polynomial.

    There are references that have the tables for the different thermocouple types as well as both the forward and reverse polynomials. As Scott mentioned in another email, EXCEL can be a great help to convert the tables to DATA or LOOKUP.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • scottascotta Posts: 168
    edited 2007-04-01 18:29
    I did not notice the missing lookup table. I should post the entire thing
    on the object exchange when there is some free time...

    It was Firefox on Windows that did the post.

    You are correct, each thermocouple type needs a different calibration table. I think
    I have them all in a file some place.

    This is some of my first spin code. Pretty messy...

    Scott
Sign In or Register to comment.