交通灯的时间计数器不会增加
Time counter for traffic lights wont increment
我正在为作业编写简单的交集 "simulator"。它是这样的:
用户输入每条车道上在红绿灯处停车的汽车数量,并定义换灯次数。当定时器值等于用户定义的时间时,灯应该改变它们的颜色。
time_counter中出现问题,过程如下
time_counter_process: process (reset, clk)
begin
if reset = '1' then
time_counter<= "0000000";
elsif clk'event and clk='1' then
if (state = state_1 and time_counter < state_1_time_value) then
-- in state_1 one some lights are green and others red, simple
time_counter <= time_counter + 1;
if (state = state_1 and time_counter = state_1_time_value) then
time_counter <= "0000000";
end if;
etc...
elsif state = state_6 and time_counter < state_6_time_value then
time_counter <= time_counter + 1;
if state = state_6 and time_counter = state_6_time_value then
time_counter <= "0000000";
end if;
end if;
end if;
end process time_counter_process;
我不知道为什么,但计时器没有正确递增。它保持在值“000000”。谁能告诉我为什么它不能正常工作?
为了更清楚起见,我还将展示状态如何变化
begin
next_state<= state;
case state is
when state_1 =>
if time_counter < state_1_time_value then
traffic_signal_1 <= "10"; --green
traffic_signal_2 <= "00"; -- red
traffic_signal_3 <= "00";
traffic_signal_4 <= "10";
traffic_signal_5 <= "00";
traffic_signal_6 <= "00";
elsif time_counter = state_1_time_value then
next_state<=state_2;
end if;
您应该将大 if 语句与 case 语句交换 state
并将计数器条件实现为每个 when 选项中的 if 语句。
像time_counter = state_1_time_value
这样的条件永远不会成立,因为信号在进程结束时更新。您所在的分支中 time_counter < state_1_time_value
为真,因此 time_counter = state_1_time_value
不可能也为真。
time_counter_process: process (reset, clk)
begin
if (reset = '1') then
time_counter<= "0000000";
elsif rising_edge(clk) then
case state is
when state_1 =>
if (time_counter < state_1_time_value) then
-- in state_1 one some lights are green and others red, simple
time_counter <= time_counter + 1;
else
time_counter <= "0000000";
end if;
-- etc...
when state_6 =>
if (time_counter < state_6_time_value) then
time_counter <= time_counter + 1;
else
time_counter <= "0000000";
end if;
end case;
end if;
end process time_counter_process;
顺便说一句,您应该在设计中更喜欢同步重置。
我正在为作业编写简单的交集 "simulator"。它是这样的: 用户输入每条车道上在红绿灯处停车的汽车数量,并定义换灯次数。当定时器值等于用户定义的时间时,灯应该改变它们的颜色。
time_counter中出现问题,过程如下
time_counter_process: process (reset, clk)
begin
if reset = '1' then
time_counter<= "0000000";
elsif clk'event and clk='1' then
if (state = state_1 and time_counter < state_1_time_value) then
-- in state_1 one some lights are green and others red, simple
time_counter <= time_counter + 1;
if (state = state_1 and time_counter = state_1_time_value) then
time_counter <= "0000000";
end if;
etc...
elsif state = state_6 and time_counter < state_6_time_value then
time_counter <= time_counter + 1;
if state = state_6 and time_counter = state_6_time_value then
time_counter <= "0000000";
end if;
end if;
end if;
end process time_counter_process;
我不知道为什么,但计时器没有正确递增。它保持在值“000000”。谁能告诉我为什么它不能正常工作?
为了更清楚起见,我还将展示状态如何变化
begin
next_state<= state;
case state is
when state_1 =>
if time_counter < state_1_time_value then
traffic_signal_1 <= "10"; --green
traffic_signal_2 <= "00"; -- red
traffic_signal_3 <= "00";
traffic_signal_4 <= "10";
traffic_signal_5 <= "00";
traffic_signal_6 <= "00";
elsif time_counter = state_1_time_value then
next_state<=state_2;
end if;
您应该将大 if 语句与 case 语句交换 state
并将计数器条件实现为每个 when 选项中的 if 语句。
像time_counter = state_1_time_value
这样的条件永远不会成立,因为信号在进程结束时更新。您所在的分支中 time_counter < state_1_time_value
为真,因此 time_counter = state_1_time_value
不可能也为真。
time_counter_process: process (reset, clk)
begin
if (reset = '1') then
time_counter<= "0000000";
elsif rising_edge(clk) then
case state is
when state_1 =>
if (time_counter < state_1_time_value) then
-- in state_1 one some lights are green and others red, simple
time_counter <= time_counter + 1;
else
time_counter <= "0000000";
end if;
-- etc...
when state_6 =>
if (time_counter < state_6_time_value) then
time_counter <= time_counter + 1;
else
time_counter <= "0000000";
end if;
end case;
end if;
end process time_counter_process;
顺便说一句,您应该在设计中更喜欢同步重置。