基于VHDL的交通灯控制器的设计6
发布网友
发布时间:2023-09-27 08:26
我来回答
共2个回答
热心网友
时间:2024-12-04 06:48
---------------------------交通灯控制器设计,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紧急车辆的通行时间,不需要的话可以删掉。。。。。
热心网友
时间:2024-12-04 06:49
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
--------------------------------------------------------------------
entity exp18 is
port( Clk : in std_logic; --时钟输入
Rst : in std_logic; --复位输入
R1,R2 : out std_logic; --红灯输出
Y1,Y2 : out std_logic; --黄灯输出
G1,G2 : out std_logic; --绿灯输出
Display : out std_logic_vector(7 downto 0); --七段码管显示输出
SEG_SEL : buffer std_logic_vector(2 downto 0) --七段码管扫描驱动
);
end exp18;
--------------------------------------------------------------------
architecture behave of exp18 is
signal Disp_Temp : integer range 0 to 15;
signal Disp_Decode : std_logic_vector(7 downto 0);
signal SEC1,SEC10 : integer range 0 to 9;
signal Direction : integer range 0 to 15;
signal Clk_Count1 : std_logic_vector(9 downto 0); --产生0.5Hz时钟的分频计数器
signal Clk1Hz : std_logic;
signal Dir_Flag : std_logic; --方向标志
begin
process(Clk)
begin
if(Clk'event and Clk='1') then
if(Clk_Count1<1000) then
Clk_Count1<=Clk_Count1+1;
else
Clk_Count1<="0000000001";
end if;
end if;
end process;
Clk1Hz<=Clk_Count1(9);
process(Clk1Hz,Rst)
begin
if(Rst='0') then
SEC1<=0;
SEC10<=2;
Dir_Flag<='0';
elsif(Clk1Hz'event and Clk1Hz='1') then
if(SEC1=0) then
SEC1<=9;
if(SEC10=0) then
SEC10<=1;
else
SEC10<=SEC10-1;
end if;
else
SEC1<=SEC1-1;
end if;
if(SEC1=0 and SEC10=0) then
Dir_Flag<=not Dir_Flag;
end if;
end if;
end process;
process(Clk1Hz,Rst)
begin
if(Rst='0') then
R1<='1';
G1<='0';
R2<='1';
G2<='0';
else --正常运行
if(SEC10>0 or SEC1>3) then
if(Dir_Flag='0') then --横向通行
R1<='0';
G1<='1';
R2<='1';
G2<='0';
else
R1<='1';
G1<='0';
R2<='0';
G2<='1';
end if;
else
if(Dir_Flag='0') then --横向通行
R1<='0';
G1<='0';
R2<='1';
G2<='0';
else
R1<='1';
G1<='0';
R2<='0';
G2<='0';
end if;
end if;
end if;
end process;
process(Clk1Hz)
begin
if(SEC10>0 or SEC1>3) then
Y1<='0';
Y2<='0';
elsif(Dir_Flag='0') then
Y1<=Clk1Hz;
Y2<='0';
else
Y1<='0';
Y2<=Clk1Hz;
end if;
end process;
process(Dir_Flag)
begin
if(Dir_Flag='0') then --横向
Direction<=10;
else --纵向
Direction<=11;
end if;
end process;
process(SEG_SEL)
begin
case (SEG_SEL+1) is
when "000"=>Disp_Temp<=Direction;
when "001"=>Disp_Temp<=Direction;
when "010"=>Disp_Temp<=SEC10;
when "011"=>Disp_Temp<=SEC1;
when "100"=>Disp_Temp<=Direction;
when "101"=>Disp_Temp<=Direction;
when "110"=>Disp_Temp<=SEC10;
when "111"=>Disp_Temp<=SEC1;
end case;
end process;
process(Clk)
begin
if(Clk'event and Clk='1') then --扫描累加
SEG_SEL<=SEG_SEL+1;
Display<=Disp_Decode;
end if;
end process;
process(Disp_Temp) --显示转换
begin
case Disp_Temp is
when 0=>Disp_Decode<="00111111"; --'0'
when 1=>Disp_Decode<="00000110"; --'1'
when 2=>Disp_Decode<="01011011"; --'2'
when 3=>Disp_Decode<="01001111"; --'3'
when 4=>Disp_Decode<="01100110"; --'4'
when 5=>Disp_Decode<="01101101"; --'5'
when 6=>Disp_Decode<="01111101"; --'6'
when 7=>Disp_Decode<="00000111"; --'7'
when 8=>Disp_Decode<="01111111"; --'8'
when 9=>Disp_Decode<="01101111"; --'9'
when 10=>Disp_Decode<="01001000"; --'='
when 11=>Disp_Decode<="00010100"; --'||'
when others=>Disp_Decode<="00000000"; --全灭
end case;
end process;
end behave;
自己参考下吧