Creating Single VAR(nib) with a 2 Bit input
gc3076
Posts: 44
I am reviewing Dr. Tracy Allen's excellent applications notes on the use of quadrature encoders with the PBASIC Stamp:
http://www.emesystems.com/BS2fsm.htm#twobit
Can someone explain to me how to get a NIB from the two separate unique single BIT inputs from A & B ?
I undersatnd that the NIB is converted to a DEC from Binary result of the AB BITs but I don't understand how the two separate inputs·A & B become the·NIB ·i.e. A=1, B=1, new=11. How do I get the·11 ?
----> ----> ----> ----> CW
0 1 1 0 0 B
0 0 1 1 0 A
<---- <---- <---- <---- CCW
0 1 3 2 0 state
http://www.emesystems.com/BS2fsm.htm#twobit
Can someone explain to me how to get a NIB from the two separate unique single BIT inputs from A & B ?
I undersatnd that the NIB is converted to a DEC from Binary result of the AB BITs but I don't understand how the two separate inputs·A & B become the·NIB ·i.e. A=1, B=1, new=11. How do I get the·11 ?
----> ----> ----> ----> CW
0 1 1 0 0 B
0 0 1 1 0 A
<---- <---- <---- <---- CCW
0 1 3 2 0 state
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
I am trying to understand how A input pin and the B input pin are combined to a single VAR.
pinstate_A = 1
pinstate_B = 1
new_AB_combine_state = 11(BIN) or 3 (DEC) as shown at http://www.emesystems.com/BS2fsm.htm#twobit
How is AB combined, not sure if I have asked this clearer or not ?
How about this added/changed in your existing code:
pinstates VAR NIB
pinstate_A VAR pinstates.highbit
pinstate_B VAR pinstates.lowbit
Double check the exact syntax.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
<!--StartFragment -->
How would:
pinstates VAR NIB
pinstate_A VAR pinstates.highbit
pinstate_B VAR pinstates.lowbit
combine my A B pin states to a single binary value ?
DO
new = ((2*INPUT_FR_PIN_B) + INPUT_FR_PIN_A)
DEBUG DEC new, CR
PAUSE 1000
LOOP
pinStates··· VAR··· Nib
pinA·········VAR··· pinStates.BIT0
pinB·········VAR··· pinStates.BIT1
Now you can do this:
· pinA = Input_Chan_A
· pinB = Input_Chan_B
... and now the pinStates variable will hold the two-bit states of your inputs.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
EncoderState········· VAR······· Nib········· 'Holds the complete State of the encoder
EncoderHigh·········· PIN······· 0··········· 'Sets EncoderHigh = whatever state PIN 0 is in
EncoderLow··········· PIN······· 1··········· 'Sets EncoderLow = whatever state PIN 1 is in
EncoderState = 2·* EncoderHigh + EncoderLow·· 'Sets EncoderState = whatever the Binary add of Pin 0 & Pin 1
Now, what I can't prove (the stamp stuff isn't installed on the new laptop yet) is this:
EncoderState········· VAR······· Nib········· 'Holds the complete state of the encoder
EncoderHiPin········· PIN········· 0··········'Sets EncoderHiPin = whatever state pin 0 is in
EncoderLoPin········· PIN········· 1········· 'Sets EncoderLoPin = whatever state pin 1 is in
EncoderState.BIT0 = EncoderHiPin············· 'Put the state of Pin 0 into the high bit of the encoder var
EncoderState.BIT1 = EncoderLoPin············· 'Put the state of Pin 1 into the low bit of the encoder var
So, will the second method work, or do I need more qualifiers for it?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket
KK
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Thanks. As long as the var EncoderState is only read, it will never contain a invalid number.
This, of corse, providing you include a one time EncoderState = 0 line, after that, it shouldn't be an issue.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket
KK
·
Your question...
>Can someone explain to me how to get a NIB from the two separate unique single BIT inputs from A & B ?
>I don't understand how the two separate inputs A & B become the NIB i.e. A=1, B=1, new=11. How do I get the 11 ?
... the program in question never deals with unique single bits. And reads them as a Nib at one gulp by referring to inC:
new=inC & 3 ' read new state of encoder, mask two bits
Then it uses that value (%00, %01, %10, %11 as pairs) in combination with the previous value that was there, to decide what direction (if any) the encoder has moved. Then the new value becomes the previous value and the cycle repeats. It never deals with individual bits.
If I did want to refer to them individually, in the context of a state machine, I would read the whole port into a variable, as above, and then refer to the bits in the variable. It is not a good idea to capture the bits individually, because while you are reading one, the other one can change, and that defeats the logic of the state machine. The secret is to read the port all at one gulp into a variable and then apply the logic to the variable...
newState VAR Nib
pA VAR newState.BIT0
pB VAR newState.BIT1
newState = inC ' to read pins p8, p9, p10 and p11 at one stroke
DEBUG BIN1 pA, TAB , BIN1 pB
I looked at the URL you referenced <http://www.emesystems.com/BS2fsm.htm#twobit>, and saw that the format of the ascii diagrams was all screwed up with bad aspacing. It is the open source HTML editor I was trying out. (NVU). It messes up the formating on the page whenever I edit image tags. I went in and restored the formatting and republished the page.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com