Verilog Vending Machine Output Error

Discussion in 'Embedded Systems and Microcontrollers' started by monky91, Jul 5, 2013.

  1. monky91

    Thread Starter New Member

    Oct 3, 2011
    1
    0
    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?

    Code ( (Unknown Language)):
    1.  
    2. module process2(
    3. input clk,
    4. input [2:0]sw,
    5. input cancel, confirm, enter_coin,
    6. output reg[6:0]coin, refund,
    7. output reg error, dispense);
    8.  
    9.  
    10. always @(negedge confirm, negedge cancel, negedge enter_coin) begin
    11. if (!cancel) begin
    12.     refund=coin;
    13.     coin=0;
    14.     dispense=0;
    15.     error=0;
    16. end
    17.  
    18. else if (!confirm) begin   
    19.     error=1;       
    20.     if (coin>=40) begin
    21.         refund=coin-40;
    22.         coin=0;
    23.         dispense=1;
    24.         error=0;
    25.     end
    26.  
    27. end
    28.  
    29. else if (!enter_coin)begin
    30.     error<=0;
    31.     case (sw)
    32.     3'b001:coin=coin+5;
    33.     3'b010:coin=coin+10;
    34.     3'b100:coin=coin+25;
    35.     default:;
    36.     endcase
    37.     if(coin>=50) begin
    38.         refund=coin-50;
    39.         coin=50;
    40.     end
    41. end
    42. end
    43. endmodule
    44.  
     
Loading...