Shop OBEX P1 Docs P2 Docs Learn Events
DS1620 and celsius degrees below 0 ? — Parallax Forums

DS1620 and celsius degrees below 0 ?

Mikael SMikael S Posts: 60
edited 2012-02-19 00:43 in Propeller 1
With the code below i read the temperature in celsius from a DS1620, and it works fine until the temperature goes under 0 degrees. Then i get a strange result!
Im going to present the tempreature on a LED display, its because of that i have to separate the digits.
 Repeat
    Temperature.Start(0,1,2)
    cData := Temperature.gettempc
    
    tensValue := (cData/100)
    onesValue := (cData/10)-((cData/100)*10)

    debug.start(31, 30, 0, 9600)
    debug.dec(tensValue)
    debug.dec(onesValue)
    debug.str(string(" C"))
    debug.tx(13)

Comments

  • Mark_TMark_T Posts: 1,981
    edited 2012-02-16 14:09
    You need to test if the value is negative, if so output a '-' character and set the temperature positive for the digit extraction code. Something like
        if cData < 0
          cData := -cData
          debug.tx ('-')
        ....
        ....
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2012-02-16 15:18
    The DS1620 returns its data as 9-bit twos complement with a resolution of 0.5 °C. I don't know what additional your "temperature" object does to massage the reading, but it should extend the sign from the ninth bit up to fill the 32 bit long, and multiply the raw reading times 5 to return the value in units of 0.5 degree. At that point most DEC methods should display it correctly as either a positive or a negative number. There may be something else going on. I don't understand what the tensValue and onesValue variables are for.
  • JonnyMacJonnyMac Posts: 9,197
    edited 2012-02-16 15:26
    @Mikael: As Tracy points out, without knowing what your object is doing with the raw value from the DS1620 guidance can be tricky. If you have the raw 9-bit value from the DS1620 you can do this:
    traw := (traw << 23) ~> 23
    

    This fix the sign bit so you can do normal math on it. As Tracy pointed out, the raw value is in 0.5 degree units. Multiply that by 5 to get 0.1 degree units with 0.5 degrees of resolution. From there everything is pretty easy.
  • Mikael SMikael S Posts: 60
    edited 2012-02-16 21:54
    This part is from the DS1620 object:
       tc := tc << 23 ~> 23                                ' extend sign bit
        tc *= 5                                             ' convert to 10ths
        return tc
    
    

    So its already does that for me.
    My problem is that i need to get the answer in two separate digits (to present it on a LED display).
  • JonnyMacJonnyMac Posts: 9,197
    edited 2012-02-17 07:46
    It's pretty easy:
    if (tc < 0)
        sign := 1
        tc := -tc
      else
        sign := 0
    
      tens := tc / 100
      ones := (tc // 100) / 10
      tenths := tc // 10
    
  • Mikael SMikael S Posts: 60
    edited 2012-02-19 00:43
    That was pretty easy! Stupid me again....
Sign In or Register to comment.