COLA.VHD
上传用户:dgjihui88
上传日期:2013-07-23
资源大小:43k
文件大小:10k
源码类别:

VHDL/FPGA/Verilog

开发平台:

MultiPlatform

  1. ----libray and package declaraction
  2. library IEEE;
  3. use IEEE.std_logic_1164.all;
  4. use IEEE.std_logic_arith.all;
  5. use IEEE.std_logic_unsigned.all;
  6. use work.my_pkg.all;--package including 'debounce' component
  7. ----input and output pins declaraction
  8. Entity Vendor is
  9.   Port(       reset:in std_logic;--power reset to another buying 
  10.                 clk:in std_logic;--system clock 1KHz
  11.              ok_buy:in std_logic;--push button to confirm buying
  12.              coin_5:in std_logic;--push button to throw coin 5 dollar
  13.             coin_10:in std_logic;--push button to throw coin 10 dollar
  14.         select_cola:in std_logic;--push button to choose drink cola
  15.         select_diet:in std_logic;--push button to choose drink diet
  16.          cancel_buy:in std_logic;--push button to cancel buying
  17.         led_cola_ok:out std_logic;--led to show cola available
  18.         led_diet_ok:out std_logic;--led to show diet available
  19.        led_cola_sel:out std_logic;--led to show cola choosen
  20.        led_diet_sel:out std_logic;--led to show diet choosen 
  21.             led_buy:out std_logic;--led to show buying confirmed
  22.          led_cancel:out std_logic;--led to show buying canceled
  23.            led_five:out std_logic_vector(2 downto 0);--leds to show coin_five numbers
  24.             led_ten:out std_logic_vector(1 downto 0);--leds to show coin_ten numbers
  25.     led_five_return:out std_logic_vector(2 downto 0);--leds to show coin_five returned
  26.      led_ten_return:out std_logic_vector(1 downto 0);--leds to show coin_ten returned            
  27.        led_cola_out:out std_logic;--led to show cola been delivered 
  28.        led_diet_out:out std_logic);--led to show diet been delivered
  29. end;
  30. ----define the signal_structure and _flow of the device 
  31. Architecture vendor_arch of vendor is
  32. --global signals flowing among different circuit blocks
  33.   signal ok:std_logic;--edged ok_buy   
  34.   signal cancel:std_logic;--edged cancel_buy 
  35.   signal money_ok:std_logic;--signal for sufficient money
  36.   signal return_clk:std_logic;--twinkling signal for coin returned
  37.   signal cola_choice:std_logic;--to maintain the cola_selection status 
  38.   signal diet_choice:std_logic;--to maintain the diet_selection status   
  39.   signal total_amount_five:integer range 0 to 15;--total amount of coin five(maxi.=15)  
  40.   signal total_amount_ten:integer range 0 to 20;--total amount of coin ten(maxi.=20)  
  41.   signal cola_out:std_logic;--signal shown cola have been delivered  
  42.   signal diet_out:std_logic;--signal shown diet have been delivered 
  43. begin
  44. ----to generate 4Hz return_clk via dividing 1024Hz clock by 256
  45. return_clock:Block
  46.   Signal count:std_logic_vector(7 downto 0);--free count from 0 to 256
  47. Begin
  48.   Process(reset,clk)
  49.   Begin 
  50.     if reset = '1' then count<="00000000";
  51.                         return_clk<='0';
  52.     elsif rising_edge(clk) then
  53.        count <= count + "00000001" ;
  54.        if count(7)='1' then return_clk<='1';
  55.                        else return_clk<='0';
  56.        end if;
  57.     end if; 
  58.   end process;
  59. end block;
  60. ----to count the number and amount of coin 10 and light the leds
  61. coin_10_counting:Block
  62.   signal coin10:std_logic;--cleared coin_10 signal to count push-button frequency
  63.   signal no_coin_ten:integer range 0 to 2;--no. of thrown coin ten(maxi.=2)  
  64. begin
  65.   u1:debounce port map(clk=>clk,ext=>coin_10,push_out => coin10); 
  66.   Process(reset,coin10)
  67.   begin
  68.     if reset = '1' then total_amount_ten<=0;
  69.                         no_coin_ten<= 0;
  70.                         led_ten<= "00";                                           
  71.     elsif rising_edge(coin10) then--triggered by edged coin10 signal
  72.           total_amount_ten<=total_amount_ten+10;
  73.           if no_coin_ten<2 then led_ten(no_coin_ten) <= '1';  
  74.                                 no_coin_ten <= no_coin_ten + 1;
  75.                            else no_coin_ten<=2;--(maxi. no of coin 10=2)
  76.           end if;       
  77.     end if;
  78.   end process;        
  79. end block;
  80. ----to count the number and amount of coin 5 and light the leds
  81. coin_5_counting:Block
  82.   signal coin5:std_logic;--cleared coin_5 signal to count push-button frequency  
  83.   signal no_coin_five:integer range 0 to 3;--no. of thrown coin five(maxi.=3)  
  84. begin
  85.   u2:debounce port map(clk=>clk,ext=>coin_5,push_out => coin5);    
  86.   Process(reset,coin5)
  87.   begin
  88.     if reset = '1' then total_amount_five<=0;
  89.                         no_coin_five<= 0;
  90.                         led_five<= "000";                                           
  91.     elsif rising_edge(coin5) then--triggered by edged coin10 signal
  92.           total_amount_five<=total_amount_five+5;
  93.           if no_coin_five<3 then led_five(no_coin_five) <= '1';  
  94.                                  no_coin_five <= no_coin_five + 1;
  95.                             else no_coin_five<=3;--(maxi. no of coin 5=3)
  96.           end if;       
  97.     end if;
  98.   end process;        
  99. end block;
  100. ----to select cola or diet
  101. select_drink:block
  102. begin
  103.   Process(reset,clk)
  104.   begin
  105.     if reset = '1' then led_cola_sel<='0';
  106.                         led_diet_sel<='0';
  107.     elsif rising_edge(clk) then
  108.           if select_cola='1' then  led_cola_sel<='1';
  109.                                    led_diet_sel<='0';--exclusive double choices
  110.                                    cola_choice<='1';--to maintain the cola_selection status
  111.                                    diet_choice<='0';--exclusive double choices                                                                      
  112.           end if;
  113.           if select_diet='1' then  led_cola_sel<='0';--exclusive double choices
  114.                                    led_diet_sel<='1';
  115.                                    diet_choice<='1';--to maintain the diet_selection status
  116.                                    cola_choice<='0';--exclusive double choices                                                                    
  117.           end if;
  118.     end if;
  119.   end process;
  120. end block;  
  121. ----to check total amount and decide returned coins and twinkle the leds
  122. coin_returned:Block
  123.   signal total_amount:integer range 0 to 35;--(maxi. total amount=35)
  124. begin
  125.   Process(reset,clk)
  126.   begin
  127.     if reset = '1' then total_amount<=0;
  128.                         money_ok<='0';
  129.                         led_five_return<= (others =>'0');
  130.                         led_ten_return<= (others =>'0');                        
  131.     elsif rising_edge(clk) then
  132.       total_amount<=total_amount_ten + total_amount_five;
  133.       if total_amount >=15 then money_ok<='1';
  134.                            else money_ok<='0';
  135.       end if;
  136.       if (cancel='1') then--if cancelled, return all coins
  137.          for i in 0 to 2 loop
  138.              led_five_return(i)<=return_clk;
  139.          end loop; 
  140.          for i in 0 to 1 loop
  141.              led_ten_return(i)<=return_clk;
  142.          end loop; 
  143.       elsif (diet_out='1' or cola_out='1') then--return coins after drink delivered
  144.         case total_amount is
  145.         When 0 to 14 => for i in 0 to 2 loop
  146.                           led_five_return(i)<=return_clk;
  147.                         end loop;
  148.                         for i in 0 to 1 loop
  149.                           led_ten_return(i)<=return_clk;
  150.                         end loop; 
  151.         when 15 => null;
  152.         when 20 => led_five_return(0)<=return_clk;
  153.         when 25 => led_ten_return(0)<=return_clk;
  154.         when 30 => led_ten_return(1)<=return_clk;
  155.                    led_five_return(1)<=return_clk;                               
  156.         when others=> led_ten_return(0)<=return_clk;
  157.                       led_ten_return(1)<=return_clk;
  158.         end case;
  159.       end if;
  160.     end if;
  161.   end process;        
  162. end block;
  163. ----to check confirming or canceling buying
  164. ok_or_cancel: block
  165. begin
  166.   p1:process(reset,ok_buy)--to maintain the confirming status
  167.   begin
  168.     if reset = '1' then ok<='0';
  169.                         led_buy<='0';
  170.     elsif rising_edge(ok_buy) then
  171.                    ok<='1';
  172.                    led_buy<='1'; 
  173.     end if;
  174.   end process; 
  175.   p2:process(reset,cancel_buy)--to maintain the canceling status
  176.   begin
  177.     if reset = '1' then cancel<='0';
  178.                         led_cancel<='0'; 
  179.     elsif rising_edge(cancel_buy) then
  180.                   cancel<='1';
  181.                   led_cancel<='1';                  
  182.     end if;
  183.   end process;   
  184. end block;
  185. ----to deliver the selected drink and book the remaining number
  186. give_check: block
  187.   signal no_cola:integer range 0 to 20;--bottle no. of remaining cola
  188.   signal no_diet:integer range 0 to 20;--bottle no. of remaining diet
  189. begin
  190.   cola_out<='1' when (money_ok='1' and ok='1' and cola_choice='1')
  191.        else '0';--to deliver cola if necessary
  192.   led_cola_out<=cola_out;   
  193.   diet_out<='1' when (money_ok='1' and ok='1' and diet_choice='1')
  194.        else '0';--to deliver diet if necessary
  195.   led_diet_out<=diet_out;
  196.   cola:Process(reset,cola_out)--to book the bottle no. of remaining cola
  197.   begin
  198.     if reset = '1' then no_cola<=20;
  199.                         led_cola_ok<='1'; 
  200.     elsif rising_edge(cola_out) then
  201.           no_cola<=no_cola-1;
  202.           if no_cola=0 then led_cola_ok<='0';--to show cola empty status
  203.                        else led_cola_ok<='1';          
  204.           end if;
  205.     end if;
  206.   end process;
  207.   diet:Process(reset,diet_out)--to book the bottle no. of remaining diet
  208.   begin
  209.     if reset = '1' then no_diet<=20;  
  210.                         led_diet_ok<='1';   
  211.     elsif rising_edge(diet_out) then
  212.           no_diet<=no_diet-1;
  213.           if no_diet=0 then led_diet_ok<='0';--to show diet empty status
  214.                        else led_diet_ok<='1';
  215.           end if;
  216.     end if;
  217.   end process;  
  218. end block;
  219. -----------------------------------------------------
  220. end vendor_arch;
  221. ------------------------------------------------------