0% found this document useful (0 votes)
36 views

Assignment

This document describes a module for a vending machine with the following: 1) It contains inputs for clock, reset, cancel, coins, and product selections and outputs for product dispensed, change given, and coins returned. 2) It uses a state machine with 17 states to control the vending machine's behavior based on coin inputs, product selections, and cancel. 3) The states control updating coin counts, dispensing products, and returning change or coins based on the inputs and state transitions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Assignment

This document describes a module for a vending machine with the following: 1) It contains inputs for clock, reset, cancel, coins, and product selections and outputs for product dispensed, change given, and coins returned. 2) It uses a state machine with 17 states to control the vending machine's behavior based on coin inputs, product selections, and cancel. 3) The states control updating coin counts, dispensing products, and returning change or coins based on the inputs and state transitions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

module vending_machine(

input clk,

input rst,

input cancel,

input coin1,

input coin2,

input select1,

input select2,

input select3,

output reg product,

output reg [2:0] change,

output reg [2:0] return

);

reg [4:0]state,next_state;

reg [2:0]coincount;

parameter idle=5'b00000,s1=5'b00001,s2=5'b00010,s3=5'b00011,

s4=5'b00100,s5=5'b00101,s6=5'b00110,s7=5'b00111,

s8=5'b01000,s9=5'b01001,s10=5'b01010,s11=5'b01011,s12=5'b01100,s13=5'b01101,

s14=5'b01110,s15=5'b01111,s16=5'b10000,s17=5'b10001;

always @(posedge clk)

begin

if(rst==1'b0) //Active Low reset


begin

state<=idle;

end

else

begin

state<=next_state;

end

end

//////////////////////////////////////////////////////////// Controlling the entire


module/////////////////////////////////////////////////////////////////////

always @(*)

begin

case(state)

idle:begin

product<=1'b0; change<=2'b00; return<=2'b00;

coincount=0;

if(!select1 && select2 && !select3)

begin next_state<=s1; end

else if(select1 && !select2 && !select3)

begin next_state<=s7; end

else if(!select1 && !select2 && select3)

begin next_state<=s12; end

end

s1:begin

product<=1'b0; change<=2'b00; return<=2'b00;


next_state<=s2;

end

s2:begin

change<=2'b00; product<=1'b0;return<=2'b00;

if(coincount>3)

begin next_state<= s3; end

else if(coin1 && !coin2 && (coincount<4))

begin next_state<=s4; end

else if(!coin1 && coin2 && (coincount<4))

begin next_state<=s5; end

else if(cancel)

begin next_state<=s6; end

end

s3:begin

change<=coincount-4; product<=1'b1; return<=2'b00;

next_state<=s2;

end

s4:begin

change<=1'b0; coincount<=coincount+1; product<=1'b0; return<=2'b00;

next_state<=s2;

end

s5:begin

change<=1'b0; coincount<=coincount+2; product<=1'b0; return<=1'b0;

next_state<=s2;

end
s6:begin

change<=1'b0;product<=1'b0;

return<=coincount;

next_state<=idle;

end

s7:begin

change<=1'b0;product<=1'b0;return<=2'b00;

next_state<=s8;

end

s8:begin

change<=1'b0;product<=1'b0;return<=2'b00;

if(coincount>2)

begin next_state<=s9; end

else if(!coin1 && coin2 && (coincount<3))

begin next_state<=s10; end

else if(coin1 && !coin2 && (coincount<3))

begin next_state<=s11; end

else if(cancel)

begin next_state<=s6; end

end

s9:begin

change<=coincount-3;product<=1'b1;

next_state<=s8;

end

s10:begin
change<=1'b0; coincount=coincount+2; product<=1'b0;

next_state<=s8;

end

s11:begin

change<=1'b0; coincount<=coincount+1; product<=1'b0;

next_state<=s8;

end

s12:begin

next_state<=s13;

end

s13:begin

change<=1'b0; product<=1'b0;

if(coincount>4)

begin next_state<=s14; end

else if(coin1 && !coin2 && coincount<5)

begin next_state<=s15; end

else if(!coin1 && coin2 && coincount<5)

begin next_state<=s16; end

else if(cancel)

begin next_state<=s6; end

end

s14:begin

change<=coincount-5; product<=1'b1;

next_state<=s13;

end
s15:begin

change<=1'b0; coincount<=coincount+1; product<=1'b0;

next_state<=s13;

end

s16:begin

change<=1'b0;coincount<=coincount+2; product<=1'b0;

next_state<=s13;

end

endcase

end

endmodule

You might also like