📄 autosell.vhd
字号:
--文件名:autosell.vhd
--功能:货物信息存储,进程控制,硬币处理,余额计算,显示等功能
--说明:显示的钱数coin的值以5角为单位
library ieee;
use ieee.std_logic_arith.all;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity autosell 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);--价格、数量数据
led :out std_logic_vector(7 downto 0); --钱数、商品数量显示数据
shift : out std_logic_vector(3 downto 0); --数码管位选信号
cs : out std_logic_vector(1 downto 0)); --数码管、发光二极管片选信号
end autosell;
architecture behav of autosell is
type state is (st0,st1,st2,st3,st4,st5,st6);
signal current_state : state:=st0;
type ram_type is array(3 downto 0)of std_logic_vector(7 downto 0);
signal ram :ram_type; --定义RAM
signal item0,act : std_logic_vector(3 downto 0); --显示、开关信号
signal act10,act5 : std_logic; --1元硬币、5角硬币
signal dout : std_logic_vector(4 downto 0);
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 clk1k,clk1: std_logic; --控制系统的时钟信号
begin
com:process(set,clk1,price,quantity)
variable quan:std_logic_vector(3 downto 0);
begin
if clk1'event and clk1='1' then act5<='0'; act10<='0';
if set='0' then ram(conv_integer(item))<=price & quantity;act<="0000";--把商品的单价、数量置入到RAM
elsif coin0='0' then
if coin<"1001"then coin<=coin+1; --投入5角硬币,coin自加1
else coin<="0000";
end if;
elsif coin1='0' then
if coin<"1001"then coin<=coin+2; --投入1元硬币,coin自加2
else coin<="0000";
end if;
elsif sel='0' then item<=item+1; --对商品进行循环选择
elsif get='0' 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<="0111"; --购买时,自动售货机对4种商品的操作
elsif item="01" then act<="1011";
elsif item="10" then act<="1101";
elsif item="11" then act<="1110";
end if;
end if;
elsif finish='0' 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='1' 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;
m1k:process(clk) --此进程完成对50000hz的脉冲分频
variable q: integer range 0 to 50000;
begin
if clk'event and clk='1' then q:=q+1;
if q<25000 then clk1k<='0';
elsif q<50000 then clk1k<='1';
else q:=0;clk1k<='1';
end if;
end if;
end process;
process(clk1k) --此进程完成对1000hz的脉冲分频
variable q: integer range 0 to 1000;
begin
if clk1k'event and clk1k='1' then q:=q+1;
if q<500 then clk1<='0';
elsif q<1000 then clk1<='1';
else q:=0;clk1<='1';
end if;
end if;
end process;
code0:process(item) --商品指示灯译码
begin
case item is
when "00"=>item0<="0111";
when "01"=>item0<="1011";
when "10"=>item0<="1101";
when others=>item0<="1110";
end case;
end process;
process(clk1k)
begin
if clk1k'event and clk1k='1' then
case current_state is
when st0=> --熄灭无关的发光二极管;
cs<="11";
dout<="11111";
shift<="1111";
current_state<=st1;
when st1=> --dout(4)是用来判断点亮数码管或发光二极管;
cs<="10";
dout<='1'&coin;
shift<="1110";
current_state<=st2;
when st2=>
cs<="01";
dout(4)<='0';
shift<="1111";
current_state<=st3;
when st3=>
cs<="10";
dout<='1'&qua;
shift<="0111";
current_state<=st4;
when st4=>
cs<="01";
dout(4)<='0';
shift<="1111";
current_state<=st5;
when st5=>
cs<="10";
if act5='1' then dout<="10101";shift<="1011";
elsif act10='1' then dout<="10000";shift<="1101";
else shift<="1111";dout<="11111";
end if;
current_state<=st1;
when others=>
current_state<=st0;
end case;
end if;
end process;
code1: process (dout,item0,act) --钱数的BCD到七段码的译码
begin
if dout(4)='1' then
case dout(3 downto 0) is
when "0000"=>led<="10000001";
when "0001"=>led<="11001111";
when "0010"=>led<="10010010";
when "0011"=>led<="10000110";
when "0100"=>led<="11001100";
when "0101"=>led<="10100100";
when "0110"=>led<="10100000";
when "0111"=>led<="10001111";
when "1000"=>led<="10000000";
when "1001"=>led<="10000100";
when others=>led<="11111111";
end case;
else led<=act(3)&item0&act(2 downto 0); --用发光二极管显示货品;
end if;
end process;
end behav;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -