2 -- * Quadrature Signal Decoder *
3 -- Used for IRC sensor interfacing
5 -- (c) 2010 Marek Peca <hefaistos@gmail.com>
7 -- Updated for generic size
8 -- 2016 Pavel Pisa <pisa@cmp.felk.cvut.cz>
10 -- license: GNU LGPL and GPLv3+
13 use ieee.std_logic_1164.all;
14 use ieee.std_logic_arith.all;
15 use ieee.std_logic_unsigned.all;
16 use ieee.numeric_std.all;
18 entity qcounter_nbit is
20 bitwidth: integer := 32
26 qcount: out std_logic_vector (bitwidth - 1 downto 0);
27 a_rise, a_fall, b_rise, b_fall, ab_event: out std_logic;
28 ab_error: out std_logic
32 architecture behavioral of qcounter_nbit is
41 subtype std_logic4 is std_logic_vector (3 downto 0);
42 signal a, b, a_prev, b_prev: std_logic;
43 signal count_prev: std_logic_vector (bitwidth - 3 downto 0)
45 signal count: std_logic_vector (bitwidth - 3 downto 0);
47 -- stabilize signal a between clock ticks
48 -- active on rising edge of the clock signal
56 -- stabilize signal b between clock ticks
57 -- active on rising edge of the clock signal
65 -- the first two bits are combinational logic only
68 qcount(bitwidth - 1 downto 2) <= count;
70 -- purpose of this process is only to propagate signals to the pins
71 comb_event: process (a_prev, b_prev, a, b)
79 if ((a xor a_prev) and (b xor b_prev)) = '1' then -- a i b se zmenily zaroven
80 -- forbidden double transition
83 a_rise <= (a xor a_prev) and a; -- a rising
84 a_fall <= (a xor a_prev) and not a; -- a falling
85 b_rise <= (b xor b_prev) and b; -- b rissing
86 b_fall <= (b xor b_prev) and not b; -- b falling
87 ab_event <= (a xor a_prev) or (b xor b_prev); --a or b changed
91 -- carry to the third bit (binary)
92 comb_count: process (a_prev, b_prev, a, b, count,count_prev)
94 if (a_prev = '0') and (b_prev = '1') and (a = '0') and (b = '0') then --posun dopredu
95 count <= count_prev + 1;
96 elsif (a_prev = '0') and (b_prev = '0') and (a = '0') and (b = '1') then --posun dozadu
97 count <= count_prev - 1;
103 -- all state update is done at clock signal rising edge
104 -- reset count_prev register, it propagates to combinational count
105 -- results automatically
108 wait until clock'event and clock = '1';
110 count_prev <= (others => '0');