library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity TrafficLight is
Port(clk2, reset: in std_logic;
light: out std_logic_vector(11 downto 0));
end TrafficLight;
architecture BEHAVIORAL of TrafficLight is
type zt is(s0, s1, s2, s3);
signal current_state: zt;
signal clk1, clk3,clk4, clk1k: std_logic;
signal east_red, east_yellow, east_green: std_logic; --东方三种灯
signal west_red, west_yellow, west_green: std_logic; --西方三种灯
signal north_red, north_yellow, north_green: std_logic; --北方三种灯
signal south_red, south_yellow, south_green: std_logic; --南方三种灯
signal ew_high, ns_high: std_logic_vector(3 downto 0); --南北和东西方向倒计时显示的高位数
signal ew_low, ns_low: std_logic_vector(3 downto 0); --倒计时显示的低位数
begin
p1: process
variable c: integer := 0;
begin
wait until clk2'event and clk2 = '1';
if c < 50 then
c := c + 1;
else
clk1k <= not clk1k;
c := 0;
end if;
end process p1;
p11: process
variable c: integer := 0;
begin
wait until clk1k'event and clk1k = '1';
if c < 125 then
c := c + 1;
else
clk4 <= not clk4;
c:= 0;
end if;
end process p11;
p2: process(clk4) --从4hz分频到2hz,用于黄灯闪烁
begin
if rising_edge(clk4) then
clk3 <= not clk3;
end if;
end process p2;
p3:process(clk3) --从2hz分频到1hz,计数时钟
begin
if rising_edge(clk3) then
clk1 <= not clk1;
end if;
end process p3;
p4: process(reset, clk1, current_state)
begin
if(clk1 = '1' and clk1'event) then --当clk1处于上升沿
if reset = '1' then
current_state <= s0; --复位时强行进入s0状态
ns_low <= "0000";
ns_high <= "0000";
ew_low <= "0000";
ew_high <= "0000";
else
if(ns_low = "0000" and not(ns_high = "0000")) then --如果低位不够减,则高位减一,低位置9
ns_low <= "1001";
ns_high <= ns_high - 1;
else --每扫描一次,时间减一,如果低位够减,则只需低位减一,高位不变
ns_low <= ns_low - 1;
ns_high <= ns_high;
end if;
if(ew_low = "0000" and not(ew_high = "0000")) then
ew_low <= "1001";
ew_high <= ew_high - 1;
else
ew_low <= ew_low - 1;
ew_high <= ew_high;
end if;
case current_state is --检测当前状态
when s0 => --当现在是南北红,东西绿
if(ew_high = "0000" and ew_low = "0000") then --如果绿灯时间结束
current_state <= s1; --改变状态为南北红,东西黄
north_red <= '1'; south_red <= '1';
north_yellow <= '0'; south_yellow <= '0';
north_green <= '0'; south_green <= '0';
east_yellow <= '1'; west_yellow <= '1';
east_green <= '0'; west_green <= '0';
east_red <= '0'; west_red <= '0';
ew_high <= "0000"; ew_low <= "0100"; --黄灯显示时间为4
end if;
when s1 => --当现在是南北红,东西黄
if(ew_high = "0000" and ew_low = "0000") then --黄灯时间结束
current_state <= s2; --南北绿,东西红
north_green <= '1'; south_green <= '1';
north_red <= '0'; south_red <= '0';
north_yellow <= '0'; south_yellow <= '0';
east_red <= '1'; west_red <= '1';
east_green <= '0'; west_green <= '0';
east_yellow <= '0'; west_yellow <= '0';
ns_high <= "0001"; ns_low <= "1001"; --绿灯显示时间为19
ew_high <= "0010"; ew_low <= "0100"; --红灯显示时间为24,多出来1因为要对应两个0
end if;
when s2 => --当现在是南北绿,东西红
if(ns_high = "0000" and ns_low = "0000") then --绿灯时间结束
current_state <= s3; --南北黄,东西红
north_yellow <= '1'; south_yellow <= '1';
north_green <= '0'; south_green <= '0';
north_red <= '0'; south_red <= '0';
east_red <= '1'; west_red <= '1';
east_green <= '0'; west_green <= '0';
east_yellow <= '0'; west_yellow <= '0';
ns_high <= "0000"; ns_low <= "0100"; --黄灯时间为4
end if;
when s3 => --当现在是南北黄,东西红
if(ns_high = "0000" and ns_low = "0000") then --黄灯时间结束
current_state <= s0; --南北红,东西绿
north_red <= '1'; south_red <= '1';
north_green <= '0'; south_green <= '0';
north_yellow <= '0'; south_yellow <= '0';
east_green <= '1'; west_green <= '1';
east_red <= '0'; west_red <= '0';
east_yellow <= '0'; west_yellow <= '0';
ns_high <= "0010"; ns_low <= "0100"; --红灯时间为24
ew_high <= "0001"; ew_low <= "1001"; --绿灯时间为19
end if;
end case;
end if;
end if;
end process;
light(0) <= east_red; light(1) <= east_green;
light(2) <= east_yellow and clk3; --实现黄灯闪烁
light(3) <= south_red; light(4) <= south_green;
light(5) <= south_yellow and clk3; --实现黄灯闪烁
light(6) <= west_red; light(7) <= west_green;
light(8) <= west_yellow and clk3; --实现黄灯闪烁
light(9) <= north_red; light(10) <= north_green;
light(11) <= north_yellow and clk3; --实现黄灯闪烁
end BEHAVIORAL;
http://wenku.baidu.com/view/759eafc1aa00b52acfc7ca52.html?st=1,自己下载去,完了给个好评!
百度文库里面有