output entire array
SexieWASD
Posts: 41
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.
how do I get that to be,
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
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
If pos or bitval are out-of-range then value is returned unchanged.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
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:
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
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
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······
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···
To reset (ie 0) a bit do····
·
To Toggle (ie 0->1 or 1->0)· then do this····
·
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
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Samuel
www.RobotBASIC.com
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA