有限状态机及其状态由两个过程控制

时间:2018-06-08 08:44:15

标签: vhdl state-machine fsm

我现在正试图用VHDL语言编写有限状态机(FSM)(我实际上是VHDL的新手)。我想要实现的是,只要机器在 S11 中, STint 将分别随着 CLK2 而减少(所以我可以控制减少的速度)。但是, S0 直到 S10 CLK1 控制。

这是我的代码:

begin
 process(state,DI,HI,QI,rst)      
 begin
    case State is
      when S0 =>
          DO <= '0'; HO <= '0'; QO <= '0'; ST <= '0'; STint <= 9;
        if DI'EVENT and DI = '1' then Nextstate <= S4;
          elsif HI'EVENT and HI = '1' then Nextstate <= S2;
          elsif QI'EVENT and QI = '1' then Nextstate <= S1;
          elsif rst'EVENT and rst = '1' then Nextstate <= S10;
        else Nextstate <= S0; end if;
      when S1 =>
          DO <= '0'; HO <= '0'; QO <= '0'; ST <= '0';
        if DI'EVENT and  DI = '1' then Nextstate <= S5;
          elsif HI'EVENT and HI = '1' then Nextstate <= S3;
          elsif QI'EVENT and QI = '1' then Nextstate <= S2;
          elsif rst'EVENT and rst = '1' then Nextstate <= S10;
        else Nextstate <= S1; end if;
      when S2 =>
          DO <= '0'; HO <= '0'; QO <= '0'; ST <= '0';
        if DI'EVENT and DI = '1' then Nextstate <= S6;
          elsif HI'EVENT and HI = '1' then Nextstate <= S4;
          elsif QI'EVENT and QI = '1' then Nextstate <= S3;
          elsif rst'EVENT and rst = '1' then Nextstate <= S10;
        else Nextstate <= S2; end if;  
      when S3 =>
          DO <= '0'; HO <= '0'; QO <= '0'; ST <= '0';
        if DI'EVENT and DI = '1' then Nextstate <= S7;
          elsif HI'EVENT and HI = '1' then Nextstate <= S5;
          elsif QI'EVENT and QI = '1' then Nextstate <= S4;
          elsif rst'EVENT and rst = '1' then Nextstate <= S10;
        else Nextstate <= S3; end if;       
      when S4 =>
          DO <= '0'; HO <= '0'; QO <= '0'; ST <= '0';
        if DI'EVENT and DI = '1' then Nextstate <= S8;
          elsif HI'EVENT and HI = '1' then Nextstate <= S6;
          elsif QI'EVENT and QI = '1' then Nextstate <= S5;
          elsif rst'EVENT and rst = '1' then Nextstate <= S10;
        else Nextstate <= S4; end if;  
      when S5 =>
          DO <= '0'; HO <= '0'; QO <= '0'; ST <= '0';
        if DI'EVENT and DI = '1' then Nextstate <= S9;
          elsif HI'EVENT and HI = '1' then Nextstate <= S7;
          elsif QI'EVENT and QI = '1' then Nextstate <= S6;
          elsif rst'EVENT and rst = '1' then Nextstate <= S10;
        else Nextstate <= S5; end if;  
      when S6 =>
        DO <= '0'; HO <= '0'; QO <= '0'; ST <= '0';
          Nextstate <= S11;      
      when S7 =>
        DO <= '0'; HO <= '0'; QO <= '1'; ST <= '0';
          QOint <= -1;
          Nextstate <= S11;
      when S8 =>
        DO <= '0'; HO <= '1'; QO <= '0'; ST <= '0';
          HOint <= HOint -1;
          Nextstate <= S11;
      when S9 =>
        DO <= '0'; HO <= '1'; QO <= '1'; ST <= '0';
          HOint <= HOint -1;
          QOint <= QOint -1;
          Nextstate <= S11;
      when S10 =>                 
        DOint <= 9; HOint <= 9; QOint <= 9; STint <= 9;
          Nextstate <= S0;
      when others => null;          
    end case;
  end process;

  process(CLK1)   
  begin
   if CLK1'EVENT and CLK1 = '1' then
      State <= Nextstate;
   end if;
  end process;

  process(state,CLK2)
  begin
   case State is 
       when S11 =>
        DO <= '0'; HO <= '0'; QO <= '0'; ST <= '1';
    if CLK2'EVENT and CLK2 = '1' then STint <= STint -1;
    elsif STint <= 0 then Nextstate <= S0; end if;
       when others => null;
    end case;
  end process;


我已经尝试了很多替代方案,例如使用 CLK1 作为 STint 减少的时钟,但是由于其他它没有运行良好> int (s)将一次又一次减少。
有关详细信息,此FSM实际上与自动售货机类似,但在 S11 中,计时器将开始倒计时,然后转到下一个状态,即 S0 。其他状态仅用于输入(例如货币)和输出(例如更改)。在 S11 中,只要定时器仍在倒计时, ST LED将亮起。
有什么办法可以实现吗?如果有人能指出我为什么我的代码无法工作,我会很高兴。似乎机器在 S11 时,它无法改变其状态。这就是我的代码在TINA中的样子: enter image description here

非常感谢你。祝你今天愉快!
PS:计时器显示在 STcount 输出

1 个答案:

答案 0 :(得分:1)

您的方法存在一些问题:

  • FSM的多个时钟。您应避免在设计中使用多个时钟,并仅在必要时使用它。为给定电路设置两个时钟是问题。
  • 使用单个时钟。如果你想使用时钟保持控制,可以构建一个独立的电路来控制时钟的产生。计数器可以解决简单的设计问题。更复杂的设计可能需要使用特定的硬件来生成时钟(例如PLL)。
  • 您正在分配至少DO的不同过程中的信号。这不应该合成。

我建议你重新考虑你的状态机控制并理解降低速度的限制。