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

📄 shopping.vhd

📁 利用vhdl编写的商店的模型程序
💻 VHD
字号:
library ieee;
use ieee.std_logic_arith.all;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity shopping is
port ( clk:in std_logic;                              --系统时钟
	set,get,sel,finish: in std_logic;                 --设定、买、选择、完成信号
	coin0,coin1: in std_logic;                        --5角硬币、1元硬币
	price,quantity  :in std_logic_vector(3 downto 0); --价格、数量数据
	item0 , act:out std_logic_vector(3 downto 0);     --显示、开关信号
	Seg7 :out std_logic_vector(7 downto 0);           --钱数、商品数量显示数据
	Segslt:out std_logic_vector(7 downto 0);          --选择显示数码管数据
	act10,act5   :out std_logic);                     --1元硬币、5角硬币
end shopping;

architecture behav of shopping is
	type  ram_type is array(3 downto 0)of std_logic_vector(7 downto 0); 
	signal ram :ram_type;                                  --定义RAM
	signal item: std_logic_vector(1 downto 0);             --商品种类
	signal coin: std_logic_vector(3 downto 0);             --币数计数器
	signal pri,qua:std_logic_vector(3 downto 0);           --商品单价、数量
	signal clk1: std_logic;                                --控制系统的时钟信号
	signal y0,y1,y2 : std_logic_vector(7 downto 0);           --钱数,商品数量
begin

process(clk)                            --此进程完成对32Mhz的脉冲分频
variable q: std_logic_vector( 24 downto 0);
begin
   if clk'event and clk='1' then q:=q+1;
   end if;
   if q="111111111111111111111111" then clk1<='1';
   else clk1<='0';
   end if;
end process;

process(set,clk)
variable quan:std_logic_vector(3 downto 0);
begin
  if set='1' then 
    ram(conv_integer(item))<=price & quantity;act<="0000";	--把商品的单价、数量置入到RAM
  elsif clk'event and clk='1' then  
    act5<='0'; act10<='0';
    if coin0='1' then     
	   if coin<"1001"then 
	      coin<=coin+1;            --投入5角硬币,coin自加1
	   else coin<="0000";
       end if;
    elsif coin1='1' then 
	   if coin<"1001"then coin<=coin+2;            --投入1元硬币,coin自加2
	   else coin<="0000";
	   end if;
    elsif sel='1' then item<=item+1;                  --对商品进行循环选择
    elsif get='1' then                              --对商品进行购买
       if qua>"0000" and coin>=pri then 
          coin<=coin-pri;quan:=quan-1;
          ram(conv_integer(item))<=pri & quan;
          if   item="00" then act<="1000";  --购买时,自动售货机对4种商品的操作
		  elsif item="01" then act<="0100";
		  elsif item="10" then act<="0010";
		  elsif item="11" then act<="0001";
		  end if;
		end if;
    elsif  finish='1' then                            --结束交易,退币(找币)
       if coin>"0001" then act10<='1';coin<=coin-2;     --此IF语句完成找币操作
       elsif coin>"0000" then act5<='1'; coin<=coin-1;
       else act5<='0'; act10<='0';
       end if;
    elsif get='0' then act<="0000";                  
       for i in 4 to 7 loop                    
         pri(i-4)<=ram (conv_integer(item))(i);           --商品单价的读取
       end loop;
       for i in 0 to 3 loop
         quan(i):=ram(conv_integer(item))(i);            --商品数量的读取
       end loop; 
    end if;
  end if;
  qua<=quan;
end process;

process(item)                          --商品指示灯译码
begin
case item is
when "00"=>item0<="1000";
when "01"=>item0<="0100";
when "10"=>item0<="0010";
when others=>item0<="0001";
end case;
end process;

process (coin)                       --钱数的BCD到七段码的译码
begin
  case coin is
      when "0000"=>y0<="11111100";
      when "0001"=>y0<="01100000";
      when "0010"=>y0<="11011010";
      when "0011"=>y0<="11110010";
      when "0100"=>y0<="01100110";
      when "0101"=>y0<="10110110";
      when "0110"=>y0<="10111110";
      when "0111"=>y0<="11100000";
      when "1000"=>y0<="11111110";
      when "1001"=>y0<="11110110";
      when others=>y0<="00000000";
  end case;
end process;

process (qua)                       --数量的BCD到七段码的译码
begin
  case qua is
      when "0000"=>y1<="11111100";
      when "0001"=>y1<="01100000";
      when "0010"=>y1<="11011010";
      when "0011"=>y1<="11110010";
      when "0100"=>y1<="01100110";
      when "0101"=>y1<="10110110";
      when "0110"=>y1<="10111110";
      when "0111"=>y1<="11100000";
      when "1000"=>y1<="11111110";
      when "1001"=>y1<="11110110";
      when others=>y1<="00000000";
  end case;
end process;

process (pri)                       --单价的BCD到七段码的译码
begin
  case pri is
      when "0000"=>y2<="11111100";
      when "0001"=>y2<="01100000";
      when "0010"=>y2<="11011010";
      when "0011"=>y2<="11110010";
      when "0100"=>y2<="01100110";
      when "0101"=>y2<="10110110";
      when "0110"=>y2<="10111110";
      when "0111"=>y2<="11100000";
      when "1000"=>y2<="11111110";
      when "1001"=>y2<="11110110";
      when others=>y2<="00000000";
  end case;
end process;


process (clk)                      --分别显示数量、单价和钱数
variable t: std_logic_vector(9 downto 0);
begin
  if clk'event and clk='1' then
    t:=t+1;
  end if;

  case t(9 downto 8) is
    when "00"=>
      Segslt<="00000100";Seg7<=y0;
    when "01"=>
      Segslt<="00000010";Seg7<=y1;
    when "11"=>
      Segslt<="00000001";Seg7<=y2;
    when others=>null;
  end case;  
end process;
end behav;

⌨️ 快捷键说明

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