OpenCores
URL https://opencores.org/ocsvn/quadraturecount/quadraturecount/trunk

Subversion Repositories quadraturecount

[/] [quadraturecount/] [tags/] [v1_1_1_1/] [QuadratureDecoder.vhd] - Diff between revs 3 and 9

Only display areas with differences | Details | Blame | View Log

Rev 3 Rev 9
library ieee;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_1164.all;
 
 
-- c2003 Franks Development, LLC
-- c2003 Franks Development, LLC
-- http://www.franks-development.com
-- http://www.franks-development.com
-- !This source is distributed under the terms & conditions specified at opencores.org
-- !This source is distributed under the terms & conditions specified at opencores.org
 
 
--How we 'talk' to the outside world:
--How we 'talk' to the outside world:
entity QuadratureDecoderPorts is
entity QuadratureDecoderPorts is
    port (
    port (
        clock :                 in std_logic;
        clock :                 in std_logic;
        QuadA :                 in std_logic;
        QuadA :                 in std_logic;
        QuadB :                 in std_logic;
        QuadB :                 in std_logic;
        Direction :     out std_logic;
        Direction :     out std_logic;
           CountEnable :        out std_logic
           CountEnable :        out std_logic
    );
    );
end QuadratureDecoderPorts;
end QuadratureDecoderPorts;
 
 
--What we 'do':
--What we 'do':
architecture QuadratureDecoder of QuadratureDecoderPorts is
architecture QuadratureDecoder of QuadratureDecoderPorts is
 
 
        --local 'variables' or 'registers'
        --local 'variables' or 'registers'
 
 
        --this runs our state machine: where are we in the decoding process?
        --this runs our state machine: where are we in the decoding process?
        --the following constants describe each state
        --the following constants describe each state
        --note that every possible state is not listed.  the unused states
        --note that every possible state is not listed.  the unused states
        --are physically unreachable in a functioning quadratre device, given that the 
        --are physically unreachable in a functioning quadratre device, given that the 
        --clock is fast enough to 'catch' each transition on the quadrature inputs
        --clock is fast enough to 'catch' each transition on the quadrature inputs
        --LR means left-right, RL = left-right.  Of course the two are reversed
        --LR means left-right, RL = left-right.  Of course the two are reversed
        --if the two quadratre inputs are switched.
        --if the two quadratre inputs are switched.
        signal state : std_logic_vector(3 downto 0);
        signal state : std_logic_vector(3 downto 0);
        constant Wait0 : std_logic_vector(3 downto 0) := "0000";
        constant Wait0 : std_logic_vector(3 downto 0) := "0000";
        constant Wait1 : std_logic_vector(3 downto 0) := "0001";
        constant Wait1 : std_logic_vector(3 downto 0) := "0001";
        constant Count0 : std_logic_vector(3 downto 0) := "0010";
        constant Count0 : std_logic_vector(3 downto 0) := "0010";
        constant Count1 : std_logic_vector(3 downto 0) := "0011";
        constant Count1 : std_logic_vector(3 downto 0) := "0011";
        constant LR1 : std_logic_vector(3 downto 0) := "1001";
        constant LR1 : std_logic_vector(3 downto 0) := "1001";
        constant LR2 : std_logic_vector(3 downto 0) := "1101";
        constant LR2 : std_logic_vector(3 downto 0) := "1101";
        constant LR3 : std_logic_vector(3 downto 0) := "0101";
        constant LR3 : std_logic_vector(3 downto 0) := "0101";
        constant RL1 : std_logic_vector(3 downto 0) := "0100";
        constant RL1 : std_logic_vector(3 downto 0) := "0100";
        constant RL2 : std_logic_vector(3 downto 0) := "1100";
        constant RL2 : std_logic_vector(3 downto 0) := "1100";
        constant RL3 : std_logic_vector(3 downto 0) := "1000";
        constant RL3 : std_logic_vector(3 downto 0) := "1000";
 
 
        --this is a temp where the two quadrature inputs are stored
        --this is a temp where the two quadrature inputs are stored
        signal Quad : std_logic_vector(1 downto 0);
        signal Quad : std_logic_vector(1 downto 0);
 
 
        --as a single quadrature count is made up of several states, and the decoder
        --as a single quadrature count is made up of several states, and the decoder
        --can remain in a given state indefinately (if the quadrature input
        --can remain in a given state indefinately (if the quadrature input
        --device is not 'moving'), so we need these 'gate-ing' variables
        --device is not 'moving'), so we need these 'gate-ing' variables
        --to keep us from counting on every clock when we sit idle in the 
        --to keep us from counting on every clock when we sit idle in the 
        --'count' state; thusly, we just count on the first clock 
        --'count' state; thusly, we just count on the first clock 
        --upon entering a 'count' state.
        --upon entering a 'count' state.
        signal counted : std_logic;
        signal counted : std_logic;
        signal counting : std_logic;
        signal counting : std_logic;
 
 
