-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.vhd
302 lines (253 loc) · 6.47 KB
/
project.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity project is
port ( rstb : in std_logic;
rf_data : in std_logic_vector(2 downto 0) := "000";
mtl : out std_logic_vector(3 downto 0);
mtr : out std_logic_vector(3 downto 0);
CLK_50M : in std_logic;
SEG : out std_logic_vector(6 downto 0) := "0111111";
DP : out std_logic := '0'
);
end project;
architecture Behavioral of project is
signal speed_l : integer range 0 to 250000;
signal speed_r : integer range 0 to 250000;
signal motor_lcnt : integer range 0 to 250000;
signal phase_lclk : std_logic;
signal motor_rcnt : integer range 0 to 250000;
signal phase_rclk : std_logic;
signal phase_lcnt : std_logic_vector(1 downto 0) := "00";
signal phase_lout : std_logic_vector(3 downto 0) := "0000";
signal phase_rcnt : std_logic_vector(1 downto 0) := "00";
signal phase_rout : std_logic_vector(3 downto 0) := "0000";
signal system_clk : std_logic := '0';
signal segment_clk : std_logic := '0';
shared variable seg_count : integer range 0 to 250000 := 250000;
shared variable seg_on : std_logic := '1';
shared variable left_selected : std_logic := '1';
shared variable right_selected : std_logic := '0';
shared variable btn_free : std_logic := '1';
shared variable mtl_speed : std_logic_vector(1 downto 0) := "00";
shared variable mtr_speed : std_logic_vector(1 downto 0) := "00";
begin
process(rf_data)
begin
case rf_data is
when "100" =>
if btn_free = '1' then
if left_selected = '0' then
left_selected := '1';
right_selected := '0';
DP <= '1';
else
left_selected := '0';
right_selected := '1';
DP <= '0';
end if;
btn_free := '0';
end if;
when "001" =>
if btn_free = '1' then
if left_selected = '1' then
case mtl_speed is
when "11" =>
mtl_speed := "10";
speed_l <= 62500;
when "10" =>
mtl_speed := "01";
speed_l <= 124999;
when others =>
mtl_speed := "00";
speed_l <= 0;
end case;
elsif right_selected = '1' then
case mtr_speed is
when "11" =>
mtr_speed := "10";
speed_r <= 62500;
when "10" =>
mtr_speed := "01";
speed_r <= 124999;
when others =>
mtr_speed := "00";
speed_r <= 0;
end case;
end if;
end if;
btn_free := '0';
when "010" =>
if btn_free = '1' then
if left_selected = '1' then
case mtl_speed is
when "00" =>
mtl_speed := "01";
speed_l <= 124999;
when "01" =>
mtl_speed := "10";
speed_l <= 62500;
when others =>
mtl_speed := "11";
speed_l <= 50000;
end case;
elsif right_selected = '1' then
case mtr_speed is
when "00" =>
mtr_speed := "01";
speed_r <= 124999;
when "01" =>
mtr_speed := "10";
speed_r <= 62500;
when others =>
mtr_speed := "11";
speed_r <= 50000;
end case;
end if;
end if;
btn_free := '0';
when others => btn_free := '1';
end case;
end process;
process(rstb, speed_l, system_clk)
begin
if rstb = '0' or speed_l = 0 then
motor_lcnt <= 0;
phase_lclk <= '0';
elsif rising_edge(system_clk) then
if motor_lcnt >= speed_l then
motor_lcnt <= 0;
phase_lclk <= not phase_lclk;
else
motor_lcnt <= motor_lcnt + 1;
end if;
end if;
end process;
process(rstb, speed_r, system_clk)
begin
if rstb = '0' or speed_r = 0 then
motor_rcnt <= 0;
phase_rclk <= '0';
elsif rising_edge(system_clk) then
if motor_rcnt >= speed_r then
motor_rcnt <= 0;
phase_rclk <= not phase_rclk;
else
motor_rcnt <= motor_rcnt + 1;
end if;
end if;
end process;
process(rstb, phase_lclk)
begin
if rstb = '0' then
phase_lcnt <= (others => '0');
elsif rising_edge(phase_lclk) then
phase_lcnt <= phase_lcnt + 1;
end if;
end process;
process(rstb, phase_lcnt)
begin
if rstb = '0' then
phase_lout <= (others => '0');
else
case phase_lcnt is
when "00" => phase_lout <= "1000";
when "01" => phase_lout <= "0100";
when "10" => phase_lout <= "0010";
when "11" => phase_lout <= "0001";
when others => phase_lout <= "0000";
end case;
end if;
end process;
process(rstb, phase_rclk)
begin
if rstb = '0' then
phase_rcnt <= (others => '0');
elsif rising_edge(phase_rclk) then
phase_rcnt <= phase_rcnt + 1;
end if;
end process;
process(rstb, phase_rcnt)
begin
if rstb = '0' then
phase_rout <= (others => '0');
else
case phase_rcnt is
when "00" => phase_rout <= "1000";
when "01" => phase_rout <= "0100";
when "10" => phase_rout <= "0010";
when "11" => phase_rout <= "0001";
when others => phase_rout <= "0000";
end case;
end if;
end process;
process(segment_clk)
begin
if rising_edge(segment_clk) then
if left_selected = '1' then
if seg_count >= (249999 - speed_l) then
seg_count := 0;
if seg_on = '1' then
seg <= "0000000";
seg_on := '0';
else
seg <= "0011111";
seg_on := '1';
end if;
else
seg_count := seg_count + 1;
end if;
else
if seg_count >= (249999 - speed_r) then
seg_count := 0;
if seg_on = '1' then
seg <= "0000000";
seg_on := '0';
else
seg <= "0011111";
seg_on := '1';
end if;
else
seg_count := seg_count + 1;
end if;
end if;
end if;
end process;
process(CLK_50M)
variable cnt : integer range 0 to 3 := 0;
begin
if rising_edge(CLK_50M) then
if cnt = 3 then
system_clk <= not system_clk;
cnt := 0;
else
cnt := cnt + 1;
end if;
else
NULL;
end if;
end process;
process(system_clk)
variable cnt : integer range 0 to 44 := 0;
begin
if rising_edge(system_clk) then
if cnt = 44 then
segment_clk <= not segment_clk;
cnt := 0;
else
cnt := cnt + 1;
end if;
else
NULL;
end if;
end process;
mtl(0) <= phase_lout(0);
mtl(1) <= phase_lout(1);
mtl(2) <= phase_lout(2);
mtl(3) <= phase_lout(3);
mtr(0) <= phase_rout(3);
mtr(1) <= phase_rout(2);
mtr(2) <= phase_rout(1);
mtr(3) <= phase_rout(0);
end Behavioral;