此程序设计的是模拟交通灯系统的控制,其中状态:ally是东西南北方向都亮黄灯,sny、sng分别为南北方向亮黄、绿灯,同样,ewy和ewg分别为东西方向亮黄、绿灯。
本设计假设南北方向为主干道,通行时间为60秒,东西方向为次干道,通行时间为30秒,在两干道交替通行时有5秒的时间,此时两条干道的黄灯都要闪,在这个时候,原来通行的干道上的车应准备等待,原来在等待的车辆则可准备行驶;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity traffic is
port(clk:in std_logic;
rst:in std_logic;
pout:out std_logic_vector(12 downto 1));
end traffic;
architecture one of traffic is
type state is (ally,sng,sny,ewg,ewy);
signal ct:state:=ally;
signal nt:state;
signal pp :std_logic_vector(12 downto 1);
signal maxtime:bit;
signal mediatime:bit;
signal mintime:bit;
signal mintime1:bit;
signal a:bit;
signal time:integer range 0 to 60;
signal time1:integer range 0 to 30;
signal time2:integer range 0 to 5;
signal time3:integer range 0 to 5;
begin
neg:process(clk,rst) is
begin
if rst='1' then
ct<=ally;
elsif (clk'event and clk='1') then
ct<=nt;
end if;
end process neg;
com:process(ct,clk) is
begin
if rising_edge(clk) then
case ct is
when ally => pp<="100100100100";
nt<=sng;
when sng => pp<="010001010001";mediatime<='0';time1<=time1+1;
if time1=29 then mediatime<='1';end if;
if mediatime='1'then
nt<=sny;
end if;
when sny => mintime<='0';time2<=time2+1;
if a='0' then pp<="110001110001";
else pp<="010001010001";
end if;
if time2=4 then mintime<='1';end if;
if mintime='1' then
nt<=ewg;
end if;
when ewg => pp<="001010001010";maxtime<='0';time<=time+1;
if time="59" then maxtime<='1';end if;
if maxtime='1' then
nt<=ewy;
end if;
when ewy => mintime1<='0';time3<=time3+1;
if a='0' then pp<="001110001110";
else pp<="001010001010";
end if;
if time3=4 then mintime1<='1';end if;
if mintime1='1' then
nt<=sng;
end if;
when others => nt<=ally;
end case;
end if;
end process com;
la:process(ct)
begin
if rising_edge(clk) then
a<=not a;
end if;
end process la;
pout<=pp;
end one;
本人水平十分有限,错误之处难免,望大家指正。
文章评论(0条评论)
登录后参与讨论