代码代写｜EGR 244 Lab 9 – Vending Machine
- Write Verilog code for sequential circuit/state machine
- Use the I/O pins on the Altera board to interface with a 7-segment display
- Use BCD to display a two digit decimal value
❼ Altera MAX II development board
❼ USB Blaster cable
❼ USB power cable
❼ PB505 Workstation
❼ Two 220 Ω resistors (Orange-Orange-Brown-Gold color bands)
❼ 7-segment display
For this lab, you’ll be writing Verilog code to imitate the functions of a (relatively) simple vending machine. You’ll be writing several modules to break this task up in to smaller parts,and its important to test each module individually before putting them all together. So, for example, even if you don’t end up using all the LEDs in your final code, you may want to assign them pins initially to help test each module.
To mimic the operation of a vending machine, you’ll have 3 main phases in your machine:
- Accepting money – You’ll use two buttons as stand ins for a user inserting a dime or a quarter in your machine, and display the amount of money inserted using the two 7-segment displays on the work station
- Dispensing item/not enough money message – The other two buttons will indicate when the user has selected an item to purchase from the machine
- Returning change – Then, if change is needed, you’ll display how much change is dispensed back to the user
To simplify the behavior of your vending machine, all state changes will be synchronous (i.e. occurring at the same clock signal), using the clock divider code you used in the last lab.
In this lab, unlike the state machines you’ve seen in the homework and previous labs, the raw number of states in your machine is actually in the hundreds, since the amount of money (among the other states) has to be included in your state, and you have 100 different values (00 through 99). This means that it is impossible (or at least very impractical) towrite a state transition diagram for each possible state. Instead, its more useful to think of the state of the machine to as broken down into “controlling states” and “data states”. The controlling states (like those above) make up a “controller” that determine the behavior of the state machine, while the data states make up the “datapath” which is essentially memory (e.g. how much money has been inserted) that will help the controller to decide what to do next.
The procedure that follows will describe the different modules that I believe will prove useful in implementing this vending machine, but you are free to implement the machine differently (there is always more than one way to code something), but I would encourage you to ask me for advice before any major departures from the modules I describe.
- The vending module – Your “main” module
❼ This module will have your usual input and output assignments to the I/O pins on the chip (buttons, LEDs, clock, and now expansion pins)
❼ It will also contain instantiations of your other modules that you’ll use: clock divider, bcd, money counter, etc
❼ The most amount of code here will be the always block that will act as the controller for your state transitions. Its sensitivity list should just be the clock edge. It will contain several if or case statements, that will look something like the following:
1 if ( state == accept && button_dime == 0) begin
2 state <= accept ;
3 money <= money +10;
Which would be for an if statement for when you’re in the “accepting money” state, and a dime is inserted, then stay in the accepting money state, and add 10 cents to the “money” register.
❼ Tip: To help with your logic/state transitions, I highly suggest explicitly coding what each state variable will transition to in every if/case statement, even if they don’t change. Like in the above example, I didn’t need to actually specify that the state variable should update to the “accept” state, because it was already in that state, but its a good habit to get into.
- A binary to BCD module – a two digit BCD conversion module for the output of your money state
❼ This will be a combinatorial logic module that takes in a binary value (from 1 to 100) and outputs two bcd digits (a tens digit and a ones digit)
❼ You’ll use this to display the money inserted, cost of the item selected, and change given (if needed)
❼ Assume for this machine, that you the person will never enter more than 99 cents in money, so you don’t need to worry about overflow.
- A state display module – this will take the input of the current state, and then output the seven bits to send to the seven segment display
❼ Another combinatorial circuit, that can be just one case statement (or a few if statements) that produce the segments for your seven segment display to display which state (accepting money, dispensing, change) you’re currently in.
- State behavior:
❼ Accepting Money state – While in the “accepting money state” you should be updating the money inserted display (the built in displays on the workstation) depending on whether the dime button (add 10) or quarter button (add 25) is pressed. On the seven segment display, you should display a capital A, to show that you’re in the accepting money display. If either item button is pressed, transition to the dispensing item state
❼ Dispensing Item State – In this state you should display a lower case d on the seven segment display. On the built in display, you should display the cost associated with the button pressed (20 or 50 cents). You should stay in this state for 5 seconds (i.e. 5 clock cycles if you’ve divided the clock down to a 1 second period).
At the end of the 5 seconds, transition back to accepting money if the cost was higher that what has been entered (i.e. the user would still need to insert more money), or transition to the returning change state.
❼ Returning change state – In this state you should display a captial C on the seven segment display. On the built in display, you should display the difference between the money inserted and the item that was selected (i.e. how much money would need to be returned). You should stay in this state again for 5 seconds, before transitioning back to the accepting money state, and setting the money inserted to 0 for the next user to start the process over again.