Advertisement
Guest User

vending machine

a guest
Apr 26th, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 11.52 KB | None | 0 0
  1. library ieee;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3.  
  4. use ieee.numeric_std.all;
  5.  
  6.  
  7. entity vend is
  8.     port (
  9.         rstb : in std_logic;
  10.         clk  : in  std_logic;
  11.         sw   : in  std_logic_vector(9 downto 0);
  12.         led  : out std_logic_vector(4 downto 0);
  13.         seg1 : out std_logic_vector(7 downto 0);  
  14.         seg2 : out std_logic_vector(7 downto 0);  
  15.         seg3 : out std_logic_vector(7 downto 0);  
  16.         seg4 : out std_logic_vector(7 downto 0);  
  17.         seg5 : out std_logic_vector(7 downto 0);  
  18.         seg6 : out std_logic_vector(7 downto 0);
  19.         --Ds : out std_logic_vector(5 downto 0);
  20.         choose : out std_logic_vector(4 downto 0)
  21.     );
  22.    
  23.    
  24.     end entity;
  25.    
  26.    
  27.    
  28.     architecture behavioral of vend is
  29.    
  30.     type state_type is (rdy,vend,err,cancel);--states
  31.     signal state : state_type;
  32.     signal state_next : state_type;
  33.     signal cnt:   unsigned(10 downto 0);
  34.     signal delay: unsigned(10 downto 0);   
  35.     signal clk1hz : std_logic;
  36.     signal clockcnt : integer range 0 to 25000000;
  37.     signal As : unsigned(5 downto 0); --quarters
  38.     signal Bs : unsigned(5 downto 0); --nickles
  39.     signal Cs : unsigned(5 downto 0); --dimes
  40.     signal Ds : unsigned(5 downto 0); --total
  41.    
  42.     constant a : std_logic_vector(7 downto 0) := "10001000";
  43.    constant b : std_logic_vector(7 downto 0) := "10000011";
  44.    constant C : std_logic_vector(7 downto 0) := "10100111";
  45.    constant d : std_logic_vector(7 downto 0) := "10100101";
  46.    constant E : std_logic_vector(7 downto 0) := "10000110";
  47.    constant F : std_logic_vector(7 downto 0) := "10001110";
  48.    constant G : std_logic_vector(7 downto 0) := "11000010";
  49.    constant H : std_logic_vector(7 downto 0) := "10001001";
  50.    constant i : std_logic_vector(7 downto 0) := "11101111";
  51.    constant j : std_logic_vector(7 downto 0) := "11100001";
  52.    --NO k
  53.    constant L : std_logic_vector(7 downto 0) := "10000111";
  54.    --NO m
  55.    constant n : std_logic_vector(7 downto 0) := "10101011";
  56.    constant o : std_logic_vector(7 downto 0) := "10100011";
  57.    constant P : std_logic_vector(7 downto 0) := "10001100";
  58.    constant q : std_logic_vector(7 downto 0) := "10110000";
  59.    constant r : std_logic_vector(7 downto 0) := "10101111";
  60.    constant s : std_logic_vector(7 downto 0) := "10010011";
  61.    constant t : std_logic_vector(7 downto 0) := "10000111";
  62.    constant U : std_logic_vector(7 downto 0) := "11000001";
  63.    --NO v
  64.    constant w : std_logic_vector(7 downto 0) := "10000001";
  65.    --NO x
  66.    constant y : std_logic_vector(7 downto 0) := "10010001";
  67.    --NO z
  68.     constant s0 : std_logic_vector(7 downto 0) := "11000000";
  69.    constant s1 : std_logic_vector(7 downto 0) := "11111001";
  70.    constant s2 : std_logic_vector(7 downto 0) := "10100100";
  71.    constant s3 : std_logic_vector(7 downto 0) := "10110000";
  72.    constant s4 : std_logic_vector(7 downto 0) := "10011001";
  73.    constant s5 : std_logic_vector(7 downto 0) := "10010010";
  74.    constant s6 : std_logic_vector(7 downto 0) := "10000010";
  75.    constant s7 : std_logic_vector(7 downto 0) := "11111000";
  76.    constant s8 : std_logic_vector(7 downto 0) := "10000000";
  77.    constant s9 : std_logic_vector(7 downto 0) := "10010000"; --fill this in later
  78.     constant blank : std_logic_vector(7 downto 0):="11111111";
  79.     constant dash: std_logic_vector (7 downto 0):="00001000";
  80.     --state machine --rdy,chip,candy,vend,err
  81.     begin
  82.    
  83.     process(all)
  84.    
  85.         begin
  86.             case state is
  87.            
  88.                 when rdy =>
  89.                    
  90.                     if(sw(0) = '1')then--chip1
  91.                     choose(0)<='1';
  92.                         if(Ds >= "110010")then
  93.                         state_next<=vend;
  94.                         else
  95.                         state_next<=rdy;
  96.                         end if;
  97.                     elsif(sw(1) = '1')then--chip2
  98.                     choose(1)<='1';
  99.                         if(Ds >= "110010")then
  100.                         state_next<=vend;
  101.                         else
  102.                         state_next<=rdy;
  103.                         end if;
  104.                     elsif(sw(2) = '1')then--candy1
  105.                     choose(2)<='1';
  106.                         if(Ds >= "111100")then
  107.                         state_next<=vend;
  108.                         else
  109.                         state_next<=rdy;
  110.                         end if;
  111.                     elsif(sw(3) = '1')then--candy2
  112.                     choose(3)<='1';
  113.                         if(Ds >= "111100")then
  114.                         state_next<=vend;
  115.                         else
  116.                         state_next<=rdy;
  117.                         end if;
  118.                     elsif(sw(4) = '1')then--candy3
  119.                     choose(4)<='1';
  120.                         if(Ds >= "111100")then
  121.                         state_next<=vend;
  122.                         else
  123.                         state_next<=rdy;
  124.                         end if;
  125.                     elsif(sw(5) = '1')then--cancel
  126.                    
  127.                         state_next<=cancel;
  128.                        
  129.                     elsif(sw(6) = '1')then--quarters
  130.                         --As<= "011001";
  131.                         Ds<=  Ds + B"011001";
  132.                         --Ds<=std_logic_vector(Ds);
  133.                     elsif(sw(7) = '1')then--nickles
  134.                       -- Bs<="000101";
  135.                         Ds<= Ds + B"000101";
  136.                         --Ds<=std_logic_vector(Ds);
  137.                     elsif(sw(8) = '1')then--dimes
  138.                         --Cs<="001010";
  139.                          Ds<= Ds + B"001010";
  140.                         --Ds<=std_logic_vector(Ds);
  141.                     else
  142.                    
  143.                         state_next<=rdy;
  144.                        
  145.                     end if;
  146.                    
  147.                    
  148.                
  149.                 when cancel =>
  150.                 Ds <= Ds - Ds;
  151.                 --Ds <= std_logic_vector(Ds);
  152.                
  153.                 when vend =>
  154.                
  155.                     if(sw(0) = '1')then--chip1
  156.                         Ds <= Ds - B"110010";
  157.                     --  Ds<=std_logic_vector(Ds);
  158.                         state_next<=rdy;
  159.                     elsif(sw(1) = '1')then--chip2
  160.                         Ds<= Ds - B"110010";
  161.                         --Ds<=std_logic_vector(Ds);
  162.                         state_next<=rdy;
  163.                     elsif(sw(2) = '1')then--candy1
  164.                         Ds<= Ds - B"111100";
  165.                         --Ds<=std_logic_vector(Ds);
  166.                         state_next<=rdy;
  167.                     elsif(sw(3) = '1')then--candy2
  168.                         Ds<= Ds - B"111100";
  169.                         --Ds<=std_logic_vector(Ds);
  170.                         state_next<=rdy;
  171.                     elsif(sw(4) = '1')then--candy3
  172.                         Ds<= Ds - B"111100";
  173.                         --Ds<=std_logic_vector(Ds);
  174.                         state_next<=rdy;
  175.                     else
  176.                         state_next<=err;
  177.                     end if;
  178.                    
  179.                    
  180.                    
  181.                 when err =>
  182.                     if( Ds < "111100")then
  183.                     state_next<=rdy;
  184.                     else
  185.                     state_next<=rdy;
  186.                     end if;
  187.                
  188.                
  189.                
  190.                
  191.                 end case;
  192.            
  193.     end process;
  194.    
  195.    
  196.    
  197.    
  198.    
  199.    
  200.     process(all)
  201.         begin
  202.             case state is
  203.                
  204.                 when rdy =>
  205.                    if(Ds ="000000")then
  206.                     seg1<=r;
  207.                     seg2<=e;
  208.                     seg3<=a;
  209.                     seg4<=d;
  210.                     seg5<=y;
  211.                     seg6<=blank;
  212.                     elsif(Ds = "000101")then
  213.                     seg1<=dash;
  214.                     seg2<=dash;
  215.                     seg3<=s0;
  216.                     seg4<=s5;
  217.                     seg5<=dash;
  218.                     seg6<=dash;
  219.                     elsif(Ds = "001010")then
  220.                     seg1<=dash;
  221.                     seg2<=dash;
  222.                     seg3<=s1;
  223.                     seg4<=s0;
  224.                     seg5<=dash;
  225.                     seg6<=dash;
  226.                     elsif(Ds = "001111")then
  227.                     seg1<=dash;
  228.                     seg2<=dash;
  229.                     seg3<=s1;
  230.                     seg4<=s5;
  231.                     seg5<=dash;
  232.                     seg6<=dash;
  233.                     elsif(Ds = "010100")then
  234.                     seg1<=dash;
  235.                     seg2<=dash;
  236.                     seg3<=s2;
  237.                     seg4<=s0;
  238.                     seg5<=dash;
  239.                     seg6<=dash;
  240.                     elsif(Ds = "011001")then
  241.                     seg1<=dash;
  242.                     seg2<=dash;
  243.                     seg3<=s2;
  244.                     seg4<=s5;
  245.                     seg5<=dash;
  246.                     seg6<=dash;
  247.                     elsif(Ds = "011110")then
  248.                     seg1<=dash;
  249.                     seg2<=dash;
  250.                     seg3<=s3;
  251.                     seg4<=s0;
  252.                     seg5<=dash;
  253.                     seg6<=dash;
  254.                     elsif(Ds = "100011")then--35
  255.                     seg1<=dash;
  256.                     seg2<=dash;
  257.                     seg3<=s3;
  258.                     seg4<=s5;
  259.                     seg5<=dash;
  260.                     seg6<=dash;
  261.                     elsif(Ds = "101000")then
  262.                     seg1<=dash;
  263.                     seg2<=dash;
  264.                     seg3<=s4;
  265.                     seg4<=s0;
  266.                     seg5<=dash;
  267.                     seg6<=dash;
  268.                     elsif(Ds = "101101")then
  269.                     seg1<=dash;
  270.                     seg2<=dash;
  271.                     seg3<=s4;
  272.                     seg4<=s5;
  273.                     seg5<=dash;
  274.                     seg6<=dash;
  275.                     elsif(Ds = "110010")then
  276.                     seg1<=dash;
  277.                     seg2<=dash;
  278.                     seg3<=s5;
  279.                     seg4<=s0;
  280.                     seg5<=dash;
  281.                     seg6<=dash;
  282.                     elsif(Ds = "110111")then
  283.                     seg1<=dash;
  284.                     seg2<=dash;
  285.                     seg3<=s5;
  286.                     seg4<=s5;
  287.                     seg5<=dash;
  288.                     seg6<=dash;
  289.                     elsif(Ds = "111100")then
  290.                     seg1<=dash;
  291.                     seg2<=dash;
  292.                     seg3<=s6;
  293.                     seg4<=s0;
  294.                     seg5<=dash;
  295.                     seg6<=dash;
  296.                     else
  297.                     seg1<=dash;
  298.                     seg2<=dash;
  299.                     seg3<=s0;
  300.                     seg4<=s0;
  301.                     seg5<=dash;
  302.                     seg6<=dash;
  303.                    end if;
  304.                 when vend =>
  305.                    if(choose(0) = '1')then
  306.                     seg1<=u;
  307.                     seg2<=e;
  308.                     seg3<=n;
  309.                     seg4<=d;
  310.                     seg5<=s1;
  311.                     seg6<=blank;
  312.                     led<="00001";
  313.                     --choose(0)<= '0';
  314.                     elsif(choose(1) = '1')then
  315.                     seg1<=u;
  316.                     seg2<=e;
  317.                     seg3<=n;
  318.                     seg4<=d;
  319.                     seg5<=s2;
  320.                     seg6<=blank;
  321.                     led<="00010";
  322.                     --choose(1)<= '0';
  323.                     elsif(choose(2) = '1')then
  324.                     seg1<=u;
  325.                     seg2<=e;
  326.                     seg3<=n;
  327.                     seg4<=d;
  328.                     seg5<=s3;
  329.                     seg6<=blank;
  330.                     led<="00100";
  331.                     --choose(2)<= '0';
  332.                     elsif(choose(3) = '1')then
  333.                     seg1<=u;
  334.                     seg2<=e;
  335.                     seg3<=n;
  336.                     seg4<=d;
  337.                     seg5<=s4;
  338.                     seg6<=blank;
  339.                     led<="01000";
  340.                     --choose(3)<= '0';
  341.                     elsif(choose(4) = '1')then
  342.                     seg1<=u;
  343.                     seg2<=e;
  344.                     seg3<=n;
  345.                     seg4<=d;
  346.                     seg5<=s5;
  347.                     seg6<=blank;
  348.                     led<="10000";
  349.                     --choose(4)<= '0';
  350.                     else
  351.                     seg1<=blank;
  352.                     seg2<=blank;
  353.                     seg3<=blank;
  354.                     seg4<=blank;
  355.                     seg5<=blank;
  356.                     seg6<=blank;
  357.                     led<="00000";
  358.                     end if;
  359.                 when cancel =>
  360.                    seg1<=c;
  361.                     seg2<=a;
  362.                     seg3<=n;
  363.                     seg4<=c;
  364.                     seg5<=e;
  365.                     seg6<=l;
  366.                     led<="11111";
  367.                    
  368.                    
  369.                 when err =>
  370.                     if(Ds < "110010")then
  371.                         if(Ds = "000101")then
  372.                        seg1<=dash;
  373.                        seg2<=dash;
  374.                        seg3<=s0;
  375.                        seg4<=s5;
  376.                        seg5<=dash;
  377.                        seg6<=dash;
  378.                        elsif(Ds = "001010")then
  379.                        seg1<=dash;
  380.                        seg2<=dash;
  381.                        seg3<=s1;
  382.                        seg4<=s0;
  383.                        seg5<=dash;
  384.                        seg6<=dash;
  385.                        elsif(Ds = "001111")then
  386.                        seg1<=dash;
  387.                        seg2<=dash;
  388.                        seg3<=s1;
  389.                        seg4<=s5;
  390.                        seg5<=dash;
  391.                        seg6<=dash;
  392.                        elsif(Ds = "010100")then
  393.                        seg1<=dash;
  394.                        seg2<=dash;
  395.                        seg3<=s2;
  396.                        seg4<=s0;
  397.                        seg5<=dash;
  398.                        seg6<=dash;
  399.                        elsif(Ds = "011001")then
  400.                        seg1<=dash;
  401.                        seg2<=dash;
  402.                        seg3<=s2;
  403.                        seg4<=s5;
  404.                        seg5<=dash;
  405.                        seg6<=dash;
  406.                        elsif(Ds = "011110")then
  407.                        seg1<=dash;
  408.                        seg2<=dash;
  409.                        seg3<=s3;
  410.                        seg4<=s0;
  411.                        seg5<=dash;
  412.                        seg6<=dash;
  413.                        elsif(Ds = "100011")then--35
  414.                        seg1<=dash;
  415.                        seg2<=dash;
  416.                        seg3<=s3;
  417.                        seg4<=s5;
  418.                        seg5<=dash;
  419.                        seg6<=dash;
  420.                        elsif(Ds = "101000")then
  421.                        seg1<=dash;
  422.                        seg2<=dash;
  423.                        seg3<=s4;
  424.                        seg4<=s0;
  425.                        seg5<=dash;
  426.                        seg6<=dash;
  427.                        elsif(Ds = "101101")then
  428.                        seg1<=dash;
  429.                        seg2<=dash;
  430.                        seg3<=s4;
  431.                        seg4<=s5;
  432.                        seg5<=dash;
  433.                        seg6<=dash;
  434.                        elsif(Ds = "110010")then
  435.                        seg1<=dash;
  436.                        seg2<=dash;
  437.                        seg3<=s5;
  438.                        seg4<=s0;
  439.                        seg5<=dash;
  440.                        seg6<=dash;
  441.                        elsif(Ds = "110111")then
  442.                        seg1<=dash;
  443.                        seg2<=dash;
  444.                        seg3<=s5;
  445.                        seg4<=s5;
  446.                        seg5<=dash;
  447.                        seg6<=dash;
  448.                        else
  449.                        seg1<=dash;
  450.                        seg2<=dash;
  451.                        seg3<=s5;
  452.                        seg4<=s5;
  453.                        seg5<=dash;
  454.                        seg6<=dash;
  455.                        end if;
  456.                 else
  457.                     seg1<=blank;
  458.                     seg2<=blank;
  459.                     seg3<=blank;
  460.                     seg4<=blank;
  461.                     seg5<=blank;
  462.                     seg6<=blank;
  463.                     end if;
  464.                     end case;
  465.                     end process;
  466.                    
  467.                     --register logics
  468.     --process(clk, rstb)--1hrz
  469.     --  begin
  470.     --  -- reset
  471.     --      if (rstb = '0') then
  472.     --          cnt <= (others => '0');
  473.     --  -- rising clk edge
  474.     --      elsif (rising_edge(clk)) then---1hz
  475.     --          if(cnt < delay - 1) then
  476.     --              cnt <= cnt + 1;
  477.     --          else
  478.     --              cnt <= (others => '0');
  479.     --          end if;
  480.     --      end if;
  481.     --end process;
  482.    
  483.    
  484.         process(clk, rstb)--clk1hz
  485.     begin
  486.         -- reset
  487.         if (rstb = '0') then
  488.             state <= rdy;
  489.         -- rising clk edge
  490.         elsif (rising_edge(clk)) then--1hrz
  491.             state <= state_next;
  492.         end if;
  493.     end process;
  494.        
  495.    
  496.     process(rstb,clk)--slow clk
  497.     begin
  498.                 if (rstb ='0') then
  499.                 clockcnt<=0;
  500.                 elsif rising_edge(clk) then
  501.                     clockcnt <= clockcnt+1;
  502.                     if clockcnt = 25000000 then
  503.                         clk1hz <= not clk1hz;
  504.                         clockcnt <=0;
  505.                     end if;
  506.                 end if;
  507.     end process;
  508.    
  509.                    
  510.             end behavioral;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement