Sunteți pe pagina 1din 28

4/22/2008

ECE 52

VENDING MACHINE DESIGN

Final Project | Ankit Prasad and Sandip Agrawal

Introduction
This project focuses on the design of the software for a vending machine. A vending machine can be described as a finite state machine with various operations in each state. Using this concept we programmed the machine in Quartus and then implemented it on an FPGA. The vending machine simulates an actual real-life vending machine in its working and has the following features: Takes the most common coins: Nickels, Dimes, and Quarters Keeps track of four items: A (5 cents), B (10 cents), C (15 cents) and D (30 cents) Keeps a change box and a count of the number of nickels, dimes, and quarters remaining for change purposes Vends the item selected if enough money has been inserted and the item has not been sold out. Asks the user to insert more money if the price of the item selected exceeds the amount put in. Requests the user to make another selection if the item selected has been sold out. Calculates and returns the resulting change. Also returns the change in a smart way by using the fewest number of coins possible and substituting smaller denomination coins when out of a larger denomination one (ex. two nickels for a dime) Provides an option to cancel the transaction and eject all money inserted prior to buying an item Provides a way to restock items and change coins Keeps track of the total revenue

Design
The vending machine was treated as one large finite state machine where decisions and selections were made in the different stages. The basic state diagram of our finite state machine is incorporated in the image below:

Figure 1: Finite State Diagram

The machine starts in state A where a user makes a selection after which it goes to state B. In state B, it checks if the user has input enough money: if he hasnt, the machine returns to state A, if he has, it moves to state C. In state C, it checks if the item is sold out: if it is, the machine returns to state A (after lighting a sold out led), if it isnt, the machine moves on to state D. In state D, the machine lights the vend led, decrements the count of the item bought, updates the total revenue, and passes the total change to be returned to state E. In state E, the machine returns change with the fewest number of coins possible and the machine then moves back to state A. If while inserting money, the user chooses to eject his money, the machine moves to state F where all the money inserted is returned after which the machine moves back to state A. In implementing this design we used a Moore-type finite state machine for in our design the outputs do not take the input directly and thus get fixed for a clock cycle. This is opposed to a mealy design where changing inputs will change the output the entire duration the clock is high and thus outputs are valid only at the clock edge. Implementing all this functionality required the design of a number of subcomponents, which were then finally integrated together in the finite state machine. The figure below shows the major components of our vending machine:

Figure 2: Vending Machine Components

The main components used in the vending machines and the finite states are described in detail below:

NDQ-COUNT The NDQCOUNT tracks the number of nickel, dime and quarters the user inputs, and outputs the total number of each coin inserted and their total value. We have used three counters to keep track of the number of each coin. When a user inputs a coin, the count enable of the respective counter becomes high and the corresponding counter increases by 1. The number of coins is multiplied with its money value using a multiplier and then they are all summed using an adder. This way the final output represents the total money inserted by the user. The schematic of NDQ-count is shown in Figure 3.
lpm_counter0
clearA
INPUT VCC INPUT VCC INPUT VCC

up counter

sclr clock cnt_en


inst
OUTPUT

clockA

q[3..0]

N[ 3..0]

N in

lpm_mult0
dataa[3..0] result[7..0] 5
Unsigned mult iplication

lpm_counter0
up counter

inst 9

sclr clock
Din
INPUT VCC

lpm_add_sub0
q[3..0]
OUTPUT

D[3.. 0]

dataa[7..0]

cnt_en
inst1

dataa[3..0] result[7..0] 10
Unsigned multiplicat ion inst12

cout

lpm_counter0
up counter

inst10

sclr
OUTPUT

dat a[ 8]

lpm_mult1

datab[7..0]

A A+B B

result[7..0]

dat a[ 7..0].0] data[8.

lpm_add_sub1
Q[3. .0]

clock
Qin
INPUT VCC

result[8..0]

sumNDQ[ 9]

q[3..0]

dataa[8..0]

cnt_en
inst2

lpm_mult2
dataa[3..0] result[8..0] 25
inst 11 U nsigned m ultiplication

datab[8..0]
inst 15

A A +B B

sumNDQ[ 8. .0] sum NDQ[9. .0]


OUTPUT

sumNDQ[ 9.. 0]

cout

sumN DQ[9]

Figure 3: NDQCount.bdf

CHANGECOINS CHANGECOINS keeps track of the number of coins that are available in the change box of the vending machine. There are three counters to count the number of nickel, dimes and quarters. There is a common presetcoin input which presets the value of each counter to 5 coins. This is used to load the change box. We have used three down counters, one for each type of coin. Whenever, the machine returns a change as a particular coin, the count enable of that counter becomes high and the value of the counter decreases by 1. Thus, the output returns us the number of each coin in the change box. The schematic diagram is shown below.
lpm_counter4
presetcoin clock
INPUT VCC INPUT VCC INPUT VCC

sset clock

down counter sset 5

OUTPUT

decrN

countn[3..0]

q[3..0] cnt_en
inst

lpm_counter4
sset clock
decrD
INPUT VCC

down counter sset 5

OUTPUT

countd[3..0]

q[3..0] cnt_en
inst1

lpm_counter4
sset clock
decrQ
INPUT VCC

down counter sset 5

OUTPUT

countq[3..0]

q[3..0] cnt_en
inst2

Figure 4: changecoins.bdf

COINRET COINRET compares the magnitude of the change with the value of the three types of coins and outputs if the change is greater or equal to 25, 10 and 5 cents. The diagram below shows the schematic of COINRET. The input value is the change remaining to be returned. There are three comparators that compare the input value to 5, 10, and 25 and outputs if the change to be returned is greater than or equal to 5, 10 and 25 cents, thus giving rise to three outputs. For instance, if the input to this block is 20, then cgtD and cgtN both will be high. This was used later in deciding which coin was returned as we gave precedence to the higher denomination coins if available in the change box i.e. we return a dime here instead of two nickel if a dime is available and the change is greater than or equal to 10 cents.
lpm_compare2
unsigned compare cdata1[9..0]
INPUT VCC

dataa[9..0] datab[]=25
inst

ageb

OUTPUT

cgtQ

lpm_compare3
unsigned compare

dataa[9..0] datab[]=10
inst1

ageb

OUTPUT

cgtD

lpm_compare4
unsigned compare

dataa[9..0] datab[]=5
inst2

ageb

OUTPUT

cgtN

Figure 5: coinret.bdf

TOTREV TOTREV is our total revenue counter that keeps track of the total amount of revenue obtained by the vending machine. In other words, it represents the total amount of transactions occurred. The schematic diagram for TOTREV is shown below. We have used an adder and a flip-flop register to keep track of the total revenue. When a transaction occurs, the input to the adder is fed with the price of the item that is sold and the other input is the amount previously stored. The adder sums the two values and is fed into the register. The register stores the value. When a next transaction occurs, the price of the item is added to this new amount thereby generating the total money in the register.

lpm_add_sub3
data[9..0]
INPUT VCC

dataa[9..0]

A A+B datab[9..0] B
inst

result[9..0]

lpm_dff0
DFF

clear

INPUT VCC INPUT VCC

clk

sclr data[9..0] clock


inst1

q[9..0]

OUTPUT

rev [9..0]

Figure 6: totrev.bdf

ITEMRECORD ITEMRECORD keeps track of the number of each item that is available in the vending machine. We used four down counters, one for each of our item. Each of the counters has a common input called presetitem that presets the value of the counter to 4 items. This is used to load the machine with items to be sold. All the down counters have a count enable input. When a user buys an item, the count enable for that item gets high and that item counter decreases by 1. The four outputs return the number of items which are used to detect when an item is sold out. The schematic diagram is shown below.
lpm_counter1
presetitem
INPUT VCC INPUT VCC INPUT VCC

sset clock

down counter sset 4

clock

q[3..0]
decA

OUTPUT

itemcounta[3..0]

cnt_en
inst

lpm_counter1
sset clock
decB
INPUT VCC

down counter sset 4

q[3..0] cnt_en
inst1

OUTPUT

itemcountb[3..0]

lpm_counter1
sset clock q[3..0]
decC
INPUT VCC OUTPUT

down counter sset 4

itemcountc[3..0]

cnt_en
inst2

lpm_counter1
sset clock
decD
INPUT VCC

down counter sset 4

OUTPUT

itemcountd[3..0]

q[3..0] cnt_en
inst3

Figure 7: itemrecord.bdf

TOTRETDISP TOTRETDISP computes the total number of each coin to be returned as change and returns these numbers as the inputs to seven segment displays. The schematic block diagram is shown below. We used three counters, one for each type of coin, to store the number of total coins returned as change. We also used three multiplexers in the schematic. The inputs to these multiplexers are the total number of coins to be returned as change from the change counters and the total number of coins the user inserted. This was done so that when a person presses the eject button, the multiplexer selects each coins the user inserted and outputs it. Else, the multiplexer selects the return change coins. The outputs of the multiplexers are fed into bcdtoseven blocks that converts the Binary Coded Decimal as the inputs to seven segment displays.
retD in[3.. 0] retN in[3.. 0] retQin[3.. 0] dummy Q[3..0] dummy D[3..0] dummy N[3..0]
OUTPUT OUTPUT OUTPUT

bcdtosev en N[3. .0] disp[ 1.. 7]


OUTPUT

Nret disp[1..7]

lpm_counter3
clearret clockret
INPUT VCC INPUT VCC

inst1
VCC INPUT VCC INPUT VCC INPUT

up counter

sclr clock

lpm_mux2
q[3..0] data1x[3..0] data0x[3..0]
inst6 bcdtosev en

result[3..0]
N[3. .0] disp[ 1.. 7]
OUTPUT

retN

INPUT VCC

cnt_en
inst

Dret disp[1..7]

sel
inst2

lpm_counter3
up counter

sclr clock
retD
INPUT VCC

lpm_mux2
q[3..0] data1x[3..0] data0x[3..0]
inst 7

result[3..0] sel

cnt_en
inst4

lpm_counter3
up counter

sclr clock
retQ
INPUT VCC

lpm_mux2
bcdtosev en

q[3..0]

data1x[3..0] data0x[3..0]
inst8

result[3..0] sel

N[3. .0]

disp[ 1.. 7]

OUTPUT

Qret disp[1..7]

cnt_en
inst5

inst3
NOT

Loadret

INPUT VCC

inst39

Figure 8: totretdisp.bdf

PCOMP (Price Comparison) PCOMP takes the total amount the user inputs to buy an item, and outputs if the user has inserted sufficient money to buy the item. In other words, PCOMP checks if the money input by the user, is greater than or equal to the price of the item. The schematic diagram for the price comparator is shown below. We used four comparators, one for each item, and outputs if the input money is greater than or equal to the corresponding item. The outputs of these comparators are then inserted into the multiplexer that selects the comparator output that represents the item selected by the user and returns as the output. For instance, if a user inserts 25 cents and selects Item D, the block will output 0 as the price of item D is 30 cents but it will output 1 if the user selects item A, B, or C as these cost less than 25 cents.

selSD selSC selSB

INPUT VCC INPUT VCC INPUT VCC

lpm_compare6
unsigned compare sumndq1[9..0]
INPUT VCC

dataa[9..0] datab[]=5
inst

ageb

OR2

inst38

lpm_compare0
unsigned compare f[1..0] f [1] f[0] f [0]

dataa[9..0] datab[]=10
inst4

ageb

inst7

sel[1..0]

lpm_compare1
unsigned compare

data0 data1 data2 data3

inst40

OR2

OUTPUT

result

enuf

lpm_mux0
dataa[9..0] datab[]=15
inst5

ageb

lpm_compare5
unsigned compare

dataa[9..0] datab[]=30
inst1

ageb

Figure 9: PComp.bdf

VENDMAIN

This is the heart of our Vending Machine. It integrates all of the other components. The VENDMAIN vhdl file implements the state decisions made by the vending machine. The states and the descriptions are as follows:

Figure 10: States in vendmain.vhd

State A When the machine is started, it goes into this state. The machine remains in this state while the user inputs money. There are three input switches that determine the coin inserted. When the user inserts a coin, the NDQCOUNT updates itself and stores the total amount of money input. The machine remains in this state unless the person selects an item or presses eject. The machine passes to state F if the user presses eject and to state B if the user selects an item. State B In this state, the machine checks if the amount inserted by the user is greater than or equal to the price of the item selected. This is done by the PCOMP component described above. If user inputs less money, the machine asks the user to input more money and goes back to state A. If the money is sufficient, the machine passes to state C. State C In this state, it checks if the machine has the selected item. This is done by comparing the item stock count with 0. If the item is not available, it lights a sold out led and returns to State A, where the user can either eject their money out or choose a different item. If the item is available, the machine goes to state D. State D In this state, the machine vends the selected item, and decreases the item count by 1. It also increases the amount in the revenue counter by the price of the item so that the revenue counter remains updated. The machine updates the item count in the component ITEMRECORD and updates the total revenue in the TOTREV component. The machine then passes to state E. State E In this state, a subtractor calculates the change to be returned. If no change is to be returned, the transaction ends and the machine go back to its initial state A. But instead, if the machine has to return the change, COINRET determines what coins to return. It does the calculation so that the machine has to return the minimum number of coins using the coins available in the change box. It then shows the number of returned coins in seven segment displays. The machine stays in this state until there is no more change to be returned. The machine updates the number of coins in the change box and then goes back to the initial state A. State F The machine comes to this state if the user presses the eject button. In this state, the machine takes the count of nickels, dimes and quarters from the sumndqcount component and outputs it in the change return displays. Once this is done, the program goes back to state A. *States A2, A3, A4, F2, Fwait, Feject, SWait, Q, R, and R2 A careful analysis of vendmain.vhd reveals these states as well. These states are all intermediary steps that were used between the above stages for delaying, debugging, or ease of coding purposes and do not serve significant roles apart from aiding transitions.

BIGVENDMAIN This is the final block diagram that takes all the inputs and outputs of the vending machine. The VENDMAIN described above is the main part of it where all the decision making process takes place. The schematic diagram is shown below. The inputs to the machine are as follows : Dmach, Nmach, Qmach -> Switches to input coins to the machine SelA, SelB, SelC, SelD -> Select an item Presetcoins, Presetitems -> Loads the machine with change coins and items Eject, Reset, EndWait - > Other Controls The outputs to the machine are: VendA, VendB, VendC, VendD -> Vend an item Soldout, moremoney, changeout -> Pretty self-explanatory dcountn, dcountd, dcountq -> Number of change coins in the change box dcountA, dcountB, dcountC, dcountD -> Number of each item in the machine sum_tens, sum_ones -> the tens and ones digit of the total input money NretDisp, Qretdisp, Dretdisp -> number of change coins to be returned Totrev -> total revenue The total money input by the user (sum_tens and sum_ones) is displayed using two seven segment displays. The number of change coins returned is also displayed using three separate seven segments. The counts of items remaining, change coins left, and the total revenue are all displayed on one seven segment display with the help of a multiplexer. We used LEDs to represent the vending of the items (VendA/B/C/D) and for other outputs like soldout, moremoney and changeout.
7447
st[ 1] st[ 2] st[ 3]

A B C D LTN RBIN BIN BCD TO inst 39


so[0]

OA OB OC OD OE OF OG RBON 7SEG OA OB OC OD OE OF OG RBON 7SEG

st[ 2. .0] st [0] st[ 1. .0]

OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT

st1 st2 st3 st4 st5 st6 st7

PIN_6 PIN_7 PIN_8 PIN_9 PIN_11 PIN_12 PIN_13

7447

so[2..0] so[1..0]

so[1] so[2] so[3]

A B C D LTN RBIN BIN

OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT

so1 so2 so3 so4 so5 so6 so7

PIN_17 PIN_18 PIN_19 PIN_20 PIN_21 PIN_23 PIN_24

vendmain clock
INPUT VCC

NOT

clock reset
INPUT VCC INPUT VCC INPUT VCC INPUT VCC INPUT VCC INPUT VCC INPUT VCC INPUT VCC INPUT VCC INPUT VCC

vendA vendB v endC v endD soldout moremoney changeout dummyN[ 3. .0] dummyD[ 3. .0]

OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT

v endA sel[2..0] v endB v endC v endD soldout moremoney changeout

BCD TO inst 40 PIN_109 PIN_110 PIN_111 PIN_113 PIN_114 PIN_115 PIN_116


st[ 3. .0] so[3.. 0]
VCC INPUT

reset

INPUT VCC

PIN_230
inst9 Nmach

PIN_191

Nmach Dmach Qmach selA selB selC selD eject presetit em presetcoins endwait

PIN_192 Dmach PIN_193 Qmach PIN_194 PIN_195 PIN_196 PIN_198 PIN_199 PIN_200 PIN_201 PIN_202
selA selB selC selD eject preset item preset coins

dummyQ[ 3. .0] sum_tens[ 3. .0] sum_ones[ 3. .0] dcount d[3..0] dcount n[3..0]

result [2..0] 0] result [1..

NOT

endwait

INPUT VCC

dcount q[3..0] dit emcounta[3.. 0] dit emcountb[3.. 0] dit emcountc[3.. 0] dit emcountd[3.. 0] Nretdisp[ 1. .7] Dretdisp[ 1. .7] Qretdisp[ 1. .7] rev [9.. 0] inst
OUTPUT OUTPUT OUTPUT

PIN_41 inst26 PIN_40 data0x[3..0] PIN_39 data1x[3..0] data2x[3..0]

sel[2..0]

result [0] result[ 0] result[ 1] result[ 2] result[ 3]

7447
A B C D LTN RBIN BIN OA OB OC OD OE OF OG RBON

NOT OUTPUT NOT NOT NOT NOT NOT NOT

D1 D2 D3 D4 D5 D6 D7

inst 2 inst 3 inst 4 inst 5 inst 6 inst 7 inst 8

OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT OUTPUT

result[ 3. .0]

PIN_117 PIN_118 PIN_119 PIN_120 PIN_126 PIN_127 PIN_128

PIN_231

inst 1

Nret disp[1.. 7] Dret disp[1.. 7] Qret disp[1.. 7]

data3x[3..0] result[3..0] data4x[3..0] data5x[3..0] data6x[3..0] data7x[3..0] PIN_138 PIN_139 PIN_131 lpm_mux3 PIN_141 PIN_129 PIN_147 PIN_142 PIN_132 PIN_148 PIN_143 PIN_133 PIN_149 PIN_144 PIN_134 PIN_151 PIN_146 PIN_136 PIN_152 PIN_137 PIN_153 PIN_154

BCD TO 7SEG inst 27

lpm_mux4

totrev conv rdec[ 7. .4] 3. .0] rdec[ rev bin[ 9. .0] rev dec[11. .0] rdec[ 11.. 0]

rdec[ 11.. 8]

data3x[3..0] data2x[3..0] data1x[3..0] data0x[3..0]


inst 12

result[3..0]

inst15

lpm_counter5
up counter

sel[1..0]

clock
inst10

q[1..0]
VCC

Figure 11: bigvendmain.bdf

Design Contributions
Although blanket statements such as "We worked equally on all parts" have been discouraged, since there was never a moment in this design project when one person worked without the others presence, it is truly tough to distinguish our contributions. However, we have tried to specify our contributions as follows: Sandip: Ankit: Simulation and testing the basic components prior to vendmain.vhd Coded the states of vendmain.vhd Implemented the total revenue counter Troubleshooting most software components and designs Wiring most of the hardware Designed the basic components prior to vendmain.vhd Designed and deduced the necessary states of vendmain.vhd Incorporated vendmain.vhd into bigvendmain.bdf and connected the inputs and outputs Pin assignments for most of the hardware Hardware troubleshooting

Simulation Steps
Note: all variables are pretty self explanatory except dummyN, dummyD, and dummyQ which denote the change returned in decimal as opposed to Nretdisp, Dretdisp, and Qretdisp which are formatted for the seven segment display Reset, presetcoins to 5 (see dcount# where # is n, d, or q), presetitems to 4 (see ditemcount# where # is a, b, c, or d) Insert 1N, 1Q, and 1D. Select A. (Return 1Q and 1D) Insert 1N and 1D. Select B. (Return 1N) Insert 1N. Select C. (More Money case) Insert 1D. Select C. (Return 0) Insert 1N. Select A. (Return 0) Insert 1N and 1D. Eject. (Return 1N and 1D) Insert 1Q. Select A. (Return 2D) Insert 1N. Select A. (Return 0) Insert 1N. Select A. (Sold Out case)

Conclusion
In this project, we successfully designed and implemented a simple vending machine. This vending machine kept tally of its four items, change coins available, and total revenue. It vended the item requested and returned change in the fewest coins possible amongst those available and had indicators to indicate special cases such as soldout and moremoney. The hardware implementation of the vending machine had a number of seven segment displays enabling the user to note the money inserted, change returned, item counts, change coin counts, and total revenue. We learnt a lot about digital logic in completing this project. We learnt how to break down a real world problem to that solvable by a finite state machine. We also sharpened our VHDL skills and Quartus troubleshooting skills. In implementing the hardware, we improved our wiring and debugging skills. We would like to thank Dr. Dwyer for empowering us with knowledge enabling us to design this vending machine. We would also like to thank all the ECE 52 TAs, especially our own lab TA, Mike Feng, for helping us along the way.

Date: April 22, 2008


0 ps 16.45 ns
clock reset presetcoins presetitem Nmach Dmach Qmach eject selA selB selC selD dummyN dummyD dummyQ vendA vendB vendC vendD soldout moremoney sum_ones sum_tens dcountn dcountd dcountq ditemcounta ditemcountb ditemcountc ditemcountd rev endwait changeout 0 0 0 0 0 0 0 0 5 15 5 5 4 4 4 3 0 0 5 3 4 5 0 0 5 5 1 0 0 5 0 0 0 1 1 1

db/vending.sim.cvwf*

Project: vending
140.0 s

0 0 0

1 1 0 12

0 0

5 1 4

0 0

0 5

5 1

0 0 4

5 2

0 0

5 2 2

4 2 3 3 4 30 35 40 45 1 0

Page 1 of 2

Revision: vending

Date: April 22, 2008


0 ps 16.45 ns
Nretdisp Dretdisp Qretdisp 0111111 0111111 0111111 0000110 0000110 0000110

db/vending.sim.cvwf*

Project: vending
140.0 s

0111111 0111111 0111111 0111111 1011011

0111111 0111111

Page 2 of 2

Revision: vending

Date: April 22, 2008

vendmain.vhd

Project: vending

1 2 3 4 5 6 7

library ieee; USE ieee.std_logic_1164.all; ENTITY vendmain IS PORT( clock, reset, Nmach,Dmach,Qmach, selA, selB, selC, selD, eject, presetitem, presetcoins, endwait: IN STD_LOGIC; vendA, vendB,vendC, vendD, soldout,moremoney, changeout: OUT STD_logic ; dummyN, dummyD, dummyQ, sum_tens, sum_ones, dcountd, dcountn, dcountq,ditemcounta, ditemcountb, ditemcountc,ditemcountd : OUT STD_LOGIC_VECTOR(3 DOWNTO 0); Nretdisp, Dretdisp, Qretdisp: OUT STD_LOGIC_VECTOR(1 TO 7); rev: OUT STD_LOGIC_VECTOR(9 DOWNTO 0)); END vendmain ; -- changeout is '1' when there is no change coin to return ARCHITECTURE behaviour of vendmain IS COMPONENT NDQcount port ( clearA : IN STD_LOGIC; clockA : IN STD_LOGIC; Nin : IN STD_LOGIC; Din : IN STD_LOGIC; Qin : IN STD_LOGIC; D : OUT STD_LOGIC_VECTOR(3 downto 0); N : OUT STD_LOGIC_VECTOR(3 downto 0); Q : OUT STD_LOGIC_VECTOR(3 downto 0); sumNDQ : OUT STD_LOGIC_VECTOR(9 downto 0) ); END COMPONENT; COMPONENT lpm_add_sub2 PORT ( dataa : IN STD_LOGIC_VECTOR (9 DOWNTO 0); datab : IN STD_LOGIC_VECTOR (9 DOWNTO 0); result : OUT STD_LOGIC_VECTOR (9 DOWNTO 0) ); END COMPONENT; COMPONENT PComp port ( selSD : IN STD_LOGIC; selSC : IN STD_LOGIC; selSB : IN STD_LOGIC; sumndq1 : IN STD_LOGIC_VECTOR(9 downto 0); enuf : OUT STD_LOGIC );
Page 1 of 9 Revision: vending

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

Date: April 22, 2008

vendmain.vhd

Project: vending

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

END COMPONENT; COMPONENT itemrecord port ( clock : IN STD_LOGIC; decA : IN STD_LOGIC; decB : IN STD_LOGIC; decC : IN STD_LOGIC; decD : IN STD_LOGIC; presetitem : IN STD_LOGIC; itemcounta : OUT STD_LOGIC_VECTOR(3 itemcountb : OUT STD_LOGIC_VECTOR(3 itemcountc : OUT STD_LOGIC_VECTOR(3 itemcountd : OUT STD_LOGIC_VECTOR(3 ); END COMPONENT;

downto downto downto downto

0); 0); 0); 0)

COMPONENT coinret port ( cdata1 : IN STD_LOGIC_VECTOR(9 downto 0); cgtQ : OUT STD_LOGIC; cgtD : OUT STD_LOGIC; cgtN : OUT STD_LOGIC ); END COMPONENT; COMPONENT changecoins port ( clock : IN STD_LOGIC; presetcoin : IN STD_LOGIC; decrN : IN STD_LOGIC; decrD : IN STD_LOGIC; decrQ : IN STD_LOGIC; countd : OUT STD_LOGIC_VECTOR(3 downto 0); countn : OUT STD_LOGIC_VECTOR(3 downto 0); countq : OUT STD_LOGIC_VECTOR(3 downto 0) ); END COMPONENT; COMPONENT retdisp port ( clearret : IN STD_LOGIC; clockret : IN STD_LOGIC; retN : IN STD_LOGIC; retD : IN STD_LOGIC; retQ : IN STD_LOGIC; Loadret : IN STD_LOGIC;
Page 2 of 9 Revision: vending

Date: April 22, 2008

vendmain.vhd

Project: vending

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

retDin : IN STD_LOGIC_VECTOR(3 downto 0); retNin : IN STD_LOGIC_VECTOR(3 downto 0); retQin : IN STD_LOGIC_VECTOR(3 downto 0); Dretdisp : OUT STD_LOGIC_VECTOR(1 to 7); dummyD : OUT STD_LOGIC_VECTOR(3 downto 0); dummyN : OUT STD_LOGIC_VECTOR(3 downto 0); dummyQ : OUT STD_LOGIC_VECTOR(3 downto 0); Nretdisp : OUT STD_LOGIC_VECTOR(1 to 7); Qretdisp : OUT STD_LOGIC_VECTOR(1 to 7) ); END COMPONENT;

COMPONENT sumndqdisp PORT ( sumndqbin : IN STD_LOGIC_VECTOR(9 DOWNTO 0); sumndq_tendig, sumndq_onedig : OUT STD_LOGIC_VECTOR(3 DOWNTO 0)); END COMPONENT; COMPONENT sumcomp IS port ( sumndq95 : IN STD_LOGIC_VECTOR(9 downto 0); sumndqg95 : OUT STD_LOGIC ); END COMPONENT; COMPONENT totrev IS port ( clear : IN STD_LOGIC; clk : IN STD_LOGIC; data : IN STD_LOGIC_VECTOR(9 downto 0); rev : OUT STD_LOGIC_VECTOR(9 downto 0) ); END COMPONENT; -- four bit signals SIGNAL Nout, Dout, Qout, itemcounta, itemcountb,itemcountc, itemcountd,countn, countd, countq : STD_LOGIC_VECTOR(3 downto 0); -- ten bit signals SIGNAL sumndq, data1,data2,subout,cdata1, change, changesig, addtorev: STD_LOGIC_VECTOR(9 downto 0); -- one bit signals SIGNAL Nin, Din, Qin, SelSA, SelSB, SelSC, SelSD, enuf, decA, decB, decC, decD,decrN, decrD, decrQ, cgtQ, cgtD, cgtN,retQ, retN, retD, g95, clrrev : STD_logic; -- clear load signals SIGNAL clearndq, clearret, Loadret: STD_logic;
Page 3 of 9 Revision: vending

Date: April 22, 2008

vendmain.vhd

Project: vending

145 146 147 148 149 150 151 152 153 154 155 156 157 158

TYPE STATE_type IS (A, A2, A3, A4, B, C, D, E, F, F2, Fwait, Feject, SWait, Q, R, R2); SIGNAL y : STATE_type; -- data1 = money u input, data2 = price of item selected,

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

BEGIN ndqcount1: NDQcount PORT MAP (clearndq, clock, Nin, Din, Qin, Dout, Nout, Qout, sumndq); pcomp1: PComp PORT MAP ( selSD, selSC, selSB, sumndq, enuf); item1: itemrecord PORT MAP (clock, decA, decB, decC, decD, presetitem, itemcounta, itemcountb, itemcountc,itemcountd); subs1: lpm_add_sub2 PORT MAP (data1, data2, subout); ccoins1: changecoins PORT MAP (clock, presetcoins, decrN, decrD, decrQ, countd, countn, countq); comp1: coinret PORT MAP (change, cgtQ, cgtD, cgtN); retdisp1: retdisp PORT MAP (clearret, clock, retN, retD, retQ, Loadret, Dout, Nout, Qout, Dretdisp, dummyD, dummyN, dummyQ, Nretdisp, Qretdisp); sumndqdisp1: sumndqdisp PORT MAP (sumndq, sum_tens, sum_ones); sumcomp1: sumcomp PORT MAP (sumndq, g95); totrev1: totrev PORT MAP (clrrev, clock, addtorev, rev); Main: PROCESS(clock, reset) BEGIN IF (reset ='1') THEN y<=R ; ELSIF(clock'EVENT AND clock ='1') THEN CASE y IS WHEN A => --changetest<=change; --change<="0000000000"; dcountd<=countd; dcountn<=countn; dcountq<=countq; ditemcounta<=itemcounta; ditemcountb<=itemcountb; ditemcountc<=itemcountc; ditemcountd<=itemcountd; ---Nin <= Nmach; Din <= Dmach; Qin <= Qmach; clearndq <= '0'; changeout <= '0'; clearret <= '0'; vendA <='0'; vendB <='0'; vendC <='0'; vendD <='0'; moremoney <='0'; soldout <='0'; IF (eject='1' OR g95='1') THEN
Page 4 of 9 Revision: vending

Date: April 22, 2008

vendmain.vhd

Project: vending

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

-------

-<=selD; --

Loadret<='1'; y<=Feject; ELSIF (Nmach OR Dmach OR Qmach)='1' THEN y<=A2; ELSIF (eject='1' OR sumndq>"0001011111") THEN Loadret<='1'; y<=Feject; y<=F; ELSIF (selA OR selB OR selC OR selD)='1' THEN y<=A4; selSA <=selA; selSB <=selB; selSC <=selC; selSD y<=B; ELSE y<=A; END IF;

WHEN A2 => IF Nmach='1' THEN Nin<='1'; y<=A3; ELSIF Dmach='1' THEN Din<='1'; y<=A3; ELSIF Qmach='1' THEN Qin<='1'; y<=A3; ELSE y<=A; END IF; WHEN A3 => Nin<='0'; Din<='0'; Qin<='0'; IF (Nmach OR Dmach OR Qmach)='1' THEN y<=A3; ELSE y<=A; END IF; WHEN A4 => IF (selA OR selB OR selC OR selD)='1' THEN selSA <=selA; selSB <=selB; selSC <=selC; selSD <=selD; y<=A4; ELSE y<=B; END IF;

WHEN B => IF enuf='0' THEN moremoney <= '1';


Page 5 of 9 Revision: vending

Date: April 22, 2008

vendmain.vhd

Project: vending

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

-ELSE

y<=SWait; y<=A; y<=C; END IF;

WHEN C => IF selSA ='1' THEN IF itemcounta= "0000" THEN soldout <='1'; y<=SWait; -y<=A; ELSE y<=D; END IF; ELSIF selSB='1' THEN IF itemcountb= "0000" THEN soldout <='1'; y<=SWait; y<=A; ELSE y<=D; END IF; ELSIF selSC = '1' THEN IF itemcountc= "0000" THEN soldout <= '1'; y<=SWait; y<=A; ELSE y<=D; END IF; ELSIF selSD='1' THEN IF itemcountd= "0000" THEN soldout <='1'; y<=SWait; y<=A; ELSE y<=D; END IF; END IF; WHEN SWait => IF endwait = '1' THEN y<=A; ELSE y<=SWait; END IF;

--

--

--

Page 6 of 9

Revision: vending

Date: April 22, 2008

vendmain.vhd

Project: vending

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

WHEN D => data1 <=sumndq; IF selSA='1' THEN vendA <='1'; decA <='1'; data2 <= "0000000101"; addtorev <= "0000000101"; ELSIF selSB='1' THEN vendB <='1'; decB <='1'; data2 <= "0000001010"; addtorev <= "0000001010";

ELSIF selSC='1' THEN vendC <='1'; decC <='1'; data2 <="0000001111"; addtorev <="0000001111"; ELSIF selSD='1' THEN vendD <='1'; decD <='1'; data2 <="0000011110"; addtorev <="0000011110"; END IF; --change <= subout; changetest<=subout; y<=Q; -IF change="0000000000" THEN -y<=F; --ELSE -y<=E; --END IF;

WHEN E =>

Loadret<='0'; data1<=change; IF change="0000000000" THEN y<=Fwait; y<=F; ELSIF cgtQ='1' AND countq/="0000" THEN retQ <='1';
Page 7 of 9 Revision: vending

--

Date: April 22, 2008

vendmain.vhd

Project: vending

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

decrQ <='1'; data2 <= "0000011001"; y <=Q; ELSIF cgtD='1' AND countd/="0000" THEN retD <='1'; decrD <='1'; data2 <= "0000001010"; y <=Q; ELSIF cgtN='1' AND countn/="0000" THEN retN <= '1'; decrN <= '1'; data2 <= "0000000101"; y <=Q; ELSE changeout <='1'; y<=Fwait; y<=F; END IF;

--

-- changesig<=subout; -- y <=Q; -- change <= subout; WHEN FWait => IF endwait = '1' THEN y<=F; ELSE y<=Fwait; END IF; WHEN Q => addtorev <= "0000000000"; change<=subout; decA <='0'; decB <= '0'; decC <='0'; decD <='0'; retQ <='0'; retD <='0'; retN <='0'; decrQ <='0'; decrN <='0'; decrD <='0'; y<=E; change<=changesig;

--

WHEN Feject=> IF endwait='1' THEN y<=F; ELSE y<=Feject; END IF; -----IF eject='1' THEN y<=Feject; ELSE y<=F; END IF;
Page 8 of 9 Revision: vending

Date: April 22, 2008

vendmain.vhd

Project: vending

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

WHEN F => clrrev <= '0'; clearndq <='1'; clearret <='1'; Loadret<='0'; y<=F2; WHEN F2 => y<=A; WHEN R => -y<=F; y<=R2;

WHEN R2 => clrrev <= '1'; addtorev <= "0000000000"; y<=F; END CASE; END IF; END Process; END behaviour;

Page 9 of 9

Revision: vending

Date: April 22, 2008

sumndqdisp.vhd

Project: vending

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

LIBRARY ieee; USE ieee.std_logic_1164.all; USE ieee.std_logic_unsigned.all; ENTITY sumndqdisp IS PORT ( sumndqbin : IN STD_LOGIC_VECTOR(9 DOWNTO 0); sumndq_tendig, sumndq_onedig : OUT STD_LOGIC_VECTOR(3 DOWNTO 0)); END sumndqdisp; ARCHITECTURE behavior OF sumndqdisp IS SIGNAL sumndqdec : STD_LOGIC_VECTOR(7 DOWNTO 0); BEGIN PROCESS(sumndqbin) BEGIN IF (sumndqbin = "0000000000") THEN sumndqdec <= "00000000"; ELSIF (sumndqbin = "0000000101") THEN sumndqdec <= "00000101"; ELSIF (sumndqbin = "0000001010") THEN sumndqdec <= "00010000"; ELSIF (sumndqbin = "0000001111") THEN sumndqdec <= "00010101"; ELSIF (sumndqbin = "0000010100") THEN sumndqdec <= "00100000"; ELSIF (sumndqbin = "0000011001") THEN sumndqdec <= "00100101"; ELSIF (sumndqbin = "0000011110") THEN sumndqdec <= "00110000"; ELSIF (sumndqbin = "0000100011") THEN sumndqdec <= "00110101"; ELSIF (sumndqbin = "0000101000") THEN sumndqdec <= "01000000"; ELSIF (sumndqbin = "0000101101") THEN sumndqdec <= "01000101"; ELSIF (sumndqbin = "0000110010") THEN sumndqdec <= "01010000"; ELSIF (sumndqbin = "0000110111") THEN sumndqdec <= "01010101"; ELSIF (sumndqbin = "0000111100") THEN sumndqdec <= "01100000"; ELSIF (sumndqbin = "0001000001") THEN sumndqdec <= "01100101"; ELSIF (sumndqbin = "0001000110") THEN sumndqdec <= "01110000"; ELSIF (sumndqbin = "0001001011") THEN sumndqdec <= "01110101"; ELSIF (sumndqbin = "0001010000") THEN sumndqdec <= "10000000"; ELSIF (sumndqbin = "0001010101") THEN sumndqdec <= "10000101"; ELSIF (sumndqbin = "0001011010") THEN
Page 1 of 2 Revision: vending

Date: April 22, 2008

sumndqdisp.vhd

Project: vending

52 53 54 55 56 57 58 59 60 61

sumndqdec <= ELSIF (sumndqbin sumndqdec <= ELSE sumndqdec <= END IF; sumndq_tendig <= sumndq_onedig <= END PROCESS; END behavior;

"10010000"; = "0001011111") THEN "10010101"; "00000000"; sumndqdec(7 DOWNTO 4); sumndqdec(3 DOWNTO 0);

Page 2 of 2

Revision: vending

Date: April 22, 2008

totrevconv.vhd

Project: vending

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

LIBRARY ieee; USE ieee.std_logic_1164.all; USE ieee.std_logic_unsigned.all; ENTITY totrevconv IS PORT ( revbin : IN STD_LOGIC_VECTOR(9 DOWNTO 0); revdec: OUT STD_LOGIC_VECTOR(11 DOWNTO 0)); END totrevconv; ARCHITECTURE behavior OF totrevconv IS BEGIN PROCESS(revbin) BEGIN IF (revbin = "0000000000") THEN revdec <= "000000000000"; ELSIF (revbin = "0000000101") THEN revdec <= "000000000101"; ELSIF (revbin = "0000001010") THEN revdec <= "000000010000"; ELSIF (revbin = "0000001111") THEN revdec <= "000000010101"; ELSIF (revbin = "0000010100") THEN revdec <= "000000100000"; ELSIF (revbin = "0000011001") THEN revdec <= "000000100101"; ELSIF (revbin = "0000011110") THEN revdec <= "000000110000"; ELSIF (revbin = "0000100011") THEN revdec <= "000000110101"; ELSIF (revbin = "0000101000") THEN revdec <= "000001000000"; ELSIF (revbin = "0000101101") THEN revdec <= "000001000101"; ELSIF (revbin = "0000110010") THEN revdec <= "000001010000"; ELSIF (revbin = "0000110111") THEN revdec <= "000001010101"; ELSIF (revbin = "0000111100") THEN revdec <= "000001100000"; ELSIF (revbin = "0001000001") THEN revdec <= "000001100101"; ELSIF (revbin = "0001000110") THEN revdec <= "000001110000"; ELSIF (revbin = "0001001011") THEN revdec <= "000001110101"; ELSIF (revbin = "0001010000") THEN revdec <= "000010000000"; ELSIF (revbin = "0001010101") THEN revdec <= "000010000101"; ELSIF (revbin = "0001011010") THEN revdec <= "000010010000";
Page 1 of 3 Revision: vending

Date: April 22, 2008

totrevconv.vhd

Project: vending

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

ELSIF (revbin revdec <= -------ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <=

= "0001011111") THEN "000010010101"; = "0001100100") "000100000000"; = "0001101001") "000100000101"; = "0001101110") "000100010000"; = "0001110011") "000100010101"; = "0001111000") "000100100000"; = "0001111101") "000100100101"; = "0010000010") "000100110000"; = "0010000111") "000100110101"; = "0010001100") "000101000000"; = "0010010001") "000101000101"; = "0010010110") "000101010000"; = "0010011011") "000101010101"; = "0010100000") "000101100000"; = "0010100101") "000101100101"; = "0010101010") "000101110000"; = "0010101111") "000101110101"; = "0010110100") "000110000000"; = "0010111001") "000110000101"; = "0010111110") "000110010000"; = "0011000011") "000110010101"; = "0011001000") "001000000000"; = "0011001101") "001000000101"; = "0011010010") "001000010000"; = "0011010111") "001000010101";
Page 2 of 3

THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN THEN

Revision: vending

Date: April 22, 2008

totrevconv.vhd

Project: vending

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSIF (revbin revdec <= ELSE revdec <= END IF; END PROCESS; END behavior;

= "0011011100") "001000100000"; = "0011100001") "001000100101"; = "0011100110") "001000110000"; = "0011101011") "001000110101"; = "0011110000") "001001000000"; = "0011110101") "001001000101"; = "0011111010") "001001010000"; = "0011111111") "001001010101"; "000000000000";

THEN THEN THEN THEN THEN THEN THEN THEN

Page 3 of 3

Revision: vending

S-ar putea să vă placă și