Vhdl 交通灯控制器

Vhdl 交通灯控制器,vhdl,state-machine,Vhdl,State Machine,下面是一个交通灯控制器的简单代码。它根据计数器值在状态之间循环。然而,我希望当按下按钮时,它在第一个状态上再停留10秒。我想知道我该怎么做 library IEEE; use IEEE.STD_LOGIC_1164.ALL; use ieee.std_logic_unsigned.all; entity TLC is Port ( Trafficlights: out STD_LOGIC_Vector (5 downto 0);

下面是一个交通灯控制器的简单代码。它根据计数器值在状态之间循环。然而,我希望当按下按钮时,它在第一个状态上再停留10秒。我想知道我该怎么做

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_unsigned.all;

entity TLC is
    Port (
              Trafficlights: out  STD_LOGIC_Vector (5 downto 0);
              Clck : in  STD_LOGIC;
              Reset : in  STD_LOGIC;
              P_B : in STD_LOGIC);
 end TLC;

architecture Behavioral of TLC is
  type state_type is (st0_R1_G2, st1_R1_A1_A2, st2_G1_R2, st3_A1_R2_A2); 

  signal state: state_type; 
  signal count : std_logic_vector (3 downto 0);

  constant sec10 : std_logic_vector ( 3 downto 0) := "1010";
  constant sec2 : std_logic_vector (3 downto 0 ) := "0010";
  constant sec16: std_logic_vector (3 downto 0 ) := "1111";
begin
  process (Clck,Reset)
  begin
    if Reset='1' then
      state <= st0_R1_G2;         --reset to initial state
      count <= X"0";             -- reset counter
    elsif Clck' event and Clck = '1' then --rising edge
       case (state) is              ---state transitions
         when st0_R1_G2 =>
           if count < sec10 then
             state <= st0_R1_G2;
             count <= count + 1;                
           else  
             state <= st1_R1_A1_A2;
             count <= X"0";
           end if;

         when st1_R1_A1_A2 =>
           if count < sec2 then
             state <=  st1_R1_A1_A2;
             count <= count + 1;
           else
             state <= st2_G1_R2;
             count <= X"0";
           end if;

         when st2_G1_R2 =>
           if count < sec10 then
             state <= st2_G1_R2;
             count <= count + 1;
           else
             state <= st3_A1_R2_A2;
             count <= X"0";
           end if;

         when st3_A1_R2_A2 =>   
           if count < sec2 then
             state <=  st3_A1_R2_A2;
             count <= count + 1;
           else
             state <=st0_R1_G2;
             count <= X"0";
           end if; 

         when others =>
           state <= st0_R1_G2;
       end case;      
     end if;
   end process;

   OUTPUT_DECODE: process (state)
   begin
     case state is 
       when st0_R1_G2 =>    Trafficlights <= "100001";  -- Traffic Red 1, Pedestrian Green 1 
       when st1_R1_A1_A2 => Trafficlights <= "110010";
       when st2_G1_R2 =>    Trafficlights <= "001100";
       when st3_A1_R2_A2 => Trafficlights <= "010110";
       when others =>       Trafficlights <= "100001";
     end case; 
   end process;
 end Behavioral;
IEEE库;
使用IEEE.STD_LOGIC_1164.ALL;
使用ieee.std_logic_unsigned.all;
实体TLC为
港口(
交通灯:输出标准逻辑向量(5到0);
Clck:标准逻辑中;
复位:在标准逻辑中;
P_B:标准逻辑中);
末端薄层色谱;
TLC的结构是
类型状态_类型为(st0_R1_G2、st1_R1_A1_A2、st2_G1_R2、st3_A1_R2_A2);
信号状态:状态_型;
信号计数:标准逻辑向量(3到0);
常数sec10:std_逻辑_向量(3到0):=“1010”;
常数sec2:std_逻辑_向量(3到0):=“0010”;
常数sec16:std_逻辑_向量(3到0):=“1111”;
开始
过程(Clck,复位)
开始
如果Reset='1',则

state我没有模拟这个

想法是将
计数
计数器扩展一位,包括额外的10秒。输入
p_b
用于异步设置按钮事件锁存器,该锁存器用于启动扩展计数,可能允许人行横道交通在前10秒内通过

它由JK触发器(
crosstime
)控制。此处值得注意的特点是,仅在延长的20秒期间的前10秒启用人行横道。你不会突然允许过河的

所有这些都假设你的按钮是一个按需行人过街请求,你不解释你的州名,也不解释你的灯

我猜你会想用
crosstime
打开行人绿灯

(如果这不起作用或者不是你想要的,你就得到了你付出的。)

ieee库;
使用ieee.std_logic_1164.all;
使用ieee.std_logic_unsigned.all;
实体tlc为
港口(
交通灯:输出标准逻辑向量(5到0);
clck:标准逻辑中;
复位:在标准逻辑中;
p_b:在标准逻辑中
);
末端实体薄层色谱;
tlc的结构是
类型状态_类型为(st0_r1_g2、st1_r1_a1_a2、st2_g1_r2、st3_a1_r2_a2);
信号状态:状态_型;
信号计数:标准逻辑向量(4到0);--(3至0);
信号pb_事件:标准_逻辑;
信号交叉时间:标准逻辑;——JK FF
常数sec10:std_逻辑_向量(4到0):=“01010”——(3至0):=“1010”;
常数sec2:std_逻辑_向量(4到0):=“00010”——(3至0):=“0010”;
--常数sec16:std_逻辑_向量(4到0):=“01111”——(3至0):=“1111”;
常数sec20:std_逻辑_向量(4到0):=“10100”——新的
开始
过程(clck、reset、p_b)——添加按钮
开始
如果p_b='1'和reset='0',则为按钮设置异步

pb_事件,因此您需要第二个计数器,它的计数时间与需要经过10秒的时间相同。如果主时钟频率乘以10减去1:),则循环计数是倒数。不要被
wait
语句等抛出。它们只能在模拟中工作。“时间”在已实现的VHDL中没有值,您需要计算时钟周期。尝试了这个方法,它成功了。这是有意义的,因此JK作为按钮动作的存储器,crosstime触发适当的计数器。感谢bunchHello David,我尝试合成它,我得到一个错误,说“无法合成信号pb_事件,同步描述错误。当前软件版本不支持您用于描述同步元素(寄存器、内存等)的描述样式。”您认为这是因为它是异步的,并且与其他同步设计不兼容吗?
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity tlc is
    port (
        trafficlights:  out std_logic_vector (5 downto 0);
        clck:           in  std_logic;
        reset:          in  std_logic;
        p_b:            in std_logic
    );
 end entity tlc;

