Forum Discussion
Altera_Forum
Honored Contributor
10 years agoVending Machine
Hi there. I have a school project which consists in a vending machine that has 3 products ( diferent prices) and i can only use 4 type of coins. My idea is to make a FSM ( where each state eq...
Altera_Forum
Honored Contributor
10 years agoThis is my code so far:
(coins : 0.10; 0.20; 0.50 ans 1.00) library IEEE; use IEEE.STD_LOGIC_1164.all; use IEEE.NUMERIC_STD.all; entity VendingMachine is port(sel: in std_logic_vector(1 downto 0); reset,clk,u,d,c,t: in std_logic; opn: out std_logic; total: out std_logic); end VendingMachine; architecture Behav of VendingMachine is type estados is(s0, s10,s20,s30,s40,s50,s60,s70,s80,s90,s100,s110,s120,s130,s140,s150,s160,s170,s180,s190,s200,s210,opnd); signal ps, ns : estados; begin sync_proc: process(clk, reset) begin if(reset = '1') then ps <= s0; elsif(rising_edge(clk)) then ps <= ns; end if; end process; comb_proc: process(ps,u,d,c,t,ns) begin ns <= ps; case ps is when s0 => --0.00€ if u = '1' then ns <= s10; elsif d = '1' then ns <= s20; elsif c = '1' then ns <= s50; elsif t = '1' then ns <= s100; end if; when s10 => if u = '1' then ns <= s20; elsif d = '1' then ns <= s30; elsif c = '1' then ns <= s60; elsif t = '1' then ns <= s110; end if; when s20 => if u = '1' then ns <= s30; elsif d = '1' then ns <= s40; elsif c = '1' then ns <= s70; elsif t = '1' then ns <= s120; end if; when s30 => if u = '1' then ns <= s40; elsif d = '1' then ns <= s50; elsif c = '1' then ns <= s80; elsif t = '1' then ns <= s130; end if; when s40 => if u = '1' then ns <= s50; elsif d = '1' then ns <= s60; elsif c = '1' then ns <= s90; elsif t = '1' then ns <= s140; end if; when s50 => if u = '1' then ns <= s60; elsif d = '1' then ns <= s70; elsif c = '1' then ns <= s100; elsif t = '1' then ns <= s150; end if; when s60 => if u = '1' then ns <= s70; elsif d = '1' then ns <= s80; elsif c = '1' then ns <= s110; elsif t = '1' then ns <= s160; end if; when s70 => if u = '1' then ns <= s80; elsif d = '1' then ns <= s90; elsif c = '1' then ns <= s120; elsif t = '1' then ns <= s170; end if; when s80 => if u = '1' then ns <= s90; elsif d = '1' then ns <= s100; elsif c = '1' then ns <= s130; elsif t = '1' then ns <= s180; end if; if(sel = "01") then ns <= opnd; end if; when s90 => if u = '1' then ns <= s100; elsif d = '1' then ns <= s110; elsif c = '1' then ns <= s140; elsif t = '1' then ns <= s190; end if; if(sel = "01") then ns <= opnd; end if; when s100 => if u = '1' then ns <= s110; elsif d = '1' then ns <= s120; elsif c = '1' then ns <= s150; elsif t = '1' then ns <= s200; end if; if( sel = "10") then ns <= opnd; elsif(sel = "01") then ns <= opnd; end if; when s110 => if u = '1' then ns <= s120; elsif d = '1' then ns <= s130; elsif c = '1' then ns <= s160; elsif t = '1' then ns <= s210; end if; if( sel = "10") then ns <= opnd; elsif(sel = "01") then ns <= opnd; end if; when s120 => if u = '1' then ns <= s130; elsif d = '1' then ns <= s150; elsif c = '1' then ns <= s170; end if; if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when s130 => if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when s140 => if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when s150 => if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when s160 => if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when s170 => if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when s180 => if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when s190 => if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when s200 => if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when s210 => if(sel = "01") then ns <= opnd; elsif( sel = "10") then ns <= opnd; elsif(sel <= "11") then ns <= opnd; end if; when opnd => opn <= '1'; when others => ns <= ps; end case; end process; end Behav;