Verilog Vending Machine Output Error

Thread Starter

monky91

Joined Oct 3, 2011
1
The code below is meant to work as a vending machine that only sells a drink that is 40 cent. There are 3 switches that represent 5, 10 and 25 cent; 3 pushbutton that represent confirm, cancel and enter coin. When the enter button is pressed, coin will display the amount of coins that are inputted. When confirm is pressed, the machine will dispense the drink and return the change(refund); if not enough money is inserted, it will show an error sign. When cancel is pressed, all the money will be refund.

The code for the coin entering works perfectly. But there is a problem when cancel or confirm key is pressed. When confirm is pressed, the output fluctuates alot. When cancel is pressed, the refund should show the amount that is in coin, however it shows 0.

Attached is the waveform simulation result. The red circle shows that the output keep fluctuate, while the blue circle shows that refund=0, which is suppose to be 5. Attachment2 show the fluctuated values.

Can anyone tell me what might be the problem?

Rich (BB code):
module process2(
input clk,
input [2:0]sw,
input cancel, confirm, enter_coin,
output reg[6:0]coin, refund,
output reg error, dispense);


always @(negedge confirm, negedge cancel, negedge enter_coin) begin
if (!cancel) begin
	refund=coin;
	coin=0;
	dispense=0;
	error=0;
end

else if (!confirm) begin	
	error=1;		
	if (coin>=40) begin
		refund=coin-40;
		coin=0;
		dispense=1;
		error=0;
	end

end

else if (!enter_coin)begin
	error<=0;
	case (sw)
	3'b001:coin=coin+5;
	3'b010:coin=coin+10;
	3'b100:coin=coin+25; 
	default:;
	endcase
	if(coin>=50) begin
		refund=coin-50;
		coin=50;
	end
end
end
endmodule
 

Attachments

Top