library IEEE; 
use IEEE.STD_LOGIC_1164.ALL; 
use IEEE.STD_LOGIC_ARITH.ALL; 
use IEEE.STD_LOGIC_UNSIGNED.ALL; 
entity drink_auto_sale is 
         port(clk: in std_logic; 
                   reset:in std_logic; 
                   sw101:in std_logic; 
                   sw102:in std_logic; 
                   buy : out std_logic; 
                   back: out std_logic); 
end drink_auto_sale; 
architecture Behavioral of drink_auto_sale is 
type state_type is(st0,st1); 
signal cs ,ns : state_type; 
begin 
process(clk,reset) 
begin 
         if(reset = '1') then 
                   cs <= st0; 
         elsif(clk'event and clk = '1') then    
                   cs <= ns; 
         end if; 
end process; 
 
process(reset ,cs) 
begin 
         case cs is 
                   when st0 =>        if( sw101 = '1') then 
                                                                 ns <= st1; 
                                                                 buy<= '0'; 
                                                                 back<= '0'; 
                                                        elsif(sw102 = '1') then 
                                                                 ns <= st0; 
                                                                 buy<= '1'; 
                                                                 back <= '0'; 
                                                        else 
                                                                 ns <= st0 ; 
                                                                 buy <= '0'; 
                                                                 back <= '0'; 
                                                        end if; 
                   when st1 => if(sw101 = '1') then 
                                                                 ns <= st0; 
                                                                 buy <= '1'; 
                                                                 back <= '0'; 
                                                        elsif(sw102 = '1') then 
                                                                 ns <= st0; 
                                                                 buy <= '1'; 
                                                                 back <= '1'; 
                                                        end if; 
                   when others => ns <= st0; 
                                                                 buy<= '0'; 
                                                                 back <= '0'; 
         end  case; 
end process; 
end Behavioral; 
设 计过程:设定三个状态:0分,5分;当状态为0分时,接收到5分信号脉冲后转为5分;接收到10分信号脉冲时,转到0分状态,同时弹出饮料,不找零;状态 为5分时,接受到5分信号,弹出饮料,不找零,返回0分状态;当接受到10分状态时,弹出饮料,找零,并返回零分状态。 
 |