⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 seller2.vhd

📁 程序实现自动售货机的核心功能
💻 VHD
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity seller2 is
       port (
             clk                           :in std_logic;--时钟信号            
             reset                         :in std_logic;--复位信号            
             set_25,set_5,set_10           :in std_logic;--分别代表2.5元,5元和10元的饮料选择键             
             coin5,coin10                  :in std_logic;--分别代表5角和1元的硬币信号             
             paper1,paper2,paper5,paper10  :in std_logic;--分别代表1元,2元,5元和10元的纸币信号
             other_money                   :in std_logic;--代表自动售货机不能识别的钱 
             n1,n2,n3,n4,n5                :in std_logic;--代表输入商品数量信号
             ok,cancel,continue            :in std_logic;--代表确定,取消和继续键
             output1,output2,output3       :out std_logic;--代表商品1,2,3的输出孔
             attention                     :out std_logic;--代表输出欠资信号
             mistake                       :out std_logic;--代表投错钱币的信号
             no_choice                     :out std_logic;--代表未选商品的信号
             repay                         :out std_logic--代表找钱信号
     );
end seller2;

architecture behave of seller2 is  
    type state_type is(
                       input_state,--投币状态
                       output_state,--输出商品状态
                       cond_state,--选择商品状态
                       charge_state--找钱状态
                       );

    signal state       :state_type;
    signal coin_in:std_logic;--有硬币输入
    signal pa_in:std_logic;--有纸币输入

    begin
       coin_in<=coin5 or coin10 or other_money;
       pa_in<=paper1 or paper2 or paper5 or paper10 or other_money;
      process(clk,reset)

        variable amount:std_logic_vector(6 downto 0);--投入的钱币总额
        variable price:std_logic_vector(6 downto 0);--所需的钱币总额
        variable conduct1,conduct2,conduct3:std_logic;       
   
      begin
          if(reset='1')then                       --复位
            amount:="0000000";
            price:="0000000";
            state<=input_state;
            repay<='0';
            output1<='0';output2<='0';output3<='0';
            conduct1:='0';conduct2:='0';conduct3:='0';
            attention<='0';
            no_choice<='0';
            mistake<='0';
           
          elsif(rising_edge(clk))then
            case state is
              when input_state=>

                  --计算所投入的金额总值                               
                  if(coin_in='1') then 
                             if(coin5='1')then amount:=amount+"0000001";mistake<='0';
                             elsif(coin10='1')then amount:=amount+"0000010";mistake<='0';
                             elsif(other_money='1') then mistake<='1';
                             else null;
                             end if;
                  end if;
                  if(pa_in='1') then 
                             if(paper1='1')then amount:=amount+"0000010";mistake<='0';
                             elsif(paper2='1')then amount:=amount+"0000100";mistake<='0';
                             elsif(paper5='1')then amount:=amount+"0001010";mistake<='0';
                             elsif(paper10='1')then amount:=amount+"0010100";mistake<='0';
                             elsif(other_money='1') then mistake<='1';
                             else null;
                             end if;
                  end if;
                  output1<='0';output2<='0';output3<='0';
                  repay<='0';no_choice<='0';

                  if(set_25='1' or set_5='1' or set_10='1')then state<=cond_state;
                  end if;
                  if (ok='1' and amount>=price and price>"0000000")then state<=output_state; end if;
                  if (ok='1' and amount>=price and price="0000000")then no_choice<='1';state<=cond_state;end if;
                  if ok='1' and amount<price then attention<='1';
                           if continue='1' then state<=input_state;attention<='0';end if;
                           if cancel='1'   then state<=charge_state;price:="0000000";attention<='0';end if;
                  end if;
                  if (cancel='1') then state<=charge_state;price:="0000000"; 
                  end if;
                  attention<='0';

            when cond_state=>
                  if(n1='1')then no_choice<='0';
                              if set_25='1' then price:="0000101";conduct1:='1';conduct2:='0';conduct3:='0';
                              elsif set_5='1' then price:="0001010";conduct1:='0';conduct2:='1';conduct3:='0';
                              elsif set_10='1' then price:="0010100";conduct1:='0';conduct2:='0';conduct3:='1';
                              else null;
                              end if;
                  elsif(n2='1')then no_choice<='0'; 
                              if set_25='1' then price:="0001010";conduct1:='1';conduct2:='0';conduct3:='0';
                              elsif set_5='1' then price:="0010100";conduct1:='0';conduct2:='1';conduct3:='0';
                              elsif set_10='1' then price:="0101000";conduct1:='0';conduct2:='0';conduct3:='1';
                              else null;
                              end if;
                  elsif(n3='1')then no_choice<='0';
                              if set_25='1' then price:="0001111";conduct1:='1';conduct2:='0';conduct3:='0';
                              elsif set_5='1' then price:="0011110";conduct1:='0';conduct2:='1';conduct3:='0';
                              elsif set_10='1' then price:="0111100";conduct1:='0';conduct2:='0';conduct3:='1';
                              else null;
                              end if;

                  elsif(n4='1')then no_choice<='0';
                              if set_25='1' then price:="0010100";conduct1:='1';conduct2:='0';conduct3:='0';
                              elsif set_5='1' then price:="0101000";conduct1:='0';conduct2:='1';conduct3:='0';
                              elsif set_10='1' then price:="1010000";conduct1:='0';conduct2:='0';conduct3:='1';
                              else null;
                              end if;
                  elsif(n5='1')then no_choice<='0';
                              if set_25='1' then price:="0011001";conduct1:='1';conduct2:='0';conduct3:='0';
                              elsif set_5='1' then price:="0110010";conduct1:='0';conduct2:='1';conduct3:='0';
                              elsif set_10='1' then price:="1100100";conduct1:='0';conduct2:='0';conduct3:='1';
                              else null;
                              end if;
                  end if;

                  attention<='0';
                  output1<='0';output2<='0';output3<='0';
                  repay<='0';
                  mistake<='0';

                  if (coin_in='1' or pa_in='1')then state<=input_state;
                  end if;
                  if (ok='1') then
                         if  (amount>=price and price>"0000000") then state<=output_state;
                         elsif (amount<price) then attention<='1';
                                  if cancel='1' then state<=charge_state;price:="0000000";attention<='0';end if;
                                  if continue='1' then state<=input_state;attention<='0';end if; 
                         elsif (amount>=price and price="0000000") then no_choice<='1';
                         else null;                        
                         end if;
                  elsif(cancel='1')then state<=charge_state;price:="0000000";
                  else null;
                  end if;

            when output_state=>
                          attention<='0';mistake<='0';no_choice<='0';
                          amount:=amount-price;price:="0000000";
                          if amount>"0000000" then repay<='1';state<=charge_state;
                          elsif amount="0000000" then repay<='0';state<=input_state;
                          end if;

                          if conduct1='1' then output1<='1';
                          elsif conduct2='1' then output2<='1';
                          elsif conduct3='1' then output3<='1';
                          end if;
                  
             when charge_state=>
                        repay<='1';attention<='0';mistake<='0';no_choice<='0';price:="0000000";
                        if(continue='1') then state<=input_state;price:="0000000";
                        end if;
                        if amount="0000000"  then state<=input_state;repay<='0';
                        end if;

                         output1<='0';
                         output2<='0';
                         output3<='0';

                        if amount>"0000000" then
                            if amount>="0010100" then amount:=amount-"0010100";
                            elsif amount>="0001010" then amount:=amount-"0001010";
                            elsif amount>="0000100" then amount:=amount-"0000100";
                            elsif amount>="0000010" then amount:=amount-"0000010";
                            elsif amount>="0000001" then amount:=amount-"0000001";
                            end if;
                        end if;
               end case;
       end if;
   end process;
end behave;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -