Shop OBEX P1 Docs P2 Docs Learn Events
Help with I2C DS1621 — Parallax Forums

Help with I2C DS1621

David E.David E. Posts: 13
edited 2008-06-13 04:28 in Propeller 1
I'm just getting started with the Prop, have been trying some things out, and I am trying to get a DS1621 to work over I2C.

I have it working using the i2cobject demo from the OBEX, and displaying the temperature on a TV screen. No big deal.

However, any of my attempts to actual get the .5 degree resolution have failed. I am only able to get temperature down to 1 degree, even though the device is sending out 9 bits of data. Are return values for objects/methods not allowed to be anything other than integers? I was not able to find clarification in the manual, it says nothing about limitations on the return value.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-12 18:20
    All variables in Spin are integer. The compiler will process floating point constants and expressions, but the 32-bit floating point result is treated as a 32-bit integer. You can use the floating point library to process these values and it will interpret them properly, but that's the only support for floating point at run-time.

    You can use the floating point library and convert the 2 byte temperature to floating point or you can represent the temperature as an integer scaled in terms of 1/2 degrees with 1.5 represented as 3 or 11.5 represented as 23, etc.

    Post Edited (Mike Green) : 6/12/2008 6:27:05 PM GMT
  • David E.David E. Posts: 13
    edited 2008-06-12 19:47
    Ok, so I cannot do this without modifying the I2C driver? I feel comfortable modifying the DS1621 object from the i2cobject exchange method, but I use the i2c object for other devices as well and do not want to modify it.

    pub readTempC : tempC
        ' read temp C (int only.  Not implemented decimal places! i.e. 20 not 20.5)
        if started == true
          tempC := i2cObject.readLocation(ds1621Address, _ReadTemp, 8, 9)
          return tempC
    
    



    This is the method in the DS1621 object that calls and reads the 9 bits from I2C to the device. Would I multiply this by 2 before returning from the method or is there some other way I should do this?

    Would it be better to multiply by 10 and simply remove the last digit when displaying?

    Post Edited (David E.) : 6/12/2008 7:56:11 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-12 20:34
    What you can do is to use ReadPage to read two bytes into a two byte array. You can use a temporary long for that and adjust accordingly:
    pub readTempC | tempC
        ' This returns a value of NEGX if there's an error or it returns an integer between -110 and +250
        ' representing a temperature between -55 and +125 in 1/2 degree units.
        if started == true
            if i2cObject.readPage(ds1621Address, _ReadTemp, 8, @tempC, 2) == 0
                return (((tempC.byte[noparse][[/noparse] 0 ] << 1) | (tempC.byte[noparse][[/noparse] 1 ] >> 7)) -> 9) ~> 23
        return NEGX
    
    
  • David E.David E. Posts: 13
    edited 2008-06-12 21:48
    Thanks, I will give that a try. I suppose I could multiply the return by 5 and have an integer value that's easier to read (ie 1250 for 125 degrees instead of 250 for 125 degrees).


    This is my first forray into uCs and I'm trying to get used to the integer only aspect of things, I'm used to being able to have variables of all types.
  • David E.David E. Posts: 13
    edited 2008-06-13 04:28
    Mike I gave that a try and had no success. There's an extra variable in there for the readpage call so I removed the 8 but this is not working out for me. I tried everything I could think of with the bitwise operators but I think the real problem is I really don't understand what you were doing with shifting and rotating the bits around. I thought I had it working when it came up at 25 degrees C but it never moved after that. ANything else I tried came back with 146 or a number over 10k. Actually your code always returned NEGX. It was only when I forced a readpage when I got any other values.

    I've had it with this thing, I'm going to the gym to work on something that I understand. Maybe when I come back I'll have an epiphany or something.

    Post Edited (David E.) : 6/13/2008 4:50:36 AM GMT
Sign In or Register to comment.