begin   --architecture QuadratureDecoder
begin   --architecture QuadratureDecoder
 
 
process (clock)
process (clock)
 
 
        begin --(clock)
        begin --(clock)
 
 
        if ( (clock'event) and (clock = '1') )  then --every rising edge
        if ( (clock'event) and (clock = '1') )  then --every rising edge
 
 
                --convert inputs from asynch to synch by assigning once on each rising edge of clock
                --convert inputs from asynch to synch by assigning once on each rising edge of clock
                Quad(0) <= QuadA;
                Quad(0) <= QuadA;
                Quad(1) <= QuadB;
                Quad(1) <= QuadB;
 
 
                --we are not going to be counting on this clock by default
                --we are not going to be counting on this clock by default
                CountEnable <= '0';
                CountEnable <= '0';
 
 
                --we are not in a 'count' state
                --we are not in a 'count' state
                if (Counting = '0') then
                if (Counting = '0') then
 
 
                        Counted <= '0';   --haven't counted when not in count state
                        Counted <= '0';   --haven't counted when not in count state
                        CountEnable <= '0';       --are not outputing a count either
                        CountEnable <= '0';       --are not outputing a count either
 
 
                end if;
                end if;
 
 
                --we are in a count state
                --we are in a count state
                if (Counting = '1') then
                if (Counting = '1') then
 
 
                        if (Counted = '1') then   --note that this is covered by default, but is included for clarity.
                        if (Counted = '1') then   --note that this is covered by default, but is included for clarity.
                                CountEnable <= '0';        --already counted this one, don't output a count
                                CountEnable <= '0';        --already counted this one, don't output a count
                        end if;
                        end if;
 
 
                        if (Counted = '0') then     --we haven't counted it already
                        if (Counted = '0') then     --we haven't counted it already
                                Counted <= '1';    --make sure we dont count it again on next clock
                                Counted <= '1';    --make sure we dont count it again on next clock
                                CountEnable <= '1';        --output a count!
                                CountEnable <= '1';        --output a count!
                        end if;
                        end if;
 
 
                end if;
                end if;
 
 
                -- run our state machine
                -- run our state machine
                -- the state transitions are governed by the nature of reality -
                -- the state transitions are governed by the nature of reality -
                -- vis-a-vis this is what quadratre is.
                -- vis-a-vis this is what quadratre is.
                -- the '--?' are the physically un-reachable states.
                -- the '--?' are the physically un-reachable states.
                -- note that it is imperative that the clock be at least (4 I recal)
                -- note that it is imperative that the clock be at least (4 I recal)
                -- times faster than the maximum transition rate on each quadratre
                -- times faster than the maximum transition rate on each quadratre
                -- input, or else transitions will occur in between clocks, corrupting
                -- input, or else transitions will occur in between clocks, corrupting
                -- the state of the decoder.  Put differently, the quadratre device must
                -- the state of the decoder.  Put differently, the quadratre device must
                -- physically remain in each state for at least a single clock
                -- physically remain in each state for at least a single clock
                -- or state changes will not be 'captured' and decoder output will be bogus.
                -- or state changes will not be 'captured' and decoder output will be bogus.
                -- which is substancially the case with any clock-based logic.
                -- which is substancially the case with any clock-based logic.
                -- the difference is that a normal glitch is any change in input which
                -- the difference is that a normal glitch is any change in input which
                -- has duration less than a single clock, but in quadrature, as single
                -- has duration less than a single clock, but in quadrature, as single
                -- transition of the actual device cases 4 transitions in the state,
                -- transition of the actual device cases 4 transitions in the state,
                -- by design of the quadrature encoding process.
                -- by design of the quadrature encoding process.
                case state is
                case state is
 
 
                        when Wait0 =>
                        when Wait0 =>
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "11") then state <= Wait0; end if; --?
                                if (Quad = "11") then state <= Wait0; end if; --?
                                Counting <= '0';
                                Counting <= '0';
 
 
                        when Wait1 =>
                        when Wait1 =>
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "11") then state <= Wait0; end if; --?
                                if (Quad = "11") then state <= Wait0; end if; --?
                                Counting <= '0';
                                Counting <= '0';
 
 
                        when Count0 =>
                        when Count0 =>
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "11") then state <= Count0; end if; --?
                                if (Quad = "11") then state <= Count0; end if; --?
                                Counting <= '1';
                                Counting <= '1';
 
 
                        when Count1 =>
                        when Count1 =>
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "11") then state <= Count0; end if; --?
                                if (Quad = "11") then state <= Count0; end if; --?
                                Counting <= '1';
                                Counting <= '1';
 
 
                        when LR1 =>
                        when LR1 =>
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "01") then state <= LR1; end if; --?
                                if (Quad = "01") then state <= LR1; end if; --?
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "11") then state <= LR2; end if;
                                if (Quad = "11") then state <= LR2; end if;
                                Direction <= '0';
                                Direction <= '0';
                                Counting <= '0';
                                Counting <= '0';
 
 
                        when LR2 =>
                        when LR2 =>
                                if (Quad = "00") then state <= LR2; end if; --?
                                if (Quad = "00") then state <= LR2; end if; --?
                                if (Quad = "01") then state <= LR3; end if;
                                if (Quad = "01") then state <= LR3; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "10") then state <= LR1; end if;
                                if (Quad = "11") then state <= LR2; end if; --?
                                if (Quad = "11") then state <= LR2; end if; --?
                                Direction <= '0';
                                Direction <= '0';
                                Counting <= '0';
                                Counting <= '0';
 
 
                        when LR3 =>
                        when LR3 =>
                                if (Quad = "00") then state <= Count0; end if;
                                if (Quad = "00") then state <= Count0; end if;
                                if (Quad = "01") then state <= LR3; end if;
                                if (Quad = "01") then state <= LR3; end if;
                                if (Quad = "10") then state <= LR3; end if; --?
                                if (Quad = "10") then state <= LR3; end if; --?
                                if (Quad = "11") then state <= LR2; end if;
                                if (Quad = "11") then state <= LR2; end if;
                                Direction <= '0';
                                Direction <= '0';
                                Counting <= '0';
                                Counting <= '0';
 
 
                        when RL1 =>
                        when RL1 =>
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "00") then state <= Wait0; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "10") then state <= RL1; end if; --?
                                if (Quad = "10") then state <= RL1; end if; --?
                                if (Quad = "11") then state <= RL2; end if;
                                if (Quad = "11") then state <= RL2; end if;
                                Direction <= '1';
                                Direction <= '1';
                                Counting <= '0';
                                Counting <= '0';
 
 
                        when RL2 =>
                        when RL2 =>
                                if (Quad = "00") then state <= RL2; end if; --?
                                if (Quad = "00") then state <= RL2; end if; --?
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "01") then state <= RL1; end if;
                                if (Quad = "10") then state <= RL3; end if;
                                if (Quad = "10") then state <= RL3; end if;
                                if (Quad = "11") then state <= RL2; end if; --?
                                if (Quad = "11") then state <= RL2; end if; --?
                                Direction <= '1';
                                Direction <= '1';
                                Counting <= '0';
                                Counting <= '0';
 
 
                        when RL3 =>
                        when RL3 =>
                                if (Quad = "00") then state <= Count0; end if;
                                if (Quad = "00") then state <= Count0; end if;
                                if (Quad = "01") then state <= RL3; end if; --?
                                if (Quad = "01") then state <= RL3; end if; --?
                                if (Quad = "10") then state <= RL3; end if;
                                if (Quad = "10") then state <= RL3; end if;
                                if (Quad = "11") then state <= RL2; end if;
                                if (Quad = "11") then state <= RL2; end if;
                                Direction <= '1';
                                Direction <= '1';
                                Counting <= '0';
                                Counting <= '0';
 
 
                        when others => state <= Wait0; -- undefined state; just go back to wait so we don't get stuck here...
                        when others => state <= Wait0; -- undefined state; just go back to wait so we don't get stuck here...
 
 
                end case; --state
                end case; --state
 
 
        end if; --clock'event
        end if; --clock'event
 
 
        end process; --(clock)
        end process; --(clock)
 
 
end QuadratureDecoder;
end QuadratureDecoder;
 
 
 
 
 
 
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.