Shop OBEX P1 Docs P2 Docs Learn Events
Programming help — Parallax Forums

Programming help

GuidoGuido Posts: 195
edited 2004-09-27 03:43 in BASIC Stamp
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

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-26 18:09
    Since the DS1620 uses a chip select line (called Reset in the program), you can use more than one device -- what you need to do is modify the code so that you can define the Reset pin before calling a subroutine.

    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
  • GuidoGuido Posts: 195
    edited 2004-09-27 00:09
    Jon,

    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
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-27 02:02
    You may want to spend some time with our "What's A Microcontroller?" book if you're new to programming embedded micros -- the material that follows will make more sense then.

    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
  • Tracy AllenTracy Allen Posts: 6,656
    edited 2004-09-27 03:43
    Guido,

    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.

    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
      tempIn.byte1 = -tempIn.bit8   ' extend sign
      tempC=tempIn*5       'Celsius value *10, resolution 0.5 degree
      tempF = tempC + 1000 * 9 / 5 - 1800 + 320
      RETURN
    



    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

    DO
        reset=1 : GOSUB GetTemperature
       TFmax1 = (tempF + 1024) MIN (TFmax1 +1024) - 1024
       TFmin1 = (tempF + 1024) MAX (TFmin1 +  1024) - 1024
         reset=2 : GOSUB GetTemperature
       TFmax2 = (tempF + 1024) MIN (TFmax2 +1024) - 1024
       TFmin2 = (tempF + 1024) MAX (TFmin2 +  1024) - 1024
     LOOP
    



    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
Sign In or Register to comment.