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

ds1726

stin93rstin93r Posts: 3
edited 2005-05-18 15:46 in BASIC Stamp
The ds1726 is a step or 2 up from the ds1620, I'm working on trying to interface it with my bs2. Problem is the temperature conversions are taking place, but I don't think that I'm getting the right numbers, or enough bits are being read in. I have to divide DSDATA by 17 to get it anywhere close to the actual temperature, and I'm not getting any resolution for a fraction of a temperature. The datasheet for the 1726 is available here. I'm new to this kind of thing and would really appreciate any help I can get.

' {$STAMP BS2}
' {$PBASIC 2.5}

'PIN CONSTANTS
DQ CON 0
CLK CON 1
RST CON 2
DSDATA  VAR Word
SIGN    VAR DSDATA.BIT11
T_SIGN  VAR Bit

RCONFIG CON $AC
WCONFIG CON $0C
CPU     CON %10
NOCPU   CON %00
ONESHOT  CON %01
CONT    CON  %00

STARTC  CON  $51
STOPC   CON  $22
RTEMP   CON  $AA

RHIT    CON  $A1
WHIT    CON  $01
RLOT    CON  $A2
WLOT    CON  $02
TEMP VAR Byte
LOW RST
PAUSE 100
HIGH RST
SHIFTOUT DQ,CLK,LSBFIRST,[noparse][[/noparse]WCONFIG,CPU+CONT]
LOW RST
PAUSE 100
HIGH RST
SHIFTOUT DQ,CLK,LSBFIRST,[noparse][[/noparse]STARTC]
LOW RST
AGAIN:
PAUSE 1000
DEBUG CLS
HIGH RST
SHIFTOUT DQ,CLK,LSBFIRST,[noparse][[/noparse]RTEMP]
SHIFTIN DQ,CLK,LSBPRE,[noparse][[/noparse]DSDATA\12]
LOW RST
T_SIGN = SIGN
DSDATA = DSDATA/17
DEBUG SIGN
IF T_SIGN  = 0 THEN NO_NEG1
 DSDATA = DSDATA|$FF00
 DEBUG SDEC DSDATA
NO_NEG1:
 DEBUG SDEC DSDATA," DEGREES C",CR
  DSDATA = (DSDATA */ $01CC)
IF T_SIGN = 0 THEN NO_NEG2
 DSDATA = DSDATA|$FF00
NO_NEG2:
 DSDATA = DSDATA + 32
 DEBUG SDEC DSDATA," DEGREES F",CR
 SHIFTOUT DQ,CLK,LSBFIRST,[noparse][[/noparse]STOPC]
