TRAFFIC_FSM.VHD
上传用户:dgjihui88
上传日期:2013-07-23
资源大小:43k
文件大小:6k
源码类别:

VHDL/FPGA/Verilog

开发平台:

MultiPlatform

  1. LIBRARY IEEE;   
  2. USE IEEE.std_logic_1164.all;
  3. USE IEEE.std_logic_arith.all;
  4. USE IEEE.std_logic_unsigned.all;   
  5. entity traffic_FSM is
  6.   port(reset:in std_logic;
  7.        clk:in std_logic;
  8.        ena_scan:in std_logic;
  9.        ena_1Hz:in std_logic;
  10.        flash_1Hz:in std_logic;
  11.        a_m:in std_logic;
  12.        st_butt:in std_logic;
  13.        next_state: in std_logic;
  14.        recount: out std_logic;
  15.        sign_state: out std_logic_vector(1 downto 0);
  16.        red: out std_logic_vector(1 downto 0);
  17.        green: out std_logic_vector(1 downto 0);
  18.        yellow: out std_logic_vector(1 downto 0));
  19. end;   
  20. architecture BEHAVIOR of traffic_FSM is
  21. type Sreg0_type is (r0g1, r0y1, g0r1, y0r1, y0y1, y0g1, g0y1, r0r1);
  22. signal state: Sreg0_type;     
  23. signal st_transfer: std_logic;
  24. signal light: std_logic_vector(5 downto 0); -- r(10)y(10)g(10)
  25. begin  
  26.   rebounce:process (reset,clk,ena_scan,st_butt) 
  27.   variable rebn_ff: std_logic_vector(5 downto 0);
  28.   begin 
  29.      if (st_butt='1' or reset='1') then
  30.          rebn_ff := "111111"; 
  31.          st_transfer <='0';
  32.      elsif (clk'event and clk='1') then
  33.        if (ena_scan='1') then 
  34.           if (rebn_ff >= 3) then 
  35.              rebn_ff := rebn_ff-1; 
  36.              st_transfer<='0';
  37.           elsif (rebn_ff=2) then       
  38.              rebn_ff := rebn_ff-1; 
  39.              st_transfer <='1';
  40.           else
  41.              rebn_ff := rebn_ff; 
  42.              st_transfer <='0';
  43.           end if;
  44.        end if;
  45.      end if;  
  46.   end process;  
  47.   FSM: process (clk,ena_1Hz,reset)
  48.   begin
  49.       if (reset='1') then    
  50.           state<=r0g1; -- red=2'b01; green=2'b10; yellow=2'b00;
  51.           sign_state<="01";
  52.           recount<='1';    
  53.       else
  54.         if (clk'event and clk='1') then
  55.           case STATE is
  56.                 when r0g1 =>  -- now state: red0 on green1 on  
  57.                   if (a_m='1' and ena_1Hz='1') then
  58.                     if (next_state = '1') then
  59.                         recount<='1';
  60.                         state<=r0y1;
  61.                         sign_state <= "01";
  62.                     else
  63.                         recount<='0';
  64.                         state<=r0g1; --red=2'b01; green=2'b10; yellow=2'b00;
  65.                     end if; 
  66.                   elsif (a_m='0' and ena_scan='1') then
  67.                     if (st_transfer='0') then -- 0: unchange 1:transfer light state
  68.                         recount<='1';
  69.                         state<=r0g1;  
  70.                     else 
  71.                         recount<='1';
  72.                         state<=r0y1;  
  73.                         sign_state <= "01";  
  74.                     end if;
  75.                   end if;
  76.                 when r0y1 =>  -- now state: red0 on yellow1 flash       
  77.                   if (a_m='1' and ena_1Hz='1') then
  78.                     if (next_state = '1') then
  79.                         recount<='1';
  80.                         state<=g0r1;
  81.                         sign_state <= "10";
  82.                     else
  83.                         recount<='0';
  84.                         state<=r0y1; -- red=2'b01; green=2'b00; yellow=2'b10;
  85.                     end if;
  86.                   elsif (a_m='0' and ena_scan='1') then
  87.                     if (st_transfer='0') then -- 0: unchange 1:transfer light state
  88.                         recount<='1';
  89.                         state<=r0y1; 
  90.                     else
  91.                         recount<='1';
  92.                         state<=g0r1;  
  93.                         sign_state <= "10";  
  94.                     end if;
  95.                   end if;
  96.                 when g0r1 =>  -- now state: green0 on red1 on 
  97.                   if (a_m='1' and ena_1Hz='1') then
  98.                     if (next_state = '1') then
  99.                         recount<='1';
  100.                         state<=y0r1;
  101.                         sign_state <= "11";
  102.                     else
  103.                         recount<='0';
  104.                         state<=g0r1; -- red=2'b10; green=2'b01; yellow=2'b00;
  105.                     end if;    
  106.                   elsif (a_m='0' and ena_scan='1') then
  107.                     if (st_transfer='0') then -- 0: unchange 1:transfer light state
  108.                         recount<='1';
  109.                         state<=g0r1; 
  110.                     else
  111.                         recount<='1';
  112.                         state<=y0r1;   
  113.                         sign_state <= "11";  --
  114.                     end if;
  115.                   end if;
  116.                 when y0r1 =>  -- now state: green0 on red1 on
  117.                   if (a_m='1' and ena_1Hz='1') then
  118.                     if (next_state = '1') then
  119.                         recount<='1';
  120.                         state<=r0g1;
  121.                         sign_state <= "00";
  122.                     else
  123.                         recount<='0';
  124.                         state<=y0r1; -- red=2'b10; green=2'b00; yellow=2'b01;
  125.                     end if;
  126.                   elsif (a_m='0' and ena_scan='1') then
  127.                     if (st_transfer='0') then -- 0: unchange 1:transfer light state
  128.                         recount<='1';
  129.                         state<=y0r1; 
  130.                     else
  131.                         recount<='1';
  132.                         state<=r0g1;       
  133.                         sign_state <= "00";  --
  134.                     end if;
  135.                   end if;
  136.                 when others =>
  137.                     state<=r0g1;
  138.                     recount<='0';
  139.                     sign_state <= "00";
  140.           end case; 
  141.         end if;
  142.       end if;   
  143.   end process;  
  144.   -- light: r(10)y(10)g(10)
  145.   light <= "010010" when (state=r0g1) else
  146.            "011000" when (state=r0y1) else
  147.            "100001" when (state=g0r1) else
  148.            "100100" when (state=y0r1) else
  149.            "110000"; 
  150.   red <= light(5 downto 4);
  151.   yellow <= light(3 downto 2) and (flash_1Hz & flash_1Hz);
  152.   green <= light(1 downto 0);
  153. end BEHAVIOR;