Shop OBEX P1 Docs P2 Docs Learn Events
Creating Single VAR(nib) with a 2 Bit input — Parallax Forums

Creating Single VAR(nib) with a 2 Bit input

gc3076gc3076 Posts: 44
edited 2006-02-24 13:42 in BASIC Stamp
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

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-02-23 16:40
    Not sure I understand your question properly.· A NIB is the smallest variable that can hold a 2-bit value.· You would, of course, have 2 unused bits.· The values would usually be the lower 2 bits of the NIB variable and can be masked if other data resides in the upper 2 bits, such as if you have 2 encoders.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • gc3076gc3076 Posts: 44
    edited 2006-02-23 16:46
    Sorry,

    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 ?
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-02-23 16:56
    gc3076 -

    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 -->
  • gc3076gc3076 Posts: 44
    edited 2006-02-23 17:05
    Sorry I must be really thick today. I still don't get it ...................

    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 ?
  • gc3076gc3076 Posts: 44
    edited 2006-02-23 17:18
    this works.................

    DO
    new = ((2*INPUT_FR_PIN_B) + INPUT_FR_PIN_A)
    DEBUG DEC new, CR
    PAUSE 1000

    LOOP
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-23 17:35
    The BASIC Stamp can alias bit values within a variable, so you can do this:

    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
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-02-23 18:07
    Hmmm, I was thinking this was more his question...
    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 WilliamsJon Williams Posts: 6,491
    edited 2006-02-23 18:13
    I think using the .BITx modifiers is more elgant, and is a tad faster. The only advantage of the first method is that it will clear bits 2 and 3 of the encoderState variable.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-02-23 18:21
    Jon:
    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
    ·
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-02-24 07:30
    Hi gc,

    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
  • gc3076gc3076 Posts: 44
    edited 2006-02-24 13:42
    thank you for all the help, it is appreciated
Sign In or Register to comment.