首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

VHDL交通灯控制程序~~~

VHDL交通灯控制程序~~~

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity jtd is
port (
   clk,rst:in std_logic;
   row    ut std_logic_vector(3 downto 0);
   r,y,g  ut std_logic
  );
end jtd;
architecture one of jtd is
constant y_time :integer:=5;--黄灯时间
constant rg_time:integer:=20;--红绿灯时间
type type_state is(hx_g,hx_y,zx_g,zx_y);--自定义四种状态
signal p  :integer range 0 to 3;--扫描计数器
signal state_n :type_state;--当前状态
signal clk_500  :std_logic;--扫描时钟
signal clk_1s :std_logic;--1s时钟
signal r0,y0,g0 :std_logic;--横向路口控制信号
signal r1,y1,g1 :std_logic;--纵向路口控制信号
begin
process(clk)--1kHZ分频
  variable cnt1 : integer range 0 to 200;
  variable cnt2 : integer range 0 to 250;
begin
  if clk'event and clk='1' then
  if cnt1=200 then
   cnt1:=0;
   if cnt2=250 then
    cnt2:=0;
    clk_500<=not clk_500;
   else cnt2:=cnt2+1;
   end if;
  else cnt1:=cnt1+1;
  end if;
  end if;
end process;
process(clk_500)--1HZ分频
  variable cnt: integer range 0 to 250;
begin
  if clk_500'event and clk_500='1' then
   if(p=3) then
    p<=0;
   else p<=p+1;--扫描计数器工作于1KHZ的频率
   end if;
   if cnt=250 then
    cnt:=0;
    clk_1s<=not clk_1s;
   else cnt:=cnt+1;
   end if;
  end if;
end process;
process(rst,clk_1s)--交通灯状态控制进程
  variable load0,load1: integer;
begin
  if rst='0' then
   state_n<=hx_g;load0:=rg_time;load1:=rg_time;
   r0<='0';y0<='0';g0<='0';
   r1<='1';y1<='1';g1<='1';
  elsif clk_1s'event and clk_1s='1' then
   case state_n is
    when hx_g=> if load0=0 then--横向绿灯
       load0:=y_time;state_n<=hx_y;  
      else
       load0:=load0-1;
       r0<='0';y0<='0';g0<='1';
       r1<='1';y1<='0';g1<='0';
      end if;
    when hx_y=> if load0=0 then--横向黄灯
       load1:=y_time;state_n<=zx_y;  
      else
       load0:=load0-1;
       r0<='0';y0<='1';g0<='0';
       r1<='1';y1<='0';g1<='0';
      end if;
    when zx_y=> if load1=0 then--纵向黄灯
       load1:=rg_time;state_n<=zx_g;
      else load1:=load1-1;
       r0<='1';y0<='0';g0<='0';
       r1<='0';y1<='1';g1<='0';
      end if;
    when zx_g=>if load1=0 then--纵向绿灯
       load0:=rg_time;state_n<=hx_g;
      else load1:=load1-1;
       r0<='1';y0<='0';g0<='0';
       r1<='0';y1<='0';g1<='1';
      end if;
    when others=>r0<='1';y0<='0';g0<='0';
        r1<='1';y1<='0';g1<='0';
        state_n<=hx_g;
        load0:=rg_time;load1:=rg_time;
   end case;
  end if;
end process;
process(p)--交通灯扫描进程
   begin
  case p is
   when 0=>row<="1110";r<=r0;y<=y0;g<=g0;
   when 1=>row<="1101";r<=r1;y<=y1;g<=g1;
   when 2=>row<="1011";r<=r0;y<=y0;g<=g0;
   when 3=>row<="0111";r<=r1;y<=y1;g<=g1;
  end case;
end process;
end one;

jtd.rar (1.03 KB)

顶顶,下来看看先
返回列表