?? 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 + -