architecture behavioral of tlc is
    type state_type is (st0_r1_g2, st1_r1_a1_a2, st2_g1_r2, st3_a1_r2_a2); 

    signal state: state_type; 
    signal count: std_logic_vector  (4 downto 0) ; -- (3 downto 0);
    signal pb_event:    std_logic;
    signal crosstime: std_logic;  -- JK FF

    constant sec10 : std_logic_vector (4 downto 0) := "01010"; -- ( 3 downto 0) := "1010";
    constant sec2 : std_logic_vector (4 downto 0) := "00010"; -- (3 downto 0 ) := "0010";
    -- constant sec16: std_logic_vector (4 downto 0) := "01111"; -- (3 downto 0 ) := "1111";
    constant sec20: std_logic_vector (4 downto 0) := "10100"; -- new
begin

    process (clck, reset, p_b)  -- added push button
    begin
        if p_b = '1' and reset = '0' then  -- asynch set for push button
            pb_event <= '1';
        end if;
        if reset = '1' then
            state <= st0_r1_g2;         -- reset to initial state
            count <= (others => '0');   -- reset counter
            pb_event <= '0';
            crosstime  <= '0';
        elsif clck' event and clck = '1' then -- rising edge
            if pb_event = '1' and count = "00000" then  -- J input
                crosstime <= '1';
            end if;
            case (state) is              -- state transitions
                when st0_r1_g2 =>
                    if (crosstime = '0' and count < 20) or 
                       (crosstime = '1' and count < sec10) then
                        state <= st0_r1_g2;
                        count <= count + 1;
                    else  
                        state <= st1_r1_a1_a2;
                        count <= "00000"; -- x"0";
                        if crosstime = '1' then   -- K input
                            crosstime <= '0';
                            pb_event <= '0';
                        end if;
                    end if;
                when st1_r1_a1_a2 =>
                    if count < sec2 then
                        state <=  st1_r1_a1_a2;
                        count <= count + 1;
                    else
                        state <= st2_g1_r2;
                        count <= (others => '0');
                    end if;
                when st2_g1_r2 =>
                    if count < sec10 then
                        state <= st2_g1_r2;
                        count <= count + 1;
                    else
                        state <= st3_a1_r2_a2;
                        count <= (others => '0');
                    end if;
                when st3_a1_r2_a2 =>   
                    if count < sec2 then
                        state <=  st3_a1_r2_a2;
                        count <= count + 1;
                    else
                        state <=st0_r1_g2;
                        count <= (others => '0');
                    end if; 
                when others =>
                    state <= st0_r1_g2;
            end case;
        end if;
    end process;

output_decode: 
    process (state)
    begin
        case state is 
            when st0_r1_g2 =>    trafficlights <= "100001";  -- traffic red 1, pedestrian green 1 
            when st1_r1_a1_a2 => trafficlights <= "110010";
            when st2_g1_r2 =>    trafficlights <= "001100";
            when st3_a1_r2_a2 => trafficlights <= "010110";
            when others =>       trafficlights <= "100001";
        end case; 
    end process;
end architecture behavioral;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity tlc is
    port (
        trafficlights:  out std_logic_vector (5 downto 0);
        clck:           in  std_logic;
        reset:          in  std_logic;
        p_b:            in std_logic
    );
 end entity tlc;

architecture behavioral of tlc is
    type state_type is (st0_r1_g2, st1_r1_a1_a2, st2_g1_r2, st3_a1_r2_a2); 

    signal state: state_type; 
    signal count: std_logic_vector  (4 downto 0) ;
    signal pb_event:    std_logic;
    signal crosstime: std_logic;  -- JK FF

    constant sec10 : std_logic_vector (4 downto 0) := "01010"; 
    constant sec2 : std_logic_vector (4 downto 0) := "00010";
    constant sec20: std_logic_vector (4 downto 0) := "10100";

begin

pbevent:
    process (clck, reset, p_b)
    begin
        if p_b = '1' or reset = '1' then -- async set for push button
            pb_event <= '1';             -- reset will give crosswalk event
        elsif clck'event and clck = '1' then
            if state = st0_r1_g2 and 
              (( crosstime = '1' and count = sec10) or count = sec20) then
                pb_event <= '0';
            end if;
        end if;
    end process;
unlabelled:
    process (clck, reset) 
    begin

        if reset = '1' then
            state <= st0_r1_g2;         -- reset to initial state
            count <= (others => '0');   -- reset counter
            crosstime  <= '0';
        elsif clck'event and clck = '1' then
            if pb_event = '1' and count = "00000" then  -- J input
                crosstime <= '1';
            end if;
            case (state) is
                when st0_r1_g2 =>
                    if (crosstime = '0' and count < sec20) or 
                       (crosstime = '1' and count < sec10) then
                        state <= st0_r1_g2;
                        count <= count + 1;
                    else  
                        state <= st1_r1_a1_a2;
                        count <= "00000";
                        if crosstime = '1' then   -- K input
                            crosstime <= '0';
                        end if;
                    end if;
                when st1_r1_a1_a2 =>
                    if count < sec2 then
                        state <=  st1_r1_a1_a2;
                        count <= count + 1;
                    else
                        state <= st2_g1_r2;
                        count <= (others => '0');
                    end if;
                when st2_g1_r2 =>
                    if count < sec10 then
                        state <= st2_g1_r2;
                        count <= count + 1;
                    else
                        state <= st3_a1_r2_a2;
                        count <= (others => '0');
                    end if;
                when st3_a1_r2_a2 =>   
                    if count < sec2 then
                        state <=  st3_a1_r2_a2;
                        count <= count + 1;
                    else
                        state <=st0_r1_g2;
                        count <= (others => '0');
                    end if; 
                when others =>
                    state <= st0_r1_g2;
            end case;
        end if;
    end process;
output_decode: 
    process (state)
    begin
        case state is 
            when st0_r1_g2 =>    trafficlights <= "100001";  -- traffic red 1, pedestrian green 1 
            when st1_r1_a1_a2 => trafficlights <= "110010";
            when st2_g1_r2 =>    trafficlights <= "001100";
            when st3_a1_r2_a2 => trafficlights <= "010110";
            when others =>       trafficlights <= "100001";
        end case; 
    end process;
end architecture behavioral;