基于VHDL的交通灯控制器的设计

2025-04-23 22:55:05
推荐回答(1个)
回答1:

---------------------------交通灯控制器设计,led显示规律:东西方向绿灯,而南北方向红灯
---------------------------东西方向绿灯灭,黄灯亮,南北方向仍然红灯
---------------------------南北方向绿灯,而东西方向红灯--------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity jiaotongLED is
generic(-----------------------------------定义灯亮的时间
east_green_cnt:integer:=40;------------东西方向主干道绿灯
east_yellow_cnt:integer:=5;------------东西方向主干道黄灯
south_green_cnt:integer:=30;-----------南北方向支干道绿灯
south_yellow_cnt:integer:=5;-----------南北方向支干道黄灯
exi_cnt:integer:=120);-----------------紧急车辆通行时间
port(clk:in std_logic;
rst:in std_logic;----------------------复位信号
exi_sign:in std_logic;-----------------紧急车辆信号

east_green_led:out std_logic;
east_yellow_led:out std_logic;
east_red_led:out std_logic;
south_green_led:out std_logic;
south_yellow_led:out std_logic;
south_red_led:out std_logic);
end jiaotongLED;

------------------------------------
architecture ex of jiaotongLED is
type states is(s0,s1,s2,s3,s4);
signal state1:states:=s0;
signal state:states:=s0;
signal cnt:integer range 0 to 150;
signal save_cnt:integer;
signal enable_cnt:std_logic:='0';
begin
-----------------------------------------
u1:process(rst,clk)-----------------------------信号灯的时间状态的转换
begin
if rst='1' then-----------------------------判断是否按下复位
state<=s0;
cnt<=1;
elsif clk'event and clk='1' then
if enable_cnt='1' then
cnt<=cnt+1;
else
cnt<=1;
end if;
case state is
when s0=>
if exi_sign='1' then----------------判断是否紧急车辆
save_cnt<=cnt;
state1<=s0;
state<=s4;
elsif(cnt=east_green_cnt)then
state<=s1;
else
state<=s0;
end if;
when s1=>
if exi_sign='1' then
save_cnt<=cnt;
state1<=s1;
state<=s4;
elsif(cnt=east_yellow_cnt)then
state<=s2;
else
state<=s1;
end if;
when s2=>
if exi_sign='1' then
save_cnt<=cnt;
state1<=s2;
state<=s4;
elsif(cnt=south_green_cnt)then
state<=s3;
else
state<=s2;
end if;
when s3=>
if exi_sign='1' then
save_cnt<=cnt;
state1<=s3;
state<=s4;
elsif(cnt=south_yellow_cnt)then
state<=s0;
else
state<=s3;
end if;
when s4=>
if(cnt=exi_cnt)then
cnt<=save_cnt;
state<=state1;
end if;
end case;
end if;
end process u1;
---------------------------------------------------

u2:process(state)--------------------------------------信号灯的状态显示
begin
case state is
when s0=>
east_green_led<='1';
east_yellow_led<='0';
east_red_led<='0';
south_green_led<='0';
south_yellow_led<='0';
south_red_led<='1';
enable_cnt<='1';
if(cnt=east_green_cnt)then
enable_cnt<='0';-----------------------已达到东西方向绿灯亮时间,暂停计数
end if;
when s1=>
east_green_led<='0';
east_yellow_led<='1';
east_red_led<='0';
south_green_led<='0';
south_yellow_led<='0';
south_red_led<='1';
enable_cnt<='1';
if(cnt=east_yellow_cnt)then
enable_cnt<='0';
end if;
when s2=>
east_green_led<='0';
east_yellow_led<='0';
east_red_led<='1';
south_green_led<='1';
south_yellow_led<='0';
south_red_led<='0';
enable_cnt<='1';
if(cnt=south_green_cnt)then
enable_cnt<='0';
end if;
when s3=>
east_green_led<='0';
east_yellow_led<='0';
east_red_led<='1';
south_green_led<='0';
south_yellow_led<='1';
south_red_led<='0';
enable_cnt<='1';
if(cnt=south_yellow_cnt)then
enable_cnt<='0';
end if;
when s4=>
east_green_led<='0';
east_yellow_led<='0';
east_red_led<='1';
south_green_led<='0';
south_yellow_led<='0';
south_red_led<='1';
enable_cnt<='1';
if(cnt=exi_cnt)then
enable_cnt<='0';
end if;
end case;
end process u2;
end ex;

程序已经运行仿真过,应该没有问题,另外加了一个2min紧急车辆的通行时间,不需要的话可以删掉。。。。。