Here's a tidied version from the above link. A few things of note:
- It would be difficult to integrate this code with other functions.
- If no other signals are to appear on the the input pins then the line "snap = pins & 3" is no longer needed although BRANCH command may not like operating on PINS directly.
- Jitter removal is done by swallowing the first count in the opposite direction. This will not normally add noticable error to the measurement.
- A small optimisation was done by removing some of the "pins = counter" commands from the fast counting states.
<code><pre><font size=2 face='Courier'>
; *****************************************************************************
; * *
; * Quadrature Decoder Using Finite State Machine *
; * *
; * *
; * http://homepage.ntlworld.com/the.happy.hippy/picaxe/encoder.txt *
; * *
; * Modified by Evan Hillas (evanh@clear.net.nz): *
; * 01 Aug 2004 - Streamlined execution *
; * 23 Oct 2004 - Added tripple speed *
; * 26 Oct 2006 - Showing jitter removal variant *
; *****************************************************************************
;
; States are named as _<from>_<to>_<inc/dec>
;
SYMBOL snap = b0
SYMBOL counter = b1
low portc 7 ; Flash LED on my 28X board
sertxd ("Yay",13,10)
dirsc=0
snap = pins & 3
BRANCH snap,( _00_00_f , _01_01_f , _10_10_f , _11_11_f )
; *****************************************************************************
; * *
; * Moving forward state machine *
; * *
; *****************************************************************************
; *****************************************************************************
; * Moving forward zero speed *
; *****************************************************************************
_00_00_f:
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_f , _00_01_f1 , _10_10_b , _00_11_f2 )
_01_01_f:
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_b , _01_01_f , _01_10_f2 , _01_11_f1 )
_11_11_f:
pins = counter
snap = pins & 3
BRANCH snap,( _11_00_f2 , _01_01_b , _11_10_f1 , _11_11_f )
_10_10_f:
pins = counter
snap = pins & 3
BRANCH snap,( _10_00_f1 , _10_01_f2 , _10_10_f , _11_11_b )
; *****************************************************************************
; * Moving forward single speed *
; *****************************************************************************
_10_00_f1:
counter = counter + 1
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_f , _00_01_f1 , _00_10_f3 , _00_11_f2 )
_00_01_f1:
counter = counter + 1
pins = counter
snap = pins & 3
BRANCH snap,( _01_00_f3 , _01_01_f , _01_10_f2 , _01_11_f1 )
_01_11_f1:
counter = counter + 1
pins = counter
snap = pins & 3
BRANCH snap,( _11_00_f2 , _11_01_f3 , _11_10_f1 , _11_11_f )
_11_10_f1:
counter = counter + 1
pins = counter
snap = pins & 3
BRANCH snap,( _10_00_f1 , _10_01_f2 , _10_10_f , _10_11_f3 )
; *****************************************************************************
; * Moving forward double speed *
; *****************************************************************************
_11_00_f2:
counter = counter + 2
snap = pins & 3
BRANCH snap,( _00_00_f , _00_01_f1 , _00_10_f3 , _00_11_f2 )
_10_01_f2:
counter = counter + 2
snap = pins & 3
BRANCH snap,( _01_00_f3 , _01_01_f , _01_10_f2 , _01_11_f1 )
_00_11_f2:
counter = counter + 2
snap = pins & 3
BRANCH snap,( _11_00_f2i , _11_01_f3 , _11_10_f1 , _11_11_f )
_01_10_f2:
counter = counter + 2
snap = pins & 3
BRANCH snap,( _10_00_f1 , _10_01_f2i , _10_10_f , _10_11_f3 )
_11_00_f2i:
counter = counter + 2
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_f , _00_01_f1 , _00_10_f3 , _00_11_f2 )
_10_01_f2i:
counter = counter + 2
pins = counter
snap = pins & 3
BRANCH snap,( _01_00_f3 , _01_01_f , _01_10_f2 , _01_11_f1 )
; *****************************************************************************
; * Moving forward tripple speed *
; *****************************************************************************
_01_00_f3:
counter = counter + 3
snap = pins & 3
BRANCH snap,( _00_00_f , _00_01_f1 , _00_10_f3 , _00_11_f2 )
_11_01_f3:
counter = counter + 3
snap = pins & 3
BRANCH snap,( _01_00_f3i , _01_01_f , _01_10_f2 , _01_11_f1 )
_10_11_f3:
counter = counter + 3
snap = pins & 3
BRANCH snap,( _11_00_f2 , _11_01_f3 , _11_10_f1 , _11_11_f )
_00_10_f3:
counter = counter + 3
snap = pins & 3
BRANCH snap,( _10_00_f1 , _10_01_f2 , _10_10_f , _10_11_f3 )
_01_00_f3i:
counter = counter + 3
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_f , _00_01_f1 , _00_10_f3 , _00_11_f2 )
; *****************************************************************************
; * *
; * Moving backward state machine *
; * *
; *****************************************************************************
; *****************************************************************************
; * Moving backward zero speed *
; *****************************************************************************
_00_00_b:
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_b , _01_01_f , _00_10_b1 , _00_11_b2 )
_01_01_b:
pins = counter
snap = pins & 3
BRANCH snap,( _01_00_b1 , _01_01_b , _01_10_b2 , _11_11_f )
_11_11_b:
pins = counter
snap = pins & 3
BRANCH snap,( _11_00_b2 , _11_01_b1 , _10_10_f , _11_11_b )
_10_10_b:
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_f , _10_01_b2 , _10_10_b , _10_11_b1 )
; *****************************************************************************
; * Moving backward single speed *
; *****************************************************************************
_01_00_b1:
counter = counter - 1
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_b , _00_01_b3 , _00_10_b1 , _00_11_b2 )
_11_01_b1:
counter = counter - 1
pins = counter
snap = pins & 3
BRANCH snap,( _01_00_b1 , _01_01_b , _01_10_b2 , _01_11_b3 )
_10_11_b1:
counter = counter - 1
pins = counter
snap = pins & 3
BRANCH snap,( _11_00_b2 , _11_01_b1 , _11_10_b3 , _11_11_b )
_00_10_b1:
counter = counter - 1
pins = counter
snap = pins & 3
BRANCH snap,( _10_00_b3 , _10_01_b2 , _10_10_b , _10_11_b1 )
; *****************************************************************************
; * Moving backward double speed *
; *****************************************************************************
_11_00_b2:
counter = counter - 2
snap = pins & 3
BRANCH snap,( _00_00_b , _00_01_b3 , _00_10_b1 , _00_11_b2 )
_10_01_b2:
counter = counter - 2
snap = pins & 3
BRANCH snap,( _01_00_b1 , _01_01_b , _01_10_b2 , _01_11_b3 )
_00_11_b2:
counter = counter - 2
snap = pins & 3
BRANCH snap,( _11_00_b2i , _11_01_b1 , _11_10_b3 , _11_11_b )
_01_10_b2:
counter = counter - 2
snap = pins & 3
BRANCH snap,( _10_00_b3 , _10_01_b2i , _10_10_b , _10_11_b1 )
_11_00_b2i:
counter = counter - 2
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_b , _00_01_b3 , _00_10_b1 , _00_11_b2 )
_10_01_b2i:
counter = counter - 2
pins = counter
snap = pins & 3
BRANCH snap,( _01_00_b1 , _01_01_b , _01_10_b2 , _01_11_b3 )
; *****************************************************************************
; * Moving backward tripple speed *
; *****************************************************************************
_10_00_b3:
counter = counter - 3
snap = pins & 3
BRANCH snap,( _00_00_b , _00_01_b3 , _00_10_b1 , _00_11_b2 )
_00_01_b3:
counter = counter - 3
snap = pins & 3
BRANCH snap,( _01_00_b1 , _01_01_b , _01_10_b2 , _01_11_b3 )
_01_11_b3:
counter = counter - 3
snap = pins & 3
BRANCH snap,( _11_00_b2 , _11_01_b1 , _11_10_b3 , _11_11_b )
_11_10_b3:
counter = counter - 3
snap = pins & 3
BRANCH snap,( _10_00_b3i , _10_01_b2 , _10_10_b , _10_11_b1 )
_10_00_b3i:
counter = counter - 3
pins = counter
snap = pins & 3
BRANCH snap,( _00_00_b , _00_01_b3 , _00_10_b1 , _00_11_b2 )
; *****************************************************************************
end
</font></pre></code>
Edited by - evanh on 25/10/2006 23:04:09