COLA.VHD
上传用户:dgjihui88
上传日期:2013-07-23
资源大小:43k
文件大小:10k
- ----libray and package declaraction
- library IEEE;
- use IEEE.std_logic_1164.all;
- use IEEE.std_logic_arith.all;
- use IEEE.std_logic_unsigned.all;
- use work.my_pkg.all;--package including 'debounce' component
- ----input and output pins declaraction
- Entity Vendor is
- Port( reset:in std_logic;--power reset to another buying
- clk:in std_logic;--system clock 1KHz
- ok_buy:in std_logic;--push button to confirm buying
- coin_5:in std_logic;--push button to throw coin 5 dollar
- coin_10:in std_logic;--push button to throw coin 10 dollar
- select_cola:in std_logic;--push button to choose drink cola
- select_diet:in std_logic;--push button to choose drink diet
- cancel_buy:in std_logic;--push button to cancel buying
- led_cola_ok:out std_logic;--led to show cola available
- led_diet_ok:out std_logic;--led to show diet available
- led_cola_sel:out std_logic;--led to show cola choosen
- led_diet_sel:out std_logic;--led to show diet choosen
- led_buy:out std_logic;--led to show buying confirmed
- led_cancel:out std_logic;--led to show buying canceled
- led_five:out std_logic_vector(2 downto 0);--leds to show coin_five numbers
- led_ten:out std_logic_vector(1 downto 0);--leds to show coin_ten numbers
- led_five_return:out std_logic_vector(2 downto 0);--leds to show coin_five returned
- led_ten_return:out std_logic_vector(1 downto 0);--leds to show coin_ten returned
- led_cola_out:out std_logic;--led to show cola been delivered
- led_diet_out:out std_logic);--led to show diet been delivered
- end;
- ----define the signal_structure and _flow of the device
- Architecture vendor_arch of vendor is
- --global signals flowing among different circuit blocks
- signal ok:std_logic;--edged ok_buy
- signal cancel:std_logic;--edged cancel_buy
- signal money_ok:std_logic;--signal for sufficient money
- signal return_clk:std_logic;--twinkling signal for coin returned
- signal cola_choice:std_logic;--to maintain the cola_selection status
- signal diet_choice:std_logic;--to maintain the diet_selection status
- signal total_amount_five:integer range 0 to 15;--total amount of coin five(maxi.=15)
- signal total_amount_ten:integer range 0 to 20;--total amount of coin ten(maxi.=20)
- signal cola_out:std_logic;--signal shown cola have been delivered
- signal diet_out:std_logic;--signal shown diet have been delivered
- begin
- ----to generate 4Hz return_clk via dividing 1024Hz clock by 256
- return_clock:Block
- Signal count:std_logic_vector(7 downto 0);--free count from 0 to 256
- Begin
- Process(reset,clk)
- Begin
- if reset = '1' then count<="00000000";
- return_clk<='0';
- elsif rising_edge(clk) then
- count <= count + "00000001" ;
- if count(7)='1' then return_clk<='1';
- else return_clk<='0';
- end if;
- end if;
- end process;
- end block;
- ----to count the number and amount of coin 10 and light the leds
- coin_10_counting:Block
- signal coin10:std_logic;--cleared coin_10 signal to count push-button frequency
- signal no_coin_ten:integer range 0 to 2;--no. of thrown coin ten(maxi.=2)
- begin
- u1:debounce port map(clk=>clk,ext=>coin_10,push_out => coin10);
- Process(reset,coin10)
- begin
- if reset = '1' then total_amount_ten<=0;
- no_coin_ten<= 0;
- led_ten<= "00";
- elsif rising_edge(coin10) then--triggered by edged coin10 signal
- total_amount_ten<=total_amount_ten+10;
- if no_coin_ten<2 then led_ten(no_coin_ten) <= '1';
- no_coin_ten <= no_coin_ten + 1;
- else no_coin_ten<=2;--(maxi. no of coin 10=2)
- end if;
- end if;
- end process;
- end block;
- ----to count the number and amount of coin 5 and light the leds
- coin_5_counting:Block
- signal coin5:std_logic;--cleared coin_5 signal to count push-button frequency
- signal no_coin_five:integer range 0 to 3;--no. of thrown coin five(maxi.=3)
- begin
- u2:debounce port map(clk=>clk,ext=>coin_5,push_out => coin5);
- Process(reset,coin5)
- begin
- if reset = '1' then total_amount_five<=0;
- no_coin_five<= 0;
- led_five<= "000";
- elsif rising_edge(coin5) then--triggered by edged coin10 signal
- total_amount_five<=total_amount_five+5;
- if no_coin_five<3 then led_five(no_coin_five) <= '1';
- no_coin_five <= no_coin_five + 1;
- else no_coin_five<=3;--(maxi. no of coin 5=3)
- end if;
- end if;
- end process;
- end block;
- ----to select cola or diet
- select_drink:block
- begin
- Process(reset,clk)
- begin
- if reset = '1' then led_cola_sel<='0';
- led_diet_sel<='0';
- elsif rising_edge(clk) then
- if select_cola='1' then led_cola_sel<='1';
- led_diet_sel<='0';--exclusive double choices
- cola_choice<='1';--to maintain the cola_selection status
- diet_choice<='0';--exclusive double choices
- end if;
- if select_diet='1' then led_cola_sel<='0';--exclusive double choices
- led_diet_sel<='1';
- diet_choice<='1';--to maintain the diet_selection status
- cola_choice<='0';--exclusive double choices
- end if;
- end if;
- end process;
- end block;
- ----to check total amount and decide returned coins and twinkle the leds
- coin_returned:Block
- signal total_amount:integer range 0 to 35;--(maxi. total amount=35)
- begin
- Process(reset,clk)
- begin
- if reset = '1' then total_amount<=0;
- money_ok<='0';
- led_five_return<= (others =>'0');
- led_ten_return<= (others =>'0');
- elsif rising_edge(clk) then
- total_amount<=total_amount_ten + total_amount_five;
- if total_amount >=15 then money_ok<='1';
- else money_ok<='0';
- end if;
- if (cancel='1') then--if cancelled, return all coins
- for i in 0 to 2 loop
- led_five_return(i)<=return_clk;
- end loop;
- for i in 0 to 1 loop
- led_ten_return(i)<=return_clk;
- end loop;
- elsif (diet_out='1' or cola_out='1') then--return coins after drink delivered
- case total_amount is
- When 0 to 14 => for i in 0 to 2 loop
- led_five_return(i)<=return_clk;
- end loop;
- for i in 0 to 1 loop
- led_ten_return(i)<=return_clk;
- end loop;
- when 15 => null;
- when 20 => led_five_return(0)<=return_clk;
- when 25 => led_ten_return(0)<=return_clk;
- when 30 => led_ten_return(1)<=return_clk;
- led_five_return(1)<=return_clk;
- when others=> led_ten_return(0)<=return_clk;
- led_ten_return(1)<=return_clk;
- end case;
- end if;
- end if;
- end process;
- end block;
- ----to check confirming or canceling buying
- ok_or_cancel: block
- begin
- p1:process(reset,ok_buy)--to maintain the confirming status
- begin
- if reset = '1' then ok<='0';
- led_buy<='0';
- elsif rising_edge(ok_buy) then
- ok<='1';
- led_buy<='1';
- end if;
- end process;
- p2:process(reset,cancel_buy)--to maintain the canceling status
- begin
- if reset = '1' then cancel<='0';
- led_cancel<='0';
- elsif rising_edge(cancel_buy) then
- cancel<='1';
- led_cancel<='1';
- end if;
- end process;
- end block;
- ----to deliver the selected drink and book the remaining number
- give_check: block
- signal no_cola:integer range 0 to 20;--bottle no. of remaining cola
- signal no_diet:integer range 0 to 20;--bottle no. of remaining diet
- begin
- cola_out<='1' when (money_ok='1' and ok='1' and cola_choice='1')
- else '0';--to deliver cola if necessary
- led_cola_out<=cola_out;
- diet_out<='1' when (money_ok='1' and ok='1' and diet_choice='1')
- else '0';--to deliver diet if necessary
- led_diet_out<=diet_out;
- cola:Process(reset,cola_out)--to book the bottle no. of remaining cola
- begin
- if reset = '1' then no_cola<=20;
- led_cola_ok<='1';
- elsif rising_edge(cola_out) then
- no_cola<=no_cola-1;
- if no_cola=0 then led_cola_ok<='0';--to show cola empty status
- else led_cola_ok<='1';
- end if;
- end if;
- end process;
- diet:Process(reset,diet_out)--to book the bottle no. of remaining diet
- begin
- if reset = '1' then no_diet<=20;
- led_diet_ok<='1';
- elsif rising_edge(diet_out) then
- no_diet<=no_diet-1;
- if no_diet=0 then led_diet_ok<='0';--to show diet empty status
- else led_diet_ok<='1';
- end if;
- end if;
- end process;
- end block;
- -----------------------------------------------------
- end vendor_arch;
- ------------------------------------------------------