vhdl vending machine.

Discussion in 'Programmer's Corner' started by gammaman, Apr 10, 2009.

  1. gammaman

    Thread Starter Member

    Feb 14, 2009
    29
    0
    I am trying to implement a vhdl vending machine. I have vectors to keep track of the coins. But this is not my main concern.

    What I want to be able to do is give the user multiple choices of items all with varying prices. I then want the program to detect how many and what type of coin (nickel,dime,quarter) was put in the machine, until the price or greater is reached. It will then calculate how much money to return.

    So what I need help with is the Case selection part of the code below. I need help writing a function so that it is clean.

    Code ( (Unknown Language)):
    1.  
    2. --using textio.all
    3. Entity Soda_Machine is
    4.    port(
    5.         --would need maximum of 30 nickles
    6.         NICKEL_IN : std_logic_vector(4 downto 0);
    7.         --would need maximum of 15 dimes  
    8.         DIME_IN   : std_logic_vector(3 downto 0);
    9.         --would need maxium of 6 quarters
    10.         QUARTER_IN: std_logic_vector(2 downto 0);
    11.         SELECTION: std_logic_vector(15 downto 0);
    12.         PRICE: out std_logic_vector(15 downto 0);
    13.         RESET: BOOLEAN;
    14.         CLK: BIT;
    15.         --return change and soda
    16.         NICKEL_OUT, DIME_OUT, DISPENSE: out BOOLEAN
    17.         );
    18. End Soda_Machine;
    19.  
    20. architecture BEHAVIOR of Soda_Machine is
    21.    signal Current_Coin_Count,
    22.    Next_Coin_Count: INTEGER range 0 to 30; -- 30 nickles is $1.50
    23.    signal Current_Return_Change, Next_Return_Change : BOOLEAN;
    24. begin
    25. process(NICKEL_IN, DIME_IN, QUARTER_IN, RESET, CLK,
    26.        Current_Coin_Count, Current_Return_Change)
    27. --maximum amount of coins
    28. --possible is 30 niickles        
    29. variable Temp_Coin_Count: INTEGER range 0 to 30;
    30. begin
    31. -- Set all Change Returned to 0
    32.    NICKEL_OUT <= FALSE;
    33.    DIME_OUT <= FALSE;
    34.    DISPENSE <= FALSE;
    35.    Next_Coin_Count <= 0;
    36.    NEXT_Return_Change <= FALSE;
    37. -- Synchronous reset
    38. if (not RESET) then
    39.    Temp_Coin_Count := Current_Coin_Count;
    40.    
    41. Case SELECTION is
    42.   when "000" => PRICE := "110010";
    43. End Case;      
    44. -- Check whether change was put in the Machine
    45. if (NICKEL_IN) then
    46.    Temp_Coin_Count := Temp_Coin_Count + 1; --counting number of nickles inputed
    47.  
    48. elsif(DIME_IN) then
    49.    Temp_Coin_Count := Temp_Coin_Count + 2; --counting number of dimes inputed
    50. elsif(QUARTER_IN) then
    51.    Temp_Coin_Count := Temp_Coin_Count + 5; --counting number of quarters inputed
    52. end if;
    53. -- Keeps track if enough change was put in machine
    54. --Macine Requires 25 Nickles
    55. if(Temp_Coin_Count >= 25) then
    56.    Temp_Coin_Count := Temp_Coin_Count - 25;
    57. --soda given to customer
    58.    DISPENSE <= TRUE;
    59. end if;
    60.  
    61. --If too many nickles change is returned
    62. if(Temp_Coin_Count >= 1 or
    63.    Current_Return_Change) then
    64. if(Temp_Coin_Count >= 2) then
    65.    --dime returned
    66.    DIME_OUT <= TRUE;
    67.    Temp_Coin_Count := Temp_Coin_Count - 2;
    68.    --stil return more change
    69.    Next_Return_Change <= TRUE;
    70. end if;
    71. if(Temp_Coin_Count = 1) then
    72.    --nickle returned
    73.    NICKEL_OUT <= TRUE;
    74.    Temp_Coin_Count := Temp_Coin_Count - 1;
    75. end if;
    76. end if;
    77. Next_Coin_Count <= Temp_Coin_Count;
    78. end if;
    79. end process;
    80.  
    81. process
    82. begin
    83. --returns change on positive clock edege
    84. wait until CLK' event and CLK = '1';
    85. Current_Return_Change <= NEXT_Return_Change;
    86. Current_Coin_Count <= Next_Coin_Count;
    87. end process;
    88. end BEHAVIOR;
    89.  
     
Loading...