📄 shop.vhd
字号:
--文件名:shop.vhd。
--功能:货物信息存储,进程控制,硬币处理,余额计算,显示等功能。
--说明:显示的钱数coin的 以5角为单位。
library ieee;
use ieee.std_logic_arith.all;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity shop is
port ( clk:in std_logic;-----系统时钟20mhz
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(6 downto 0);--------------钱数、商品数量显示数据
scan:out std_logic_vector(2 downto 0);--------------数码管地址选择
act10,act5 :out std_logic);-------------------------1元硬币、5角硬币
end ;
architecture one of shop is
type ram_type is array(3 downto 0)of std_logic_vector(7 downto 0);
signal ram :ram_type; ------------------定义RAM
signal clk1khz,clk1hz:std_logic;
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 clk1hzhz: std_logic; ------------------------控制系统的时钟信号
signal y0,y1,y2:std_logic_vector(6 downto 0);----钱数、商品数量
begin
-------------------------------------1khz分频
process(clk)
variable cnt: integer range 0 to 0;
begin
if clk'event and clk='1' then
if cnt=0 then clk1khz<= not clk1khz;cnt:=0;
else cnt:=cnt+1;
end if;
end if;
end process ;
-------------------------------------1hz分频
process(clk1khz)
variable cnt: integer range 0 to 0;
begin
if clk1khz'event and clk1khz='1' then
if cnt=0 then clk1hz<= not clk1hz;cnt:=0;
else cnt:=cnt+1;
end if;
end if;
end process ;
----------------------------------
process(set,clk1hz,price,quantity,item)
begin
if set='1' then
ram(conv_integer(item))<=price & quantity;------把商品的单价、数量置入到RAM
act<="0000";
elsif clk1hz'event and clk1hz='1' then
act5<='0';
act10<='0';
if coin0='1' then ----------投入5角硬币,coin自加
if coin<"1001"then
coin<=coin+1;
else coin<="0000";
end if;
elsif coin1='1' then -------投入1元硬币,coin自加2
if coin<"1001"then
coin<=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;
qua<=qua-1;
ram(conv_integer(item))<=pri & qua;
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 --此IF语句完成找币操作
act10<='1';
coin<=coin-2;
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 0 to 3 loop
pri(i)<=ram(conv_integer(item))(4+i);-------商品价格的读取
qua(i)<=ram(conv_integer(item))(i);---------商品数量的读取
end loop;
end if;
end if;
end process ;
-----------------------------------------商品指示灯译码------
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;
--------------------------------------钱数的BCD到七段码的译码
process (coin)
begin
case coin is
when"0000"=>y0<="1111110";
when"0001"=>y0<="0110000";
when"0010"=>y0<="1101101";
when"0011"=>y0<="1111001";
when"0100"=>y0<="0110011";
when"0101"=>y0<="1011011";
when"0110"=>y0<="1011111";
when"0111"=>y0<="1110000";
when"1000"=>y0<="1111111";
when"1001"=>y0<="1111011";
when others=>y0<="0000000";
end case;
end process;
------------------------------------数量的BCD到七段码的译码
process (qua)
begin
case qua is
when"0000"=>y1<="1111110";
when"0001"=>y1<="0110000";
when"0010"=>y1<="1101101";
when"0011"=>y1<="1111001";
when"0100"=>y1<="0110011";
when"0101"=>y1<="1011011";
when"0110"=>y1<="1011111";
when"0111"=>y1<="1110000";
when"1000"=>y1<="1111111";
when"1001"=>y1<="1111011";
when others=>y1<="0000000";
end case;
end process;
------------------------------------单价的BCD到七段码的译码
process (pri)
begin
case pri is
when"0000"=>y2<="1111110";
when"0001"=>y2<="0110000";
when"0010"=>y2<="1101101";
when"0011"=>y2<="1111001";
when"0100"=>y2<="0110011";
when"0101"=>y2<="1011011";
when"0110"=>y2<="1011111";
when"0111"=>y2<="1110000";
when"1000"=>y2<="1111111";
when"1001"=>y2<="1111011";
when others=>y2<="0000000";
end case;
end process;
----------------------------------数码管动态扫描
process(clk1khz,y0,y1,y2)
variable cnt:integer range 0 to 2;
begin
if clk1khz'event and clk1khz='1' then
cnt:=cnt+1;
end if;
case cnt is
when 0=>scan<="001";seg7<=y0;
when 1=>scan<="010";seg7<=y1;
when 2=>scan<="100";seg7<=y2;
when others=>null;
end case;
end process;
end ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -