Shop OBEX P1 Docs P2 Docs Learn Events
output entire array — Parallax Forums

output entire array

SexieWASDSexieWASD Posts: 41
edited 2010-01-18 01:50 in Propeller 1
I'm using an I/O expander, and writing a driver for it.

to switch pins on it I need to output a binary byte where %0000_0001 would be only the last pin on, and %0000_0010 would be the second to last pin, %1111_1111 would be all of the pins etc... 0 = off and 1 = on.

I need to read the pin's states and store it in an array so that I can change one pin without resetting all of the other pins.
after i change the value of one pin, how would I return the entire array as one variable?

For example if my array looks like this, how can i make it one number.
mynumber [noparse][[/noparse] 0 ] := 1
mynumber [noparse][[/noparse] 1 ] := 1
mynumber [noparse][[/noparse] 2 ] := 0
mynumber [noparse][[/noparse] 3 ] := 0
mynumber [noparse][[/noparse] 4 ] := 1
mynumber [noparse][[/noparse] 5 ] := 1
mynumber [noparse][[/noparse] 6 ] := 0
mynumber [noparse][[/noparse] 7 ] := 0




how do I get that to be,

newnumber == %1100_1100







EDIT: I tested my guitar circuit today, no noticeable interference or distortion, and the guitar is waiting for dye and clear coat. It should be finished fairly soon, so expect a detailed post and maybe a video of how it works, along with some code. Thank you everyone that has helped me with this. I have learned a ton so far and am having fun with the prop world, it's bit different than the web development world that I'm used to.

Post Edited (SexieWASD) : 1/16/2010 10:49:27 PM GMT

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-01-17 00:11
    If you're using an array of anything to store a byte you're wasting a lot of RAM. The tricky thing with the Propeller is that it doesn't provide direct bit access with variables, but that doesn't stop us from writing a custom method that uses masking. For example:

    pub writebit(value, pos, bitval) | mask
    
      mask := |< pos
      if (bitval == 1)
        value := value | mask
      elseif (bitval == 0) 
        value := value & !mask
      endif
    
      return value
    


    If pos or bitval are out-of-range then value is returned unchanged.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-01-17 00:18
    In your particular example you are reversing the bits of the value -- this is automatic with the Propeller. If, for example, you were reading swtiches on P0..P7 you might do this:

    number := ina[noparse][[/noparse] 7..0 ]
    


    This will put the bit on P0 in the LSB of number, and the bit on P7 in the MSB of number. If you want to reverse the order, change the way you read the bits to this:

    number := ina[noparse][[/noparse] 0..7 ]
    


    Now the value on P7 will go to th LSB of number, and the bit on P0 will go to the MSB.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-01-17 00:18
    If you only have bits, why do you want to store one bit in an array variable? Arrays are byte, word and longs, so you waste 7, 15 or 31 bits.
    You can read the states of all pins in one instruction and store it in one byte.

    If you want to access bits in a byte without touching the other bits you'd do:

    mynumber |= |<0 to set bit 0
    mynumber |= |<3 to set bit 3 .....

    and for deletion

    mynumber &= !|<3
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-01-17 00:26
    If you're writing to Propeller pins, you can write to one, or a contiguous group, of them at a time. For example, outa[noparse][[/noparse]16] := 1 raises pin A16 without affecting the others.

    -Phil
  • SamMishalSamMishal Posts: 468
    edited 2010-01-17 01:40
    SexieWad,

    There is an operator in Spin that can help you very easily obtain the state of a SINGLE bit from a variable no matter which one it is.
    ·
    The operator is |<· ….. What this operator does is return a number with only ONE BIT set to 1
    Use this formula······
    BitStatus := AnyVariable & (|< RequiredBitNumber)
    

    Lets say you have a variable X and you want the 9th bit· (ie bit 8) you do this
    ··· ·· BitStatus := X & (|< 8)
    ·
    This way you can get the value·of any bit you want from a number just by using the formula above.
    ··
    Also·if you want to set (ie 1) a particular bit in a number then do this···
    AnyVariable |= (|< RequiredBitNumber)   ''''' or more readable AnyVariable := AnyVariable | (|<RequiredBitNumber)
    

    To reset (ie 0) a bit do····
    AnyVariable &= !(|< RequiredBitNumber)   ''''' or more readable AnyVariable := AnyVariable & !(|<RequiredBitNumber)
    

    ·

    To Toggle (ie 0->1 or 1->0)· then do this····
    AnyVariable ^= (|< RequiredBitNumber)     ''''' or more readable AnyVariable := AnyVariable ^ (|<RequiredBitNumber)
    

    ·
    HOWEVER........if what you want to do is read/set the·I/O pins rather than·bits of a variabel then·SPIN is your friend again.
    The registers InA and OutA are just like any Long variable (ie 32 bits)·but you can treat them as an ARRAY of BITS.
    ·
    OutA[noparse]/noparse allows you to·set the status of·any ONE pin or a GROUP of pins.....and InA[noparse]/noparse·allows you to read the status of any ONE pin or a GROUP of pins.
    ·
    OutA[noparse][[/noparse]N] := 1· .... set the Nth bit to 1
    OutA[noparse][[/noparse]N]~~··· .... set the Nth bit to 1
    OutA[noparse][[/noparse]N] :=·0· .... set the Nth bit to·0
    OutA[noparse][[/noparse]N]~·· ··· .... set the Nth bit to 0
    ·
    OutA[noparse][[/noparse]A..B] :=·X· .... set the Ath bit down to (or up to) the Bth bit to 1·or 0 depending on the CORRESPONDING bit of X ... B will be the LSb.
    OutA[noparse][[/noparse]A..B]~~··· .... set the Ath bit down to (or up to) the Bth bit to 1·
    OutA[noparse][[/noparse]A..B]~·· ··· .... set the Ath bit down to (or up to) the Bth bit to·0
    ·
    X := InA[noparse][[/noparse]N]···· .... read the Nth bit
    X:= InA[noparse][[/noparse]A..B] ·....read the Ath bit down to (or up to) the Bth bit
    ··························· Note: if you say InA[noparse][[/noparse]5..8] you will get a different number than if you say InA[noparse][[/noparse]8..5]...the latter is the more correct
    ··························· format if you want to maintain Bit order but the former is a good trick that you may need sometimes if you want
    ··························· to have the reverse bit order.
    ·
    ·
    ·


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Samuel

    www.RobotBASIC.com


    Post Edited (SamMishal) : 1/17/2010 2:27:29 AM GMT
  • SexieWASDSexieWASD Posts: 41
    edited 2010-01-17 14:27
    Thank you MagIO2, that is exactly what I need to do, and thank you SamMishal for the well formatted explanation. I am sure that the toggle bit will come in handy very soon.

    I wish i was doing this with the prop's pins. It would be very easy that way, but I need to prepare a byte to send to a register in my I/O expander via I2C.
  • SamMishalSamMishal Posts: 468
    edited 2010-01-17 22:31
    SexieWASD said...thank you SamMishal for the well formatted explanation.
    SexieWASD I really appreciate your comment....thanks....smilewinkgrin.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Samuel

    www.RobotBASIC.com
    ·
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-01-18 00:36
    You can reverse bits in a variable, too, have a look at the >< operator in the manual. In your case, with 8-bit values you could do this:

    myValue ><= 8
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • kuronekokuroneko Posts: 3,623
    edited 2010-01-18 01:50
    SexieWASD said...
    I wish i was doing this with the prop's pins. It would be very easy that way, but I need to prepare a byte to send to a register in my I/O expander via I2C.
    You could always utilise outb. As it's not connected you can use it as a normal long and have the benefit of using the SPIN bit access functions.
Sign In or Register to comment.