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

📄 sellm.vhd

📁 vhdl实现的自动售货机 实现了售货
💻 VHD
字号:
------------------------------------------------------------------------------------ Company: -- Engineer: -- -- Create Date:    18:19:33 10/24/2007 -- Design Name: -- Module Name:    sellm - Behavioral -- Project Name: -- Target Devices: -- Tool versions: -- Description: ---- Dependencies: ---- Revision: -- Revision 0.01 - File Created-- Additional Comments: ------------------------------------------------------------------------------------library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;---- Uncomment the following library declaration if instantiating---- any Xilinx primitives in this code.--library UNISIM;--use UNISIM.VComponents.all;entity sellm isport(	CLK, RST: in std_logic;	C100, C50, C10: in std_logic;	SEL1, SEL2: in std_logic;	Return_Coins: in std_logic;	Dispense_item1: out std_logic;	Dispense_item2: out std_logic;	Collect_coins: out std_logic;	R_C100, R_C50, R_C10: out std_logic;	No_return_coins: out std_logic;	Lock_out_entry: out std_logic);end sellm;architecture Behavioral of sellm is	signal C100_b, C50_b, C10_b: std_logic;	signal C100_p, C50_p, C10_p: std_logic;	signal SEL1_b, SEL2_b: std_logic;	signal SEL1_p, SEL2_p: std_logic;	signal Return_coins_b: std_logic;	signal Return_coins_p: std_logic;	signal N100, N50, N10: integer;   -- number of coins in the machine	signal Sum,rest: integer;	signal Dispense_item1_b: std_logic:='0';	signal Dispense_item2_b: std_logic:='0';	signal Collect_coins_b: std_logic:='0';	signal R_C100_b, R_C50_b, R_C10_b: std_logic:='0';	signal No_return_coins_b: std_logic:='0';	signal Lock_out_entry_b: std_logic:='0';	signal done :std_logic:='0';	signal rejecto :std_logic:='0';	signal I :std_logic:='0';--calculate A Divided by B	function Division(A, B: integer) return integer is		variable r: integer;		variable c,d: integer; 	begin	    r:=0;	    c:=A;	    d:=B;   while c-d>=0 loop				c:= c-d;				r := r+1;		end loop;		return r;	end Division;	-- judge whether appropriate change could be give		function echange(Sum, N100, N50, N10, price: in integer) return std_logic is		variable Loop_1, Loop_2: integer;	begin		if Division((Sum-price),10) > N100 then			Loop_1 := N100;		else 			Loop_1 := Division((Sum-price),10);		end if;		if Division((Sum-10*Loop_1-price),5) > N50 then			Loop_2 := N50;		else			Loop_2 := Division((Sum-10*Loop_1-price),5);		end if;		if (Sum-price-Loop_1*10-Loop_2*5 > N10) then			return '0';		else			return '1';		end if;	end echange;		beginDispense_item1 <= Dispense_item1_b;Dispense_item2 <= Dispense_item2_b;Collect_coins <= Collect_coins_b;R_C100 <= R_C100_b;R_C50 <= R_C50_b;R_C10 <= R_C10_b;No_return_coins <= No_return_coins_b;Lock_out_entry <= Lock_out_entry_b;Detectpro: process(clk) -- generate the pulsebegin	if CLK'event and CLK = '1'then	   if Lock_out_entry_b<='0' then		   if rejecto ='0' then 		      C100_b <= C100;		      C50_b <= C50;		      C10_b <= C10;		      SEL1_b <= SEL1;		      SEL2_b <= SEL2;		      C100_p <= C100 and (not C100_b);            C50_p <= C50 and (not C50_b);            C10_p <= C10 and (not C10_b);            SEL1_p <= SEL1 and (not SEL1_b);            SEL2_p <= SEL2 and (not SEL2_b);		    else		      C100_b <= '0';            C50_b <= '0';            C10_b <= '0';            SEL1_b <= '0';            SEL2_b <= '0';    		      C100_p <= '0';            C50_p <= '0';            C10_p <= '0';            SEL1_p <= '0';            SEL2_p <= '0';          end if;  		    Return_coins_b<= Return_coins;  		    Return_coins_p <= Return_coins and (not Return_coins_b);  		   		    else  		    Return_coins_b<= '0';  		    Return_coins_p<= '0';  		 end if;    	         end if;end process Detectpro;coinpro: process(CLK, RST)begin	if RST = '1' then		N100 <= 10; N50 <= 10; N10 <= 10;		Sum <= 0;	else	   if CLK'event and CLK = '1' then		if C100_p = '1' then			N100 <= N100+1;			Sum <= Sum+10;		elsif C50_p = '1' then			N50 <= N50+1;			Sum <= Sum+5;		elsif C10_p ='1' then			N10 <= N10+1;			Sum <= Sum+1;		elsif R_C100_b = '1' then			N100 <= N100-1;		elsif R_C50_b = '1' then			N50 <= N50-1;		elsif R_C10_b = '1' then			N10 <= N10-1;		elsif Return_coins_p = '1' then			Sum <= 0;		end if;		end if;	end if;end process coinpro;Itempro: process(CLK,RST)begin   if CLK'event and CLK = '1' then			Collect_coins_b <= '0';			No_return_coins_b <= '0';			Dispense_item1_b <= '0';			Dispense_item2_b <= '0';			if SEL1_p = '1' then			   rejecto<='1';			   if Sum < 18 then 						elsif echange(Sum, N100, N50, N10, 18)='1' then				   Collect_coins_b <= '1';					Dispense_item1_b <= '1';					rest <= Sum-18;					i<='1';				  else					No_return_coins_b <= '1';				end if;			elsif SEL2_p = '1' then			    rejecto<='1';			   if Sum < 12 then 					elsif echange(Sum, N100, N50, N10, 12) = '1' then					Collect_coins_b <= '1';					Dispense_item2_b <= '1';					rest <= Sum-12;					i<='1';				else 					No_return_coins_b <= '1';				end if;			elsif Return_coins_p = '1' then				Collect_coins_b <= '1';				Lock_out_entry_b<='1';				if i='0' then				rest<=sum;				else             i<='0';				end if;				elsif done='1'then			   Lock_out_entry_b<='0';			   rejecto<='0';			   rest<=0;			end if;		end if;end process itempro;Changepro: process	variable N100_v, N50_v, N10_v, I100_v, I50_v, I10_v, Sum_v: integer;   variable Loop_1, Loop_2, Loop_3: integer;begin	wait until CLK'event and CLK = '1' and Return_coins_p = '1';	wait until CLK'event and CLK = '1';	wait until CLK'event and CLK = '1'; 	N100_v := N100; N50_v := N50; N10_v := N10;	Sum_v := rest;	if Division(Sum_v,10) > N100 then		Loop_1 := N100;	else		Loop_1 := Division(Sum_v,10);	end if;	if Division((Sum_v-10*Loop_1),5) > N50 then		Loop_2 := N50;	else		Loop_2 := Division((Sum_v-10*Loop_1),5);	end if;	Loop_3 := Sum_v-10*Loop_1-5*Loop_2;   while Loop_1>0 loop       Loop_1:=Loop_1-1;		wait until CLK'event and CLK = '1';		R_C100_b <= '1';		wait until CLK'event and CLK = '1';		R_C100_b <= '0';	end loop;	while Loop_2>0 loop		Loop_2:= Loop_2 -1;      wait until CLK'event and CLK = '1';		R_C50_b <= '1';		wait until CLK'event and CLK = '1';		R_C50_b <= '0';	end loop;   while Loop_3>0 loop      Loop_3:=Loop_3-1;		wait until CLK'event and CLK = '1';		R_C10_b <= '1';		wait until CLK'event and CLK = '1';		R_C10_b <= '0';	end loop;	   wait until CLK'event and CLK = '1';  done<='1';  wait until CLK'event and CLK='1';  done<='0';end process changepro;end Behavioral;						

⌨️ 快捷键说明

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