GOTO AGAIN

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-05-17 16:39
    Hello,

    ·· I see nobody has replied to this thread.· I don't have a DS1726 or I would test your code.· I use the DS1620 so I have code for it, as well as several to test.· Oddly the 1726 is listed as a replacement for the DS1620, however the DS1620 is capable of .5 degree C measurements, and the DS1726·only 1 degree.· Also the DS1620 appears to have a wider temp range, unless I am missing something.· If you cannot get it working properly, it might be easier to try using the DS1620 instead, if it fits your requirements.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Fe2o3FishFe2o3Fish Posts: 170
    edited 2005-05-17 20:59
    First off stin93r, look at Figure 4 and Table 4 of the datasheet. You'll notice that of
    the 12-bits that you'll read in, the lower 4 are your fractional temperature bits. Alas,
    the Basic Stamps only handle integers and the arithmetic is UNSIGNED! Alas, you're
    reading a SIGNED FIXED POINT number from your 1726 so you'll have to do a little bit
    twiddling to get it right.

    Second, dividing a negative number with an unsigned division operator will definitely
    get you some very wrong numbers! Don't do it! Your best bet is to do an ABS() on
    and turn it into a positive number. Remember to save that sign bit though for your
    output!

    Thirdly, since the BS2 doesn't do floating point you'll need to do more bit twiddling
    to get your fractional temperature -- just dividing your number by 16 (17 was incorrect)
    won't get you anything but the integer portion of the temperature.

    In pseudo-pbasic, what you'll need to do is this after your SHIFTIN
         if (SIGN <> 0) then
             DSDATA = DSDATA | $F800       ' properly sign extend a 12-bit number
             DSDATA = ABS(DSDATA)           ' turn into a positive #
             DEBUG "-", DSDATA>>4,".",DEC4 (DSDATA & $0f)*625," degrees C"
         else
             DEBUG "+", DSDATA>>4,".",DEC4 (DSDATA & $0f)*625," degrees C"
         endif
    
    


    I think you tried to use some of the 1620 code from the N&V 1620 article.
    Keep in mind that the 1620 only had 9-bits of data with only 1-bit for fractional
    temp. while the 1726 has 12-bits of data with 4-bits of fractional temp.

    I'll leave the conversion to Fahrenheit as an exercise for the reader.

    Chris, cover me on that code above. My PBASIC ain't so hot. There may be
    correct ways to do this.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Rusty-
    --
    Rusty Haddock = KD4WLZ = rusty@fe2o3.lonestar.org
    **Out yonder in the Van Alstyne (TX) Metropolitan Area**
    Microsoft is to software what McDonalds is to gourmet cooking
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-05-17 21:09
    Rusty,

    ·· Regardless of the code, you went farther than I did!· smile.gif·· When it gets complex, I usually need the part to work things out and test.· But all your points are valid.· The value is two's-compliment, although in looking at the data sheet I didn't notice myself at that time the 4-bit fractional value.· I think code verification with math like that would be best left to the math gurus, such as Tracy Allen or Jon Williams.· I'm no math wiz when needing to work with these numbers on the BASIC Stamp myself...tongue.gif·· Thanks though for looking more into it though.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-05-17 22:35
    Here is one compact way to convert the data to tenths of a degree Celsius, with sign extended, and display it:

    ' DSDATA comes from reading the sensor in 16 counts per degree C
      DSS VAR DSDATA.BIT15  ' sign bit   
      DSDATA.NIB3=DSDATA.BIT11    ' extend sign from 12 to 16 bits 
      DSDATA = -DSS ^ (ABS DSDATA */ 160) + DSS    ' degrees C times 10, both positive and negative.
      DEBUG SDEC DSDATA, TAB
      DEBUG REP "-"\DSS, DEC ABS DSDATA/10, ".", DEC1 ABS DSDATA, CR
    



    notes:
    -- DSS is an alias name for the sign bit of the twos complement number
    -- making nib3 equal to bit11 extends the sign to 16 bits.
    -- The resolution of the sensor is 1/16 degree. The */ 160 operator makes it 1/10 degree.
    The */ 160 operator works on the absolute value of the number. The operations with DSS restore the sign bit,
    based on the definition of negation in twos complement arithmetic.
    -- The first DEBUG displays the signed value of degrees times 10.
    -- The second DEBUG displays the value with the decimal point to 0.1 degree, with sign.

    I haven't used the DS1726, but I think this will work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Fe2o3FishFe2o3Fish Posts: 170
    edited 2005-05-18 03:35
    From the math perspective.... you can't divide a negative integer w/ unsigned divide
    and have it come out right. Oh, and shifting a negative number doesn't work as division
    either so.... Best thing to do is to just get the absolute value, hold the sign off somewhere,
    and dink with the value from there.

    Most everyone should be able to figure out everything else there except for that magical
    625. Where did THAT come from? Hehehe --- well, there's 4 fractional BITS so that
    Least Significant Bit has a value of 1/16 which is 0.0625. So, that fractional nybble is
    (0 to 15) * 0.0625 or (0 to 15) * 625/10000. Now where's that /10000? Don't need it!
    We just want the four decimal digits and that's where the DEC4 comes in... it zero left fills.

    While Tracy's code will probably work, I believe that mine will give 'stin93r' all available
    resolution of the DS1726 although who REALLY wants more than a single
    decimal digit of temperature?

    Sorry, it probably took me more time than most 'cause I tend to think in 'C', not PBASIC
    and I had to do the translation. smile.gif 'sides, I like math puzzles.

    Oh, and I just noticed that conversion time for 12-bits of precision is about 750milliseconds
    if that makes any difference.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Rusty-
    --
    Rusty Haddock = KD4WLZ = rusty@fe2o3.lonestar.org
    **Out yonder in the Van Alstyne (TX) Metropolitan Area**
    Microsoft is to software what McDonalds is to gourmet cooking
  • stin93rstin93r Posts: 3
    edited 2005-05-18 03:56
    Thanks for the info everyone. I still have quite a bit to learn with pbasic and basic stamps in general.
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-05-18 05:08
    Oh, I should have said,
    DSDATA.NIB3=-DSDATA.BIT11····'·extend·sign·from·12·to·16·bits
    in the program I posted. I left off the minus sign. To extend the sign from 12 to 16 bits, it needs to set all 4 bits of the high nibble equal to 1, when bit 11 of the A/D conversion is equal to one, otherwise leave them all equal to zero.

    I agree that Rusty's approach will work too, with 0.0625 degrees per bit. There are often many several approaches these problems, stin93r, and the method you choose will depend on what else you need to do with the numbers, and how well you understand the methods. The methods are tools--More tools, more experience, more fun!

    Note that the older DS1620 can give better than 0.01 degree, by reading its high resolution registers. The resolution of the main registers is 0.5 degree C.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • stin93rstin93r Posts: 3
    edited 2005-05-18 12:15
    I understand where you're getting the 1/16th of a degree. I can see why we're looking for the sign on bit 15 when we only have 12 bits read in. I've got a few other questions though

    1.) What is the dsdata.nib3? I've been looking through pbasic documenutation and didn't quite understand what a nib is.
    2.) DSDATA = DSDATA | $F800 (Not quite understanding the | $F*00, could someone clarify please?)
    3.) DSDATA & $0f (Same as number 2)

    Sorry for the questions that probably seem like nothing to you. I've done some research and still have a hard time understanding what's going on with those 3 statements. I appreciate any help I receive.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-05-18 13:08
    1. NIB3 is the highest nibble of a word value
    2. Setting the upper bits corrects the value when it is negative; this is required to use the number later
    3. This operation is isolating the lower nibble of that value

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2005-05-18 15:46
    1. Stamps can work with bits, nibbles, bytes and words. A word is a 16 bit variable that can be broken down into chunks of 2 bytes, 4 nibs or 16 bits. The Stamp allows you to refer to the chucks by name, as the four nibbles of word variable DSDATA can be referenced as DSDATA.NIB0, DSDATA.NIB1, DSDATA.NIB2 and DSDATA.NIB3. You can use those names directly in formulas, like,
    DSDATA.NIB3 = - DSDATA.BIT11
    or
    IF DSDATA.NIB3 = DSDATA.NIB2 THEN ....
    You can also give those chucks alias names, as in
    DSC VAR DSDATA.NIB3
    or
    DSS VAR DSDATA.BIT15
    and then use the alias names in place of the business with the dot.

    2. Xvar | $F800 The pipe character "|" is an operator that means bitwise OR in STAMPese. It is a mask, and the result will have all the bits from number 11 to number 15 forced to 1, and bits 0 to 10 will have whatever value they have in Xvar.

    3. Xvar & $0F The "&" is the operator for bitwise AND. A mask, and the result will have all bits from 4 to 15 forced to zero, and bits 0 to 3 retain whatever value they had in Xvar.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.