Shop OBEX P1 Docs P2 Docs Learn Events
encoder problem — Parallax Forums

encoder problem

paalajohansenpaalajohansen Posts: 2
edited 2011-11-19 16:32 in BASIC Stamp
Hi!

I'm new to encoders! Copied som code i found on the net using XOR to decide CW/CCW rotation.

http://www.parallax.com/dl/docs/cols/nv/vol6/col/nv127.pdf

B
ut... I can only get the encoder to count UP with that code. Tried some other variations of the code, but I can only get it to count up.


' {$STAMP BS2p}
' {$PBASIC 2.5}






' encoder, signals on P8 and P9 (two lsbs of nibC)
' using XOR logic
old VAR Nib ' previous state, only two bits
new VAR Nib ' current state
ob VAR old.BIT0 ' bits to test
nb VAR new.BIT1
cnt VAR Word ' for demonstration
DIRD=0 ' make port C inputs
old = IND & 0110 ' read initial state of inputs
decode:
new = IND & 0110 ' read state of encoder, two bits
IF new=old THEN decode ' loop until change to new state
cnt=ob^nb*2-1+cnt ' calculate motion
old=new ' new becomes old
DEBUG HOME,SDEC5 cnt
GOTO decode



Help me ;)

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2011-11-19 14:12
    I didn't go thru your code in detail, but the attached file works on the same principles.

    The comments should let you get your code working. Take care that your encoder inputs are on the correct pins!

    Cheers,
  • paalajohansenpaalajohansen Posts: 2
    edited 2011-11-19 14:19
    Thnx! I got the code to work, but I have the same result. Counting up both ways.
  • ercoerco Posts: 20,256
    edited 2011-11-19 16:32
    counter=counter-1 ' jump here to decrement
    OUTH=counter.LOWBYTE
    GOTO start

    cw:
    HIGH 0 ' pin 0 turns on led if turning clockwise
    counter=counter+1


    As long as you have counter-1 and counter+1 in the right places, it sure should work. Throw in a few temporary debug statements adjacent to the counter +/- commands to make sure the program is transferring properly.
Sign In or Register to comment.