library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity traffic_light is
Port (
clk : in STD_LOGIC; --系统时钟100MHZ
jin : in STD_LOGIC; --禁止通行信号
scan : out STD_LOGIC_VECTOR (3 downto 0); --数码管片选信号
seg7 : out STD_LOGIC_VECTOR (6 downto 0); --数码管七段显示控制信号abcdefg
a_ryg : out STD_LOGIC_VECTOR (2 downto 0); --主干道红、黄、蓝灯
b_ryg : out STD_LOGIC_VECTOR (2 downto 0) --支干道红、黄、蓝灯
);
end traffic_light;
architecture Behavioral of traffic_light is
type states is (s1,s2,s3,s4); --定义四种状态
signal state : states:=s1; --当前状态
signal clk_1khz,clk_1hz : std_logic:='0'; --分频信号1khz,1hz
signal count1 : integer range 0 to 49999:=0; --1khz分频计数信号
signal count2 : integer range 0 to 499:=0; --1hz分频计数信号
signal ten,one : std_logic_vector(3 downto 0); --倒计时的十位、个位
signal cnt : integer range 0 to 1:=0; --数码管扫描计数信号
signal data : std_logic_vector(3 downto 0);
signal seg7_temp : std_logic_vector(6 downto 0):=(others=>'1');
signal a : std_logic_vector(2 downto 0); --主干道红、黄、绿交通灯
signal b : std_logic_vector(2 downto 0); --支干道红、黄、绿交通灯
signal qh : std_logic_vector(3 downto 0); --计数的高位
signal ql : std_logic_vector(3 downto 0); --计数的低位
begin
--1khz分频计数
count1_gen:process(clk)
begin
if(clk'event and clk='1')then
if(count1=49999)then -- N=(clk/clk_1khz)/2-1
count1<=0;
else
count1<=count1+1;
end if;
end if;
end process;
--1khz分频(用于扫描数码管)
clk_1khz_gen:process(clk)
begin
if(clk'event and clk='1')then
if(count1=0)then
clk_1khz<=not clk_1khz;
else
clk_1khz<=clk_1khz;
end if;
end if;
end process;
--1hz分频计数