⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jiao_tong.vhd

📁 FPGA和VHDL的全过程和源码
💻 VHD
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.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段显示控制信号(abcdefgh)
ra,ya,ga:out std_logic; -- 主干道的红黄绿灯
rb,yb,gb:out std_logic); -- 支干道的红黄绿灯
end jiao_tong;
architecture onejt of jiao_tong is
	type states is (st1,st2,st3,st4); -- 4种状态
	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='1' then -- 状态st1,主干道通行35s
			if a='0' then
				qh:="0000"; -- 高位为3
				ql:="0111"; -- 低位为4
				a:='1';
				r1<='1';
				y1<='1';
				g1<='0'; -- 主干道绿灯亮
				r2<='0'; -- 支干道红灯亮
				y2<='1';
				g2<='1';
			else
				if qh="0000" and ql="0001" then -- 如果倒计时结束,则转到st2状态
					stx:=st2;
					a:='0';
					qh:="0000";
					ql:="0000";
				elsif ql="0000" then -- 实现倒计时35s
					ql:="1001";
					qh:=qh-1; -- 隐式转换
					--qh:=CONV_STD_LOGIC_VECTOR((CONV_INTEGER(qh)-1),4); -- 显式转换
				else
					ql:=ql-1;
				end if;
			end if;
		  end if;
when st2=>if jin='1' then -- 状态st2,主干道黄灯倒计时5s
			if a='0' then
				qh:="0000"; -- 高位为0
				ql:="0100"; -- 低位为4
				a:='1';
				r1<='1';
				y1<='0'; -- 主干道黄灯亮
				g1<='1';
				r2<='0'; -- 支干道红灯亮
				y2<='1';
				g2<='1';
			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='1' then -- 状态st3,支干道通行35s
			if a='0' then
				qh:="0000"; -- 高位为2
				ql:="0111"; -- 低位为4
				a:='1';
				r1<='0'; -- 主干道红灯亮
				y1<='1';
				g1<='1';
				r2<='1';
				y2<='1';
				g2<='0'; -- 支干道绿灯亮
			else
				if 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='1' then -- 状态st4,支干道黄灯倒计时5s
			if a='0' then
				qh:="0000"; -- 高位为0
				ql:="0100"; -- 低位为4
				a:='1';
				r1<='0'; -- 主干道红灯亮
				y1<='1';
				g1<='1';
				r2<='1';
				y2<='0'; -- 支干道黄灯亮
				g2<='1';
			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='0' then
	ra<=r1 and jin; -- 主干道红灯亮
	rb<=r2 and jin; -- 支干道红灯亮
	ga<=g1 or not jin;
	gb<=g2 or not jin;
	ya<=y1 or not jin;
	yb<=y2 or not jin;
	-- 实现数码管闪烁
	seg7(0)<=seg7_temp(0) or clk1hz;
	seg7(1)<=seg7_temp(1) or clk1hz;
	seg7(2)<=seg7_temp(2) or clk1hz;
	seg7(3)<=seg7_temp(3) or clk1hz;
	seg7(4)<=seg7_temp(4) or clk1hz;
	seg7(5)<=seg7_temp(5) or clk1hz;
	seg7(6)<=seg7_temp(6) or 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;
-- 7段译码
process(data)
begin
case data is
when "0000"=>seg7_temp<="1000000"; -- 0
when "0001"=>seg7_temp<="1111001"; -- 1
when "0010"=>seg7_temp<="0100100"; -- 2
when "0011"=>seg7_temp<="0110000"; -- 3
when "0100"=>seg7_temp<="0011001"; -- 4
when "0101"=>seg7_temp<="0010010"; -- 5
when "0110"=>seg7_temp<="0000010"; -- 6
when "0111"=>seg7_temp<="1111000"; -- 7
when "1000"=>seg7_temp<="0000000"; -- 8
when "1001"=>seg7_temp<="0010000"; -- 9
when others=>seg7_temp<="0000110"; -- E,出错
end case;
end process;
end onejt;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -