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

📄 sh_machine.vhd

📁 自动售货机:先投入一定数目货币
💻 VHD
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity sh_machine is
port (           clk: in std_logic;                          --系统时钟
set,sel,cancel,finish: in std_logic;                        --设定、买、选择、完成信号
            add,coin0: in bit;                             --增加货物的数量及1元硬币
get,choice_a,choice_b: in bit;                              --货物类型的选择跳转按钮及选A或选B按钮
        goods1,goods2: buffer std_logic_vector(3 downto 0);    --货物号的显示        
              display: buffer std_logic_vector(15 downto 0);            --十六种货物的显示灯
                
       money0,money1 : buffer std_logic_vector(7 downto 0));             --放入的钱数、返回的钱数的显示输出端
end sh_machine;

architecture xiao of sh_machine is

type  ram_type is array(15 downto 0)of std_logic_vector(7 downto 0); 
signal ram :ram_type;                                           --定义RAM
signal item: std_logic_vector(3 downto 0);                      --商品种类
signal coin: std_logic_vector(7 downto 0);                      --币数计数器
signal pri,qua:std_logic_vector(3 downto 0); 

begin

--数量和价格的初始化
init:process(set,clk,item)
begin
 if clk 'event and clk='1' then  
    if set='1' then         --设定商品的价格和数量
        if add='1' then
         qua <=qua+1; 
        else
            if item<"1000" then
                 pri<="0010";
                 ram(conv_integer(item))<=pri & qua;
                 item<=item+1 ;
                 qua <="0000";
            else
                 pri<="0101";
                 ram(conv_integer(item))<=pri & qua;
                 item<=item+1;
                 qua <="0000" ;
           end if;
        end if;
     elsif (finish='1' and sel='0')then      --在购买时相应的商品数量减少
        if (  (display(conv_integer(goods1))='1' or display(conv_integer(goods2+8))='1')) then
           if get='0' then
               ram(conv_integer(goods1))(3 downto 0)<=ram(conv_integer(goods1))(3 downto 0)-1;
           elsif get='1' then
               ram(conv_integer(goods2+8))(3 downto 0)<=ram(conv_integer(goods2+8))(3 downto 0)-1;
           end if;
        end if;
    end if;
end if;
end process;

--钱数及货物的计算
caculate:process
begin
wait until rising_edge(clk);
if set='0' then
     if (coin0='1' and coin<="01100100")then          --投入的钱数。
               coin<=coin+1;
          if coin>"00010100"  then
               money0<="00010100";     --输出投入的钱数
               money1<=(coin - money0);
          
          else
               money0<=coin;
               money1<=(others=>'0');
          end if;
     elsif sel='0' then
         if  cancel='1' then        --取消交易
                  money1<=money0;
                  money0<=(others=>'0');
         elsif   finish='1' then   ----选择好进行购买时
             if  (get='0' and ram(conv_integer(goods1))(3 downto 0)>"0000" )then
                   money0<=money0-2;
                 if(money0<"00000010") then     --当剩余的钱数小于最小的货品单价时退出剩余的钱
                       money1<=money0;            
                       money0<=(others=>'0');
                 end if;
             elsif (get='1' and ram(conv_integer(goods2+8))(3 downto 0)>"0000") then
                      money0<=money0-5;
                  if(money0<"00000110") then      --当剩余的钱数小于最小的货品单价时退出剩余的钱
                       money1<=money0;
                       money0<=(others=>'0');
                  end if;
             end if ;
        end if;            
     end if;
end if;
end process;

--检查指示灯是否满足点亮的条件
check:process
begin
 wait until rising_edge(clk) ;
 if set='0' then
   if money0<"00000010" then
        display<=(others=>'0');     
   elsif (money0>="00000010" and money0<"00000110") then  
       for i in 0 to 7 loop
           display(i+8)<='0' ;
           if (ram(i)(3 downto 0)>"0000") then
                  display(i)<='1' ;
           else
              display(i)<='0';
           end if;
       end loop;
  elsif  money0>="00000101" then
    for i in 0 to 15 loop  
      if (ram(i)(3 downto 0)>"0000") then
          display(i)<='1';
      else
          display(i)<='0' ;
      end if;
    end loop;
  end if;
  end if;
 end process;


--对商品的选择
choose:process
begin
 wait until rising_edge(clk) ;
 if coin0='0' then
   if  (sel='1' ) then
      if choice_a='1' then        --选择a类商品
             goods1<=goods1+1;
         if (display(conv_integer(goods1+1))='0') then
            if goods1<="0111" then
                goods1<=goods1+1;
            else
            goods1<="0000";
            end if;
         end if;
      elsif choice_b='1' then     --选择b类商品
             goods2<=goods2+1;
        if(display(conv_integer(goods2+8))='0') then
             if goods2<="0111" then
                goods2<=goods2+1;
             else
                goods2<="0000";
            end if;
         end if;
      end if;
   end if;
end if;
end process ;


end xiao;

⌨️ 快捷键说明

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