Shop OBEX P1 Docs P2 Docs Learn Events
Stamp Works ex29 Temperature Measurment — Parallax Forums

Stamp Works ex29 Temperature Measurment

Blake KochBlake Koch Posts: 39
edited 2013-06-01 10:44 in Learn with BlocklyProp
Somewhat new at this programing thing and I have 2 question about the scaling of ds1620 chip. I have read on the chips data sheet the unit is capable of reading temperatures from -55C to 125C in .5 increments. Shifting in 9 bits of info at a time. I see the upper Byte is the Sign and the Lower Byte in the Scaled value. This is where I run into trouble with the math.

Read_DS1620:
HIGH Reset ' alert the DS1620
SHIFTOUT DQ, Clock, LSBFIRST, [RdTmp] ' give command to read temp
SHIFTIN DQ, Clock, LSBPRE, [tempIn\9] ' read it in
LOW Reset ' release the DS1620

#IF _Testing #THEN
tempIn = %111111111 ' -0.5 C
#ENDIF

tempIn.BYTE1 = -sign ' extend sign bit
tC = tempIn * 5 ' convert to tenths


Question 1 In the Subroutines ( tC = tempIn * 5 ) when I debug tempIn or tC without using byte1 or 0 modifier I get a value 65535 WHY the next(9) binary number should be 512 ?
Question 2 how is the BS2 chip performing the math? tC=[(debug value) 65535] * 5 would be outside the BS2 chips ability. How does the BS2 chip know to perform math on only the lower 8bits (Byte0) if there is no modifier in the equation ?

I know there's a simple answer I just cant see it.
Thanks for any help.

Comments

  • JonnyMacJonnyMac Posts: 9,158
    edited 2013-05-31 15:14
    Wow, it's been ages since I wrote StampWorks -- and I'm glad it's still of use.

    The variable tempIn is a Word (16 bits) which is being extended by this line:

    tempIn.BYTE1 = -sign

    When all bits in a word are set to 1 the maximum value is 65535. The output from the DS1620 is only 9 bits, but we're extending it to 16 so that we can work with the sign bit (bit15) in tempIn. Multiplying the raw value from the DS1620 by 5 converts the units to 10ths of a degree. If you use SDEC instead of DEC with DEBUG you'll get answers that make more sense.
  • Ken GraceyKen Gracey Posts: 7,395
    edited 2013-05-31 16:11
    Oh my, Blake. It seems you have summoned the "King of Stamps" to answer your question. JonnyMac knows much more than he shares about this particular project. StampWorks and the accompanying hardware is his design. JonnyMac has measured more temperatures with the DS1620 than anybody I know, maybe except Tracy Allen.

    You'll find lots of professional programmers on the forums who are willing to help out. We are thankful to have the best support community in existence because of them.

    Welcome Blake Koch to the Parallax forums!
  • Blake KochBlake Koch Posts: 39
    edited 2013-06-01 07:48
    WOW that simple!
    Thank you. The SDEC made this part make sense.

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

    n VAR Word
    v VAR Word
    hb VAR v.BIT8

    v = %111111111

    DEBUG "Bin hb: ", BIN hb," SDec hb: ", SDEC2 hb,CR
    DEBUG CR,"Befor v.byte1 = -hb",CR,CR
    DEBUG "V Bin Byte 1: " , BIN8 v.BYTE0,CR,"V Bin Byte 0: ",BIN8 v.BYTE1,CR, "SDEC v: ",SDEC v
    v.BYTE1 = -hb
    DEBUG CR,"After v.Byte1 = -hb(n.bit8)",CR,CR
    DEBUG "V Bin Byte 1: " , BIN8 v.BYTE0,CR,"V Bin Byte 0: ",BIN8 v.BYTE1,CR, "SDEC v: ", SDEC v,CR
    DEBUG "Bin V: ",BIN v, CR,"Bin hb: ",BIN hb,CR,CR
    DEBUG "n = v * 5",CR,CR

    DEBUG "n before n = v * 5: ", SDEC n,CR
    n = v * 5
    DEBUG "n after n = v * 5: ", SDEC n,CR
    DEBUG "Bin n Byte0: ", BIN n.BYTE0,CR,"Bin n Byte1: ",BIN n.BYTE1, CR ,"bin(16) n: ",BIN16 n,CR
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2013-06-01 09:18
    I'll have to chime in too. In answer to question #2, multiplication works fine for both positive and negative numbers when they are in twos-complement form. For example, $FFFF represents -1 in a 16 bit field. And $FFFF * $FFFF using standard binary multiplication becomes $FFFE0001 when it is extended into a 32 bit field. But when truncated back to 16 bits, that is simple 1, the correct answer for -1 * -1. Aways true. So for example -1 * 5 is $FFFF * $5 = $0x4FFFB which when truncated is $FFFB, and verify that $FFFB+5 = $50000, which truncates to zero.

    Addition and subtraction also work correctly with twos complement numbers. However division and modulus do not. For example, it you want to convert tC to °F, don't do the math in this order, dF = dC * 5 / 9 + 32. That won't work for negative numbers due to the division. Instead do something like dF = (dC+900) * 5 / 9 - 468.

    And for display of the decimal point in one line, that too involves a division:
    DEBUG SDEC tC ' displays e.g. -15
    DEBUG REP "-"\tC.bit15, DEC ABS tC / 10, ".", DEC1 ABS tC 'displays e.g. -1.5

    The BASIC Stamp has another operator, ** that returns the upper 16 bits of a 32 bit multiply, and that is quite useful at times.

    Ken, speaking of temperature, I was revisiting the youtube video you narrated, when Paul Baker roasted a Propeller Demo Board in the climate test chamber. Very entertaining!
  • Blake KochBlake Koch Posts: 39
    edited 2013-06-01 10:44
    Again thanks this really cleared up the math function.
Sign In or Register to comment.