encoder problem
paalajohansen
Posts: 2
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
But... 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
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
But... 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
The comments should let you get your code working. Take care that your encoder inputs are on the correct pins!
Cheers,
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.