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

📄 traffic.vhd

📁 实现交通灯的vhdl程序
💻 VHD
字号:
-----交通灯设计----
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;

entity traffic is
port(clk1:   in std_logic;
	 clk2:  in std_logic;
	reset:  in std_logic;
	car_in: in std_logic;
	pout:	out std_logic_vector(12 downto 1);
	sel:    out std_logic_vector(3 downto 1);
	seg:	out std_logic_vector(7 downto 1));
end entity traffic;

architecture one of traffic is
	type states is (st0,st1,st2,st3);
	signal current_state: states;
	signal count: integer range 0 to 59;
	signal count_l: integer range 0 to 9;
	signal count_h: integer range 0 to 5;
	signal num: std_logic_vector(2 downto 0);
	signal sm: integer range 0 to 16;
	signal temp: std_logic_vector(12 downto 1);
	
begin

process(current_state,car_in,clk1,reset)
begin
if reset='1' then count<=0;current_state<=st0;
elsif clk1'event and clk1='1' then 
case current_state is
when st0 => 
			if count=59 then count<=0;
		    else count<=count+1;
			end if;
			if (car_in='1' and count=59)then current_state<=st1;
			end if;
when st1 => 
		    if count=4 then count<=0;
		    else count<=count+1;
			end if;
			if(count=4) then current_state<=st2;
			end if;
when st2 => 
			if count=29 then count<=0;
		    else count<=count+1;
			end if;
			if(car_in='0' or count=29) then current_state<=st3;
		    end if;
when st3 => 
			if count=4 then count<=0;
		    else count<=count+1;
			end if;
			if(count=4) then current_state<=st0;
			end if;
when others => null;
end case;
case count is
when 0 to 9   => count_l<=count;count_h<=0;
when 10 to 19 => count_l<=count-10;count_h<=1;
when 20 to 29 => count_l<=count-20;count_h<=2;
when 30 to 39 => count_l<=count-30;count_h<=3;
when 40 to 49 => count_l<=count-40;count_h<=4;
when 50 to 59 => count_l<=count-50;count_h<=5;
when others => null;
end case;
end if;
end process;

process(current_state)-----四种状态下交通灯的显示
begin
case current_state is
when st0 => temp<="010001010001";
when st1 => temp<="100100100100";
when st2 => temp<="001010001010";
when st3 => temp<="100100100100";
when others=> null;
end case;
pout<=temp;
end process;

process(reset,clk2)-----数码管扫描
begin
	if reset='1' then num<="000";
	elsif (clk2'event and clk2='1') then num<=num+1;
	end if;
end process;

sm<=count_l when num="000" else ----数码管第一位显示计数的个位
	count_h when num="001" else ----数码管第二位显示计数的十位
	16;
sel<=num;

process(sm)
begin
case sm is 
                ----gfedcba
when 0 => seg<= "0111111";
when 1 => seg<= "0000110";  
when 2 => seg<= "1011011";
when 3 => seg<= "1001111";
when 4 => seg<= "1100110";
when 5 => seg<= "1101101";
when 6 => seg<= "1111101";
when 7 => seg<= "0000111";
when 8 => seg<= "1111111";
when 9 => seg<= "1101111";
when 10 => seg<="1110111";
when 11 => seg<="1111100";
when 12 => seg<="0111001";
when 13 => seg<="1011110";
when 14 => seg<="1111001";
when 15 => seg<="1110001";
when 16 => seg<="0000000";
when others=> null;
end case;
end process;	 	
end one;	 	

⌨️ 快捷键说明

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