Shop OBEX P1 Docs P2 Docs Learn Events
convert stamp var to spin — Parallax Forums

convert stamp var to spin

Dwayne DibbleyDwayne Dibbley Posts: 63
edited 2011-08-20 12:25 in Propeller 1
i have the following varibles from a basic stamp that i would like to use in spin:
dataWord        VAR     Word              ' data word
dataHigh        VAR     dataWord.HIGHBYTE ' high byte of dataWord
dataLow         VAR     dataword.LOWBYTE  ' low byte of dataLow
dataByte        VAR     dataLow         ' (alternate name)
opcode          VAR     dataHigh        ' opcode (same as dataHigh)
format          VAR     dataLow         ' format (same as dataLow)
status          VAR     dataLow         ' status (same as dataLow)

status_Zero     VAR     status.BIT0     ' Zero status bit (0-not zero, 1-zero)
status_Sign     VAR     status.BIT1     ' Sign status bit (0-positive, 1-negative)
status_NaN      VAR     status.BIT2     ' Not a Number status bit (0-valid number, 1-NaN)
status_Inf      VAR     status.BIT3     ' Infinity status bit (0-not infinite, 1-infinite)

in the basic stamp code i need to use the following in spin
IF status_Zero THEN
dbg.str(string("OK")

it is reading a byte and i need to know BIT0 of that byte?

Thanks

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-08-20 11:10
    There are several threads about this topic and objects in the OBEX. You might take a look around, Johnny Mac has some nice SPIN code for determining a bit value.

    A simple method to find the value of a bit is to execute a logical AND. %0001 AND %1011 = %0001
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-08-20 11:35
    Spin does not allow the aliasing of bits, but you can use a simple method to determine the state of a bit in any value -- here's one way:
    pub bit_val(value, position)
    
      if ((position => 0) and (position =< 31))
        return (value >> position) & 1
      else
        return 0
    

    Values in Spin are signed so this method protects you from an out-of-range bit position. For your status_Zero you would do this:
    status_Zero := bit_val(status, 0)
    

    ...just before you use status_Zero. You need to call the method any time there is possibility that the status variable has changed.

    Or, as Spin is much more advanced than PBASIC, you could use the method call in place of a variable:
    if (bit_val(status, 0))
        ' do something when status.0 = 1
      else
        ' do something when status.0 = 0
    
  • Dwayne DibbleyDwayne Dibbley Posts: 63
    edited 2011-08-20 12:25
    Thanks guys, works a treat. Now onto the next hurdle :)
Sign In or Register to comment.