使用Altera Quartus Ⅱ设计一个具有投币找零功能的简易自动售货机。要求底层的模块采用VHDL文本输入,顶层的电路系统采用原理图输入。 自动售货机可以接受5角和1元硬币,货物价格为1.5元。当投入币值为两元时输出货物并找零5角,投币的钱数通过数码管显示。VHDL代码实现
时间: 2024-02-20 15:01:39 浏览: 12
好的,以下是一个简单的VHDL代码实现:
```vhdl
-- 定义输入输出信号
entity vending_machine is
port(
clk : in std_logic;
reset : in std_logic;
coin_5 : in std_logic;
coin_1 : in std_logic;
item_out : out std_logic;
change_5 : out std_logic;
coin_value : out std_logic_vector(3 downto 0);
seven_seg : out std_logic_vector(7 downto 0)
);
end entity vending_machine;
-- 实现自动售货机
architecture vending_machine_arch of vending_machine is
-- 记录投币总数
signal total_coin : integer range 0 to 15 := 0;
-- 记录是否可以出货
signal can_dispense : boolean := false;
-- 记录是否需要找零
signal need_change : boolean := false;
-- 记录是否已找零
signal change_dispensed : boolean := false;
begin
-- 用于计算投币总数
process(clk, reset)
begin
if reset = '1' then
total_coin <= 0;
elsif rising_edge(clk) then
if coin_5 = '1' then
total_coin <= total_coin + 1;
elsif coin_1 = '1' then
total_coin <= total_coin + 2;
end if;
end if;
end process;
-- 判断是否可以出货
process(total_coin)
begin
if total_coin >= 3 then
can_dispense <= true;
need_change <= (total_coin = 4);
else
can_dispense <= false;
need_change <= false;
end if;
end process;
-- 判断是否需要找零
process(clk, reset, can_dispense, change_dispensed)
begin
if reset = '1' then
change_dispensed <= false;
elsif can_dispense = true and change_dispensed = false then
if total_coin = 4 then
change_dispensed <= true;
end if;
end if;
end process;
-- 控制输出信号
item_out <= '1' when can_dispense = true else '0';
change_5 <= '1' when change_dispensed = true else '0';
coin_value <= std_logic_vector(to_unsigned(total_coin, 4));
-- 数码管显示投币总数
process(total_coin)
variable digit : integer range 0 to 9;
begin
digit := total_coin mod 10;
seven_seg <= "00000000" when total_coin = 0 else
"00111111" when digit = 0 else
"00000110" when digit = 1 else
"01011011" when digit = 2 else
"01001111" when digit = 3 else
"01100110" when digit = 4 else
"01101101" when digit = 5 else
"01111101" when digit = 6 else
"00000111" when digit = 7 else
"01111111" when digit = 8 else
"01101111";
end process;
end architecture vending_machine_arch;
```
该代码实现了一个自动售货机的基本功能,包括投币、出货、找零和数码管显示。其中,输入信号为时钟信号、复位信号、5角硬币输入信号和1元硬币输入信号;输出信号为货物输出信号、5角零钱输出信号、投币总数显示信号和七段数码管显示信号。