📄 jiao_tong.vhd
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity jiao_tong is
port(clk:in std_logic;----20mhz晶振时钟
jin:in std_logic;----禁止通行信号
scan:out std_logic_vector(1 downto 0);--------数码管地址选择信号
seg7:out std_logic_vector(6 downto 0);--------7段显示控制信号(abcdefg)
ra,ya,ga:out std_logic;-----------主干道的红黄绿灯
rb,yb,gb:out std_logic);----------支干道的红黄绿灯
end;
architecture one of jiao_tong is
type states is(st1,st2,st3,st4);------四种状态
signal clk1khz,clk1hz:std_logic;------分频信号包括1khz和1hz
signal one,ten:std_logic_vector(3 downto 0);--倒计时的个位和十位
signal cnt:std_logic_vector(1 downto 0);------数码管扫描计数信号
signal data:std_logic_vector(3 downto 0);
signal seg7_temp:std_logic_vector(6 downto 0);
signal r1,r2,g1,g2,y1,y2:std_logic;
begin
------------------------------------1khz分频------
process(clk)
variable count:integer range 0 to 9999;
begin
if clk'event and clk='1' then
if count=9999 then clk1khz<=not clk1khz;count:=0;
else count:=count+1;
end if;
end if;
end process;
-------------------------------------1hz分频-----
process(clk1khz)
variable count:integer range 0 to 499;
begin
if clk1khz'event and clk1khz='1' then
if count=499 then clk1hz<=not clk1hz;count:=0;
else count:=count+1;
end if;
end if;
end process;
--------------------------------------交通状态转换-----
process(clk1hz)
variable stx:states;
variable a:std_logic;-------倒计时赋值标志位
variable qh,ql:std_logic_vector(3 downto 0);-----计数的高位和低位
begin
if clk1hz'event and clk1hz='1' then
case stx is
when st1=>if jin='0' then---------------------------------------状态st1,主干道通行35秒
if a='0' then
qh:="0011";-----高位为3
ql:="0100";-----低位为4
a:='1';
r1<='0';
y1<='0';
g1<='1';-----主干道绿灯亮
r2<='1';-----支干道红灯亮
y2<='0';
g2<='0';
else
if qh=0 and ql=1 then ----如果倒计时结束则转到st2状态
stx:=st2;
a:='0';
qh:="0000";
ql:="0000";
elsif ql=0 then ------实现倒计时
ql:="1001";
qh:=qh-1;
else
ql:=ql-1;
end if;
end if;
end if;
when st2=>if jin='0' then --------------------------------状态st2,主干道黄灯倒计时5秒
if a='0' then
qh:="0000";-----高位为0
ql:="0100";-----低位为4
a:='1';
r1<='0';
y1<='1';----主干道黄灯点亮
g1<='0';
r2<='1';----支干道红灯点亮
y2<='0';
g2<='0';
else
if ql=1 then ----如果倒计时结束则转到st3状态
stx:=st3;
a:='0';
qh:="0000";
ql:="0000";
else
ql:=ql-1;
end if;
end if;
end if;
when st3=>if jin='0' then --------------------------------------状态st3,支干道通行25秒
if a='0' then
qh:="0010";----高位为2
ql:="0100";----低位为4
a:='1';
r1<='1';-------主干道红灯点亮
y1<='0';
g1<='0';
r2<='0';
y2<='0';
g2<='1';-------支干道绿灯点亮
else
if qh=0 and ql=1 then ----如果倒计时结束则转到st4状态
stx:=st4;
a:='0';
qh:="0000";
ql:="0000";
elsif ql=0 then -----实现倒计时25s
ql:="1001";
qh:=qh-1;
else
ql:=ql-1;
end if;
end if;
end if;
when st4=>if jin='0' then ----------------------------------状态st4,支干道黄灯倒计时5秒
if a='0' then
qh:="0000";----高位为0
ql:="0100";----低位为4
a:='1';
r1<='1';-------主干道红灯点亮
y1<='0';
g1<='0';
r2<='0';
y2<='1';-------支干道绿灯点亮
g2<='0';
else
if ql=1 then ----如果倒计时结束则转到st1状态
stx:=st1;
a:='0';
qh:="0000";
ql:="0000";
else
ql:=ql-1;
end if;
end if;
end if;
end case;
end if;
one<=ql;ten<=qh;
end process;
---------------------------------禁止通行信号,数码管闪烁显示---------------
process(jin,clk1hz,r1,r2,g1,g2,y1,y2,seg7_temp)
begin
if jin='1' then
ra<=r1 or jin;--------主干道红灯点亮
rb<=r2 or jin;--------支干道红灯点亮
ga<=g1 and not jin;
gb<=g2 and not jin;
ya<=y1 and not jin;
yb<=y2 and not jin;
------------------------------实现显示闪烁
seg7(0)<=seg7_temp(0) and clk1hz;
seg7(1)<=seg7_temp(1) and clk1hz;
seg7(2)<=seg7_temp(2) and clk1hz;
seg7(3)<=seg7_temp(3) and clk1hz;
seg7(4)<=seg7_temp(4) and clk1hz;
seg7(5)<=seg7_temp(5) and clk1hz;
seg7(6)<=seg7_temp(6) and clk1hz;
else
seg7<=seg7_temp;
ra<=r1;
rb<=r2;
ga<=g1;
gb<=g2;
ya<=y1;
yb<=y2;
end if;
end process;
------------------------------------数码管动态扫描计数--------
process(clk1khz)
begin
if clk1khz'event and clk1khz='1' then
if cnt="01" then cnt<="00";
else cnt<=cnt+1;
end if;
end if;
end process;
-------------------------------------数码管动态扫描-----------
process(cnt,one,ten)
begin
case cnt is
when "00"=> data<=one;scan<="01";
when "01"=> data<=ten;scan<="10";
when others=>null;
end case;
end process;
-----------------------------------------七段译码--------------------
process(data)
begin
case data is
when"0000"=>seg7_temp<="1111110";-----0显示
when"0001"=>seg7_temp<="0110000";-----1显示
when"0010"=>seg7_temp<="1101101";-----2显示
when"0011"=>seg7_temp<="1111001";-----3显示
when"0100"=>seg7_temp<="0110011";-----4显示
when"0101"=>seg7_temp<="1011011";-----5显示
when"0110"=>seg7_temp<="1011111";-----6显示
when"0111"=>seg7_temp<="1110000";-----7显示
when"1000"=>seg7_temp<="1111111";-----8显示
when"1001"=>seg7_temp<="1111011";-----9显示
when others=>seg7_temp<="1001111";----E显示,代表出错
end case;
end process;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -