> If the four samples were > A B > 0 0 (oldest state) > 0 1 (older state) > 1 1 (last state) > 0 0 (newest state) > > Then this last state change from 1 1 to 0 0 could be > counted as 2, rather than thrown away as an illegal change. > This would take a 64 or > 256 entry table, rather than 16 entries, but it would be just as fast. > Faster, even, since I wouldn't have to clear the top four > bits every time I got a new sample. You could also record the direction as a simple 0 or one in the 5th bit (over the top of the "older state" lsb) and then use a 32 entry table. Your example above would become A B x 1 (oldest direction) x 1 (older direction) 1 1 (last state) 0 0 (newest state) Or you could use a 16 entry table which returns an "invalid" value. When "invalid" is returned, it causes the direction bit to be used to load a 2 or -2 as appropriate. If you pick the invalid value to be 2 then a bit test and negate are all that is required. Actually... EncWait: mov w,Enc ;Port "Enc" 2 lsb's = encoder pins and w,#%00000011 ;get rid of all but 2 enc bits mov temp,w ;save the new reading xor w, old ;compare it to the old snz ;Any change? jp EncWait ; if not, go back and wait xor w,#%00000011 snz jp EncErr ; ;Table? TABLE!?!? We don't need no stinken table! clc ;get ready for right shift rl old ;align bits xor old,temp ;check for direction and save new with old EncCount jb old.1,EncUp ;The second to lsb: 1=up direction, 0=down EncDown: dec Pos ;we be going down: -1 ret ;return so action is taken EncUp: inc Pos ;we be mov'n on up: +1 ret ;return so our caller can take action Err: call EncCount ;just leave the direction as is and take action jp EncCount ; ...twice Sorry, parallax mnemonics. Based on code from Matthew Ballinger at http://www.piclist.com/techref/microchip/qenc.htm Old Enc old' 0000 0 0 0000 0000 0 1 0001 0001 1 1 0001 ;note: old was rotated left before xor 0001 0 0 0001 0001 0 0 0000 ;note: old.0 zeros out on first no change. 0001 1 0 0010 0010 1 1 0111 Old.1 (the lsb of Old) holds the direction at all times. The rotating old left one bit and xoring with the new encoder reading is weird, but just happens to work. (I'm pretty sure) --- James. -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist