Programming help
Guido
Posts: 195
I am trying to shorten my program. I would like to hook up two more temperature sensors and monitor, save low and high temperatures.
Here is a sample program and what I need help with is a way that I can use this as a goto statement for all DS1620......Somehow Identifiying each in a variable....
Thank You for your help!!!!
A very newbeeeeeeeeeeee
Main:
GOSUB GetTemperature ' READ THE DS1620
GOTO Main
END
'
DS1620init:
HIGH Reset ' ALERT THE DS1620
SHIFTOUT DQ,Clock,LSBFirst,[noparse][[/noparse]WrCfg,%10] ' USE WITH CPU; FREE-RUN
LOW Reset
PAUSE 10
HIGH Reset
SHIFTOUT DQ,Clock,LSBFirst,[noparse][[/noparse]StartC] ' START CONVERSIONS
LOW Reset
RETURN
GetTemperature:
HIGH Reset ' ALERT THE DS1620
SHIFTOUT DQ,Clock,LSBFIRST,[noparse][[/noparse]RdTmp] ' GIVE THE COMAND TO READ TEMP
SHIFTIN DQ,Clock,LSBPRE,[noparse][[/noparse]tempIn\9] ' READ IT IN
LOW Reset ' RELEASE THE DS1620
tSign = sign ' SAVE SIGN BIT
tempIn = tempIn/2 ' ROUND TO WHOLE DEGREES
IF tSign = 0 THEN NoNeg1
tempIn = tempIn | $FF00 ' EXTEND SIGN BITD FOR NEGATIVE
NoNeg1:
tempC = tempIn ' SAVE CElSIUS VALUE
tempIn = tempIn */ $01CC ' MULTIPLY BY 1.8
IF tSign = 0 THEN NoNeg2 ' IF NEGATIVE, EXTEND SIGN BITS
tempIn = tempIn | $FF00
NoNeg2:
tempIn = tempIn + 29 ' FINISH C TO F CONVERSTIONS
tempF = tempIn ' SAVE FAHRENHEIT VALUE
Here is a sample program and what I need help with is a way that I can use this as a goto statement for all DS1620......Somehow Identifiying each in a variable....
Thank You for your help!!!!
A very newbeeeeeeeeeeee
Main:
GOSUB GetTemperature ' READ THE DS1620
GOTO Main
END
'
DS1620init:
HIGH Reset ' ALERT THE DS1620
SHIFTOUT DQ,Clock,LSBFirst,[noparse][[/noparse]WrCfg,%10] ' USE WITH CPU; FREE-RUN
LOW Reset
PAUSE 10
HIGH Reset
SHIFTOUT DQ,Clock,LSBFirst,[noparse][[/noparse]StartC] ' START CONVERSIONS
LOW Reset
RETURN
GetTemperature:
HIGH Reset ' ALERT THE DS1620
SHIFTOUT DQ,Clock,LSBFIRST,[noparse][[/noparse]RdTmp] ' GIVE THE COMAND TO READ TEMP
SHIFTIN DQ,Clock,LSBPRE,[noparse][[/noparse]tempIn\9] ' READ IT IN
LOW Reset ' RELEASE THE DS1620
tSign = sign ' SAVE SIGN BIT
tempIn = tempIn/2 ' ROUND TO WHOLE DEGREES
IF tSign = 0 THEN NoNeg1
tempIn = tempIn | $FF00 ' EXTEND SIGN BITD FOR NEGATIVE
NoNeg1:
tempC = tempIn ' SAVE CElSIUS VALUE
tempIn = tempIn */ $01CC ' MULTIPLY BY 1.8
IF tSign = 0 THEN NoNeg2 ' IF NEGATIVE, EXTEND SIGN BITS
tempIn = tempIn | $FF00
NoNeg2:
tempIn = tempIn + 29 ' FINISH C TO F CONVERSTIONS
tempF = tempIn ' SAVE FAHRENHEIT VALUE
Comments
For example, if you had two devices and P2 and P3 were used as the respective reset lines, you could do something like this:
rstPin··· VAR··· Nib
...
Setup:
· rstPin = 2························ ' select device
· GOSUB Init_DS1620
· rstPin = 3
· GOSUB Init_DS1620
Main:
· rstPin = 2
· GOSUB Get_Temp
· rstPin = 3
· GOSUB Get_Temp
You'll also have to change your DS1620 subroutines to use rstPin (variable) instead of·Reset (constant).· Change these lines:
HIGH Reset
LOW Reset
... to:
HIGH rstPin
LOW rstPin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
Thank You very much for your help. I am a newbee as mentioned and really appreciate this forum. I actually understand what you have done with reseting the DS1620, but how do you handle the Variable for saving high temp and low temp per DS1620.....The final statement as TempF=TempIn.......????
Also just courious playing around with the DS1620, what gauge wire is recommended for lengths up to 20 feet. I have noticed that the DS1620 will not function if the wire gauge is to small. I think this is because of the 5 vdc drop over smaller wgauge wire.
Thanks Again Jon
Guido
Let's say you have those same two devices.· You need arrays to hold the min and max temps:
minTmp··· VAR··· Word(2)
maxTmp··· VAR··· Word(2)
Then you can do something like this:
Main:
· FOR sensor = 0 TO 1
··· LOOKUP sensor,·[noparse][[/noparse]3, 4], rstPin
··· GOSUB Get_Temp
··· IF·(tempF < minTmp(sensor)) THEN
····· minTmp(sensor) = tempF
··· ENDIF
··· IF·(tempF·> maxTmp(sensor)) THEN
····· maxTmp(sensor) = tempF
··· ENDIF
· NEXT
· ....
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
The math calculation in your program needs a little help, as the negative temperatures won't come out right. The */ operator does not work on negative numbers! The following returns both the Celsius and Fahrenheit temperatures for both positive and negative temperatures but only takes 3 lines of code.
It extends the sign by making the upper byte all ones if the 9th bit is a one. It keeps 1/2 degree Celsius resolution, so if you print out tempC, it will show the units of 0.5 degree. Then from that it calculates Fahrenheit, also to resolution of 0.1 degree. It deals with the negative by adding 1000 to the Celsius, before doing the division (which is what fails on negatives) and then subtracting out that factor (1000*9/5=1800).
The main loop can go something like the following
TFmax1 stores the maximum for the first sensor, TFmin1 the minimum, and TFmax2 and TFmin2 for the second sensor. Look up the MIN and MAX operators in the help file. They might seem counterintuitive, because you have to use the MIN operator to find the maximum and vice versa. And again you have to use an offset trick (+/-1024) to make the comparisons on positive numbers, to make it right even when the temperature is negative.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com