In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Peter Van der Zee wrote: Hi Bill; Actually quadrature operation gives double the resolution of a single channel. At any instant, with quadrature there is one of 4 possible states.... lo/lo hi/lo lo/hi hi/hi. And the encoder moves through all four of them in one physical cog, and each of them can be detected by the micro. The "forward" sequence is lo/lo to lo/hi to hi/hi to hi/lo to lo/lo and the "reverse" sequence is (not surprisingly) lo/lo to hi/lo to hi/hi to lo/hi to lo/lo. Four phase states per cog. And the "direction" is determined at any point by comparing the current state to the previous state. Attached is a state machine implementation of a quadrature encoder for you to peruse. Assemble it and single step through it with the debugger or SXSim to see how easily it works. The state lookup table is made up of all the possibilities of the previous state combined with the current state. [code] Assume RA.1 and RA.0 are inputs and connected to the two quadrature channels Assume varable Prev holds the state of the previous sample. Loop rl Prev rl Prev ;make room for the new sample coming in and Prev,#%0000_1100 ;get rid of any carry-ins and older samples Sample mov w,RA ;get new sample to w and w,#%0000_0011 ;disregard all except the two state bits or Prev,w ;merge new sample in with previous sample mov w,Prev ;get old/new state pairs call Lookup ;lookup same/forward/reverse in Prev/New state table add Position,w ;update position by one... positive or negative (delay here to set he speed you want to sample at) jmp Loop ;get next sample Lookup add pc,w ;jump into table and return with +1 for forward, -1 for reverse, and 0 for no change P00N00 retw 0 ;no change P00N01 retw 1 ;forward P00N10 retw -1 ;reverse P00N11 retw 0 ;impossible P01N00 retw -1 ;reverse P01N01 retw 0 ;no change P01N10 retw 0 ;impossible P01N11 retw 1 ;forward P10N00 retw 1 ;forward P10N01 retw 0 ;impossible P10N10 retw 0 ;no change P10N11 retw -1 ;reverse P11N00 retw 0 ;impossible P11N01 retw -1 ;reverse P11N10 retw 1 ;forward P11N11 retw 0 ;no change Cheers, Peter (pjv) [/code] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=2&m=324554#m325796 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2009 (http://www.dotNetBB.com)