Stamp Works ex29 Temperature Measurment
Blake Koch
Posts: 39
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.
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
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.
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!
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
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!