Shop OBEX P1 Docs P2 Docs Learn Events
Keep the maximum and minimum readings from DS1620 — Parallax Forums

Keep the maximum and minimum readings from DS1620

MoskogMoskog Posts: 554
edited 2008-12-06 21:14 in BASIC Stamp
Hello again.
I am thinking about keeping the max and min readings from a ds1620 temperature device, lets say I would like to·display the coldest temperature·that was read last night, and the hottest reading the following day.
Today I send the binary numbers read from the ds1620 from the outside bs2 through the parallax tx and rx devices to my·kitchen bs2 that·computes and display the celcius reading on a lcd display.
Temperature readings are updated every second on the lcd.
So I guess.. that the easiest way to handle temperature numbers is by first converting to Kelvin. I found a description on that made by dr. Tracy Allen.
The do all computing and storing, keeping track of the max and min readings while the temperature still is in Kelvin, then converting back to Celsius only just before displaying.
Am I right in this or is there easier ways (for the bs2)·like·staying with the·ds1620 binarys all the way to handle max and minimums before displaying?

Any comments will be appreciated.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-30 16:44
    Why convert to Kelvin just to record maximum and minimums? It's easier to use the DS1620 temperature scale (9-bit signed 2's complement 1/2 degree units). First convert to 1/2 degree units starting at -128C with

    newTemp = (ds1620Temp + 128) & $1FF

    You can keep a maximum and minimum by doing

    if newTemp < minTemp then minTemp = newTemp
    if newTemp > maxTemp then maxTemp = newTemp

    minTemp has to be initialized to 255 and maxTemp has to be initialized to 0

    To convert back to a 2's complement temperature in 1/2 degrees Celsius, do

    displayTemp = newTemp - 128
  • MoskogMoskog Posts: 554
    edited 2008-11-30 20:58
    Well, Mike, thank you so much for the advice, I seem to complicate things much more then neccessary all the time.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-12-01 04:09
    The main point is that you have to add a number that makes the reading positive, because the Stamp can only interpret MAX and MIN and < and > and * and */ and ** correctly for positive numbers. Then at the end you subtract to restore the negative values.

    It can be either
    newTemp = (ds1620Temp + 546) ' to convert degC*2 to Kelvin
    or
    newTemp = (ds1620Temp + 128) ' to convert degC*2 to an intermediate positive definite form.
    Eiither one, or a host of other constants, will work just fine. Then subtract the same constant after the MIN and MAX calculations. Or the adjusted constant after, say, a Fahrenheit conversion.

    A shorter way to calculate the minimum and maximum temperatures uses the Stamp's own MIN and MAX operators.
    minTemp = minTemp MAX newTemp
    maxTemp = maxTemp MIN newTemp
    It may seem strange to use the MIN operator to calculate the maximum temperature and the MAX operator to calculate the minimum, but that is because of the way the Stamp operators are defined, more like old analog computer floor and ceiling operators (see the manual). The values have to be initialized as Mike says.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • MoskogMoskog Posts: 554
    edited 2008-12-01 18:01
    Thanks, you are right, of course it doesn't neccessary have to be Kelvin. I never knew about the MAX MIN operators, and now, when looking them up in the Reference manual, I guess I never would figure out I could use them as simple as you explain in your reply above.
    Just one question, the variable "ds1620Temp" thats the whole string coming out of the ds1620, I mean all 9 bits, right?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-01 18:15
    Yes, all 9 bits (in a 16 bit word) like %0000000SDDDDDDDD where S is the sign bit and D are the 2's complement data bits.
  • MoskogMoskog Posts: 554
    edited 2008-12-01 18:25
    Ok. Thank you so much for that quick reply!
  • MoskogMoskog Posts: 554
    edited 2008-12-04 20:48
    Well now I am in real trouble. I tried the programming examples from both you Mike Green and
    Tracy Allen. I been watching the code and the LCD for several nights but cant seem to figure out whats wrong.
    I have one question, in the "newTemp = (theTemp + 128) & $1FF", what does "& $1FF" really do?

    When the temperature is 0 Celsius the display shows +0.0C (temperature now), the Max shows +0.0C but the Minimum toggles between +0.0C and +64.0C for each loop of the code.

    If the temperature goes below zero then the Max is toggling.

    Why does it toggle?

    I have a very stripped code attached (took away all other code not related to temperature) just to make it more readable. On this code you can simulate 0C, +25C and -25C and use the debug screen.

    I am sure there are some real bad errors but I cant figure out what it is.

    Any help will be appreciated!
  • Mike GreenMike Green Posts: 23,101
    edited 2008-12-04 21:07
    The first thing is that I gave you the wrong formula. It should be "newTemp = (theTemp + $100) & $1FF"

    What's happening is that the DS1620 provides values from $100 to $1FF corresponding to temperatures from -128.0C to -0.5C and values from $000 to $0FF corresponding to temperatures from 0C to +127.5C. What you want is a positive number ranging from $000 to $1FF corresponding to temperatures from -128.0C to +127.5C. The idea is that you shift the whole number range upwards by 128C. Unfortunately, I forgot that the units were in 0.5C rather than 1.0C and gave you the wrong constant to add. The $1FF keeps everything in 9-bit arithmetic by masking off the other bits.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-12-05 08:19
    Sorry, I left out one step too. The sign has to be extended in order for the addition to work correctly. By that I mean that the 1 in $1xx is the sign bit in 9-bit arithmetic, but to make it 16 bit arithmetic, $1xx needs to become $FFxx. $FF replaces the $1. Here is how to do that, and then to multiply times 5 to convert it to degrees Celsius, and offset to Kelvin to do Min/Max.
      x.byte1 = -x.bit8             ' extend the sign to the entire high byte, which will become all zeros or all ones
      degC=x*5                      ' convert to 'C*10 (resolution 0.5 'C)
                                    ' & show the result on the PC screen:
      debug "degC=",rep "-"\degC.bit15,abs degC/10,".",dec1 abs degC,CR
      Kelvin = degC + 2732   ' to  Kelvin *10
      Kmax = Kmax MIN Kelvin
      Kmin = Kmin MAX Kelvin
      Cmax = Kmax - 2732   ' back to degC*10
      Cmin = Kmin - 2732 
      debug "Cmax=",rep "-"\Cmax.bit15,abs Cmax/10,".",dec1 abs Cmax,CR
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 12/5/2008 8:24:42 AM GMT
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-12-05 16:46
    One more point. My approach converted the value to 16 bit arithmetic, while Mike's left it in 9 bit arithmetic, and it goes to show that there is more than one way to do these things.

    In Mikes,
    newTemp = (theTemp + $100) & $1FF
    it is easy to see that if theTemp from the sensor is positive, the value of newTemp will be in the range of $100 to $1FF, that is to say, 256 to 511 in decimal. How about when theTemp going in is negative? Say $1FF (-0.5 degree), then newTemp = ($1FF + $100) & $1FF = $2FF & $1FF = $0FF, which is 255 in decimal, which is as it should be, one less than the shifted value of zero. The negative temperature values fall into place from 0 to 255, less than the originally positive values. The &1FF is necessary to drop the carry/borrow and keep it in 9 bits. When converted to 16 bits, the "&" is not necessary, because the 16 bit word automatically drops the carry/borrow.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • MoskogMoskog Posts: 554
    edited 2008-12-06 10:48
    Well, now things seem to be working here, thanks to you again, Mike Green and Tracy Allen. I ended up with using the 9-bit version, not extended to 16-bit, but with good help·from code examples from both of you. I have to admit there has been lots of frusteration and I been close to give up, but gave it another try this saturday morning. I found out that the main problem was due to some bad errors in the code made by myself, like statements that came in wrong order, things should have been done after that, not before, and variable names that sometimes was too similar and too easy to mix.

    Today its about -13 Celsius here in Norway and I had to get out and heat the ds1620 with a heat gun, then get back into the kitchen to check if things worked correctly. The Low did stay at -13.5C and the High·climed·to +7.0C and stayed there while the ds1620 temperature again fell down to -12.5C like it is now.

    ·bs2Weather.jpg

    Testing on the kichen table, Up left on LCD is temperature now, "Nord-øst" means north-east wind direction. Next line "Vindstille" means calm, 0.0 meters/sec.·Line 3·shows Max wind speed.·Line 4·shows Max temperature and Min temperature, (High - Low).

    The bs2 is almost full,· no more space for further program extentions. Maybe a bs2e would be a good solution?

    Thanks anyway for all help this time!
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2008-12-06 17:21
    It sounds pretty chilly there in Norway! Here near San Francisco it is 20 degC. (68 degF). My wife and I went to the mountains last weekend and were hiking up to an alpine lake in short sleeves, with hardly a remnant of an earlier snowstorm. We need snow here soon!

    If you do go for a multislot Stamp, I'd recommend the BS2pe over the BS2e. The BS2pe has several additional commands that you may find worthwhile, among them, ones that make interface to I2C and OneWire devices easy, and also the STORE command that allows your program to READ and WRITE across slot boundaries, and also more scratchpad RAM for variables.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • MoskogMoskog Posts: 554
    edited 2008-12-06 17:48
    Tracy..

    Ok, BS2pe sounds to be a good choice, thanks so much for that advice.

    Still -13 C here.. and a little windy too, felt good to stay inside and do pbasics today. One feet of snow in the valley, up to three or more in the hills. That's today's weather report from mid-Norway.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-12-06 19:03
    Moskag

    Could you Please·post your working code for this project

    Thank so much




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • MoskogMoskog Posts: 554
    edited 2008-12-06 21:14
    sam..

    As this project is still under construction I'm not sure I will post the code on the forum yet, but I can send it to you as a private message so you can take a look.
Sign In or Register to comment.