-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQ_decoder.vhd
181 lines (156 loc) · 5.27 KB
/
Q_decoder.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 10:44:24 03/27/2015
-- Design Name:
-- Module Name: Quadrature_Decoder - Behavioral
--
-- based on:
-- 1. http://www.fpga4fun.com/QuadratureDecoder.html (Verilog code)
-- 2. http://www.hackmeister.dk/2010/07/using-a-quadrature-encoder-as-input-to-fpga/ (VHDL code)
-- Contributor: Filip Filipov
-- Forum topic:
--
----------------------------------------------------------------------------------
Library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
--use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.NUMERIC_STD.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity Q_Decoder is
Port ( Clk : in STD_LOGIC;
QuadA : in STD_LOGIC;
QuadB : in STD_LOGIC;
QuadA_s : in STD_LOGIC;
QuadB_s : in STD_LOGIC;
QuadA_t : in STD_LOGIC;
QuadB_t : in STD_LOGIC;
QuadA_f : in STD_LOGIC;
QuadB_f : in STD_LOGIC;
Position : out STD_LOGIC_VECTOR (31 downto 0);
Position_s : out STD_LOGIC_VECTOR (31 downto 0);
Position_t : out STD_LOGIC_VECTOR (31 downto 0);
Position_f : out STD_LOGIC_VECTOR (31 downto 0));
end Q_Decoder;
architecture Behavioral of Q_Decoder is
--- first wheel
signal QuadA_Delayed: unsigned(2 downto 0) := "000";
signal QuadB_Delayed: unsigned(2 downto 0) := "000";
signal Count_Enable: STD_LOGIC;
signal Count_Direction: STD_LOGIC;
signal Count: signed(31 downto 0) := (others => '0');--"00000000";
-----------------------------------
-----------------------------------
--- second wheel
signal QuadA_Delayed_s: unsigned(2 downto 0) := "000";
signal QuadB_Delayed_s: unsigned(2 downto 0) := "000";
signal Count_Enable_s: STD_LOGIC;
signal Count_Direction_s: STD_LOGIC;
signal Count_s: signed(31 downto 0) := (others => '0');--"00000000";
-----------------------------------
-----------------------------------
-- third wheel
signal QuadA_Delayed_t: unsigned(2 downto 0) := "000";
signal QuadB_Delayed_t: unsigned(2 downto 0) := "000";
signal Count_Enable_t: STD_LOGIC;
signal Count_Direction_t: STD_LOGIC;
signal Count_t: signed(31 downto 0) := (others => '0');--"00000000";
-----------------------------------
-----------------------------------
-- fourt wheel
signal QuadA_Delayed_f: unsigned(2 downto 0) := "000";
signal QuadB_Delayed_f: unsigned(2 downto 0) := "000";
signal Count_Enable_f: STD_LOGIC;
signal Count_Direction_f: STD_LOGIC;
signal Count_f: signed(31 downto 0) := (others => '0');--"00000000";
-----------------------------------
-----------------------------------
begin
--- first wheel
process (Clk)
begin
if Clk='1' and Clk'event then
QuadA_Delayed <= (QuadA_Delayed(1), QuadA_Delayed(0), QuadA);
QuadB_Delayed <= (QuadB_Delayed(1), QuadB_Delayed(0), QuadB);
if Count_Enable='1' then
if Count_Direction='1' then
Count <= Count + 1;
Position <= std_logic_vector(Count);
else
Count <= Count - 1;
Position <= std_logic_vector(Count);
end if;
end if;
end if;
end process;
Count_Enable <= QuadA_Delayed(1) xor QuadA_Delayed(2) xor QuadB_Delayed(1)
xor QuadB_Delayed(2);
Count_Direction <= QuadA_Delayed(1) xor QuadB_Delayed(2);
-----------------------------------
-----------------------------------
--- second wheel
process (Clk)
begin
if Clk='1' and Clk'event then
QuadA_Delayed_s <= (QuadA_Delayed_s(1), QuadA_Delayed_s(0), QuadA_s);
QuadB_Delayed_s <= (QuadB_Delayed_s(1), QuadB_Delayed_s(0), QuadB_s);
if Count_Enable_s='1' then
if Count_Direction_s='1' then
Count_s <= Count_s + 1;
Position_s <= std_logic_vector(Count_s);
else
Count_s <= Count_s - 1;
Position_s <= std_logic_vector(Count_s);
end if;
end if;
end if;
end process;
Count_Enable_s <= QuadA_Delayed_s(1) xor QuadA_Delayed_s(2) xor QuadB_Delayed_s(1)
xor QuadB_Delayed_s(2);
Count_Direction_s <= QuadA_Delayed_s(1) xor QuadB_Delayed_s(2);
-----------------------------------
-----------------------------------
-- third wheel
process (Clk)
begin
if Clk='1' and Clk'event then
QuadA_Delayed_t <= (QuadA_Delayed_t(1), QuadA_Delayed_t(0), QuadA_t);
QuadB_Delayed_t <= (QuadB_Delayed_t(1), QuadB_Delayed_t(0), QuadB_t);
if Count_Enable_t='1' then
if Count_Direction_t='1' then
Count_t <= Count_t + 1;
Position_t <= std_logic_vector(Count_t);
else
Count_t <= Count_t - 1;
Position_t <= std_logic_vector(Count_t);
end if;
end if;
end if;
end process;
Count_Enable_t <= QuadA_Delayed_t(1) xor QuadA_Delayed_t(2) xor QuadB_Delayed_t(1)
xor QuadB_Delayed_t(2);
Count_Direction_t <= QuadA_Delayed_t(1) xor QuadB_Delayed_t(2);
-----------------------------------
-----------------------------------
-- fourt wheel
process (Clk)
begin
if Clk='1' and Clk'event then
QuadA_Delayed_f <= (QuadA_Delayed_f(1), QuadA_Delayed_f(0), QuadA_f);
QuadB_Delayed_f <= (QuadB_Delayed_f(1), QuadB_Delayed_f(0), QuadB_f);
if Count_Enable_f='1' then
if Count_Direction_f='1' then
Count_f <= Count_f + 1;
Position_f <= std_logic_vector(Count_f);
else
Count_f <= Count_f - 1;
Position_f <= std_logic_vector(Count_f);
end if;
end if;
end if;
end process;
Count_Enable_f <= QuadA_Delayed_f(1) xor QuadA_Delayed_f(2) xor QuadB_Delayed_f(1)
xor QuadB_Delayed_f(2);
Count_Direction_f <= QuadA_Delayed_f(1) xor QuadB_Delayed_f(2);
end Behavioral;