?? autosell.vhd
字號:
--文件名:autosell.vhd
--功能:貨物信息存儲,進程控制,硬幣處理,余額計算,顯示等功能
--說明:顯示的錢數(shù)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; --系統(tǒng)時鐘
set,get,sel,finish: in std_logic; --設定、買、選擇、完成信號
coin0,coin1: in std_logic; --5角硬幣、1元硬幣
price,quantity :in std_logic_vector(3 downto 0);--價格、數(shù)量數(shù)據(jù)
led :out std_logic_vector(7 downto 0); --錢數(shù)、商品數(shù)量顯示數(shù)據(jù)
shift : out std_logic_vector(3 downto 0); --數(shù)碼管位選信號
cs : out std_logic_vector(1 downto 0)); --數(shù)碼管、發(fā)光二極管片選信號
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); --幣數(shù)計數(shù)器
signal pri,qua:std_logic_vector(3 downto 0); --商品單價、數(shù)量
signal clk1k,clk1: std_logic; --控制系統(tǒng)的時鐘信號
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";--把商品的單價、數(shù)量置入到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; --對商品進行循環(huán)選擇
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 --結(jié)束交易,退幣(找?guī)牛? if coin>"0001" then act10<='1';coin<=coin-2; --此IF語句完成找?guī)挪僮? 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); --商品數(shù)量的讀取
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=> --熄滅無關的發(fā)光二極管;
cs<="11";
dout<="11111";
shift<="1111";
current_state<=st1;
when st1=> --dout(4)是用來判斷點亮數(shù)碼管或發(fā)光二極管;
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) --錢數(shù)的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); --用發(fā)光二極管顯示貨品;
end if;
end process;
end behav;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -