Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Application Note AN2404

PSoC® Implementation of a
Newspaper Vending Machine Controller

Authors: Anant Aggarwal, Neha Joshi, Sachin Keswani, Shruti Richa


Associated Project: No
Associated Part Family: CY8C29466
Software Version: PSoC Designer™ 4.2 or PSoC Express™ 2.0

Abstract
Implementation of a Newspaper Vending Machine Controller, written in PSoC assembly language, is presented. The state of
the interface signals are indicated by LEDs and displayed on an LCD screen... Future design enhancements and potential
cost reductions are also discussed.

Introduction Solution Proposal


A newspaper vending machine is programmed by routines The challenge was to design a newspaper vending
written in PSoC assembly language. It is assumed that a machine controller given the input in terms of nickels,
newspaper costs $0.35 and that the vending machine dimes and/or quarters. Access to the newspaper is given
accepts nickels, dimes, and quarters that are deposited in to the customer along with the change. If the vending
the vending machine, one at a time. The vending machine does not have the requisite number of coins
machine’s coin sorter logic determines whether a nickel, a required for providing the correct change, the entire
dime, or a quarter has been deposited. If exact change amount deposited is refunded to the customer. A finite
has been entered then the unlatch, output signal is state machine (FSM) was used to test all possible input
generated, so that the customer can get the newspaper. If cases. The entire design was implemented and tested
the amount of deposited money exceeds the price of a using Cypress’ CY8C29466. An FSM state table was used
newspaper, then change is given. If the machine has to define all possible states.
insufficient change, the customer’s coins are refunded by
issuing the Refund output signal, and the Unlatch signal is Algorithm
not generated. Note that the money deposited by the
1. Switch inputs are supplied to the pins of the
customer is kept separate from the change repository. If
PSoC.
there is enough change in the repository then it is to be
2. Two (2) PSoC pins are used to determine
paid using the minimum amount of coins. The repository
whether a nickel, dime or quarter has been
informs the controller of the number of coins of each type
inserted, e.g., a 00 specifies don’t care, 01
available in the repository: Bits NR represent the number
specifies a nickel, 10 specifies a dime and 11
of nickels, and bits DR represent the number of dimes.
specifies a quarter.
Change is given to the customer by generating pulse
a) The output of switch SW was connected to
signals for the repository: The NR signal releases one
pins P1[0] and P0[0].
nickel, and the DR signal releases one dime. The refund
b) The first input in LSB of [PRT0DR] as the
amount is displayed on the LCD screen.
first bit.
c) The second input in LSB of [PRT1DR] as
the second bit.
d) Save the result of [PRT1DR] in [register2]
and result of [PRT0DR] in [register1].
e) Left shift [register2] by 1.
f) Add the result of [register2] to [register1] to
give the input.

12/19/2006 Rev. ** -1-


AN2404

3. Calculate the next state and the amount of Interfacing Signals


change to be returned to the user. Input Interface:
a) The current state is taken as ‘000’.
b) Based on the inputs, the index to the state SW input switch >> P0[0], 0th bit (LSB)
table is calculated (index = current state*4 SW input switch >> P1[0], 1st bit
+input).
c) The ‘Next State’ and ‘Change’ are
determined by indexing the ‘Next State Table 1. Input Specifics
Look-up Table’ and ‘Change Look-up
Table’, respectively. Value Input1 Input0 Wired Connections
d) ‘Next State’ determines the total amount of Don’t Care 0 0 Switch not to be pressed.
change inserted into the machine. Nickel Disconnect P0[0] from
e) Check if the total amount is equal to or 0 1 SW. Connect P1[0] to SW.
greater than $0.35 after every coin input. Press the switch SW
f) If 3(e) is FALSE, then the ‘Current State’ is Dime Disconnect P1[0] from
equated to ‘Next State’. Take further input 1 0 SW. Connect P[0]0 to SW.
and go to step 3(b). Press the switch SW
g) If 3(e) is TRUE, then proceed to ‘Change Quarter Connect both P1[0] and
Optimization’. Go to 4. 1 1 P[0]0 to SW. Press the
switch SW
4. The maximum change refunded to the user can
be $0.20, considering the worst case scenario Output Interface:
when the user enters up to $0.30 and then a
quarter i.e., $0.55 total. The change can be LCD: For displaying the number of coins of each
$0.00, $0.05, $0.10, $0.15 or $0.20 cents. denomination and the result of transaction.
a) Based on the change, calculate the least LEDs: For displaying when the signals for ‘Unlatch’,
number of dimes and nickels. ‘Release’ or ‘Refund’ are HIGH.
b) Check whether the required number of
nickels and dimes is present in the Unlatch P2[0] >> LED4
repository. Release P2[1] >> LED3
c) If 4(b) is TRUE, calculate the remaining Refund P2 [2] >> LED2
number of dimes and nickels in the
repository. Go to step 5. For example, say the Input1 is 11 and then Input0 is 11,
d) If 4(b) is FALSE, then calculate the next indicating that two (2) quarters were deposited. Change
optimized number of dimes and nickels. ($0.15) is optimized, i.e., one (1) dime and one (1) nickel.
Go to 4(b). The output is shown on the LCD with change. Two signals
e) If none of the optimized states are are HIGH: ‘Unlatch’ signal (LED4) and ‘Release’ signal
available in the repository, go to step 8. (LED3), these LEDs glow. The ‘Refund’ signal (LED2) is
LOW, i.e., LED2 doesn’t glow.
5. The change in terms of nickels and dimes is
returned to the user.
6. The newspaper is released. The ‘Unlatch' signal
goes HIGH, which turns LED4 “ON.”
7. The amount entered is moved to the repository.
The ‘Release’ signal goes HIGH and turns LED3
“ON.”
8. The money deposited is refunded since the
machine does not have sufficient change. The
‘Refund’ signal goes HIGH and turns LED2 “ON.”
9. The vending machine is ready for the next
transaction. Go to Step 1.

12/19/2006 Rev. ** -2-


AN2404

State Table Number of Clock Cycles Per Routine:


The state table used for implementing the finite state
machine is as shown: Transaction: 48
FSM: 117
Table 2.
Value State Input Next State Change
Loop: 74
S2 S1 S0 I1 I0 N2 N1 N0 C2 C1 C0 Ahead: 23
$0.00 0 0 0 0 0 0 0 0 0 0 0 Change5: 50
0 1 0 0 1 0 0 0
Change10: 42
1 0 0 1 0 0 0 0
Change15: 85
1 1 1 0 1 0 0 0
Change20: 43
$0.05 0 0 1 0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 0
Unlatch: 252
1 0 0 0 1 0 0 0
Refund Glow: 9
1 1 1 1 0 0 0 0
$0.10 0 1 0 0 0 0 0 0 0 0 0 Other Clock Cycles:
0 1 0 0 1 0 0 0
1 0 1 1 0 0 0 0 Label1: 13
1 1 1 1 1 0 0 0 Label2: 34
$0.15 0 1 1 0 0 0 0 1 0 0 0 Label3: 13
0 1 1 1 0 0 0 0
Label14: 38
1 0 1 1 1 0 0 0
Label15: 13
1 1 1 1 1 0 0 1
Label16: 59
$0.20 1 0 0 0 0 1 1 0 0 0 0
0 1 1 1 1 0 0 0
Label17: 38
1 0 1 1 0 0 0 0
Label18: 13
1 1 1 1 1 0 1 0 abc: 15
$0.25 1 0 1 0 0 1 1 1 0 0 0
0 1 1 1 0 0 0 0 Total Main Routine Instructions = 1055
1 0 1 1 1 0 0 0

$0.30 1 1 0
1
0
1
0
1
1
1
1
1
0
0
0
1
0
1
0
Testing and Debugging
0 1 1 1 1 0 0 0
Procedure:
1 0 1 1 1 0 0 1
An LCD and LEDs were used to debug the program.
1 1 1 1 1 1 0 0 Individual register values were displayed on the LCD.
$0.35 1 1 1 0 0 X X X 0 0 0 Partial code was run to display intermediate values.
0 1 X X X 0 0 1 Boolean values were displayed as “ON” or “OFF” states of
1 0 X X X 0 1 0 LEDs.
1 1 X X X 1 0 1

The inputs were based on the implemented functionality.


Following are some of the possible test cases:
Routines
Transaction: Implements initial conditions for every Case 1:
transaction. 1 Dime, 1 Dime, 1 Dime, 1 Dime (10 10 10 10)
State 0 >> State 2 >> State 4 >> State 6 >> State 7
FSM: Implements all possible input states. Change = $0.05, Output = 1 Nickel

Change5, Change10, Change15, and Change20: Case 2:


Implement the change to be given back to the user 1 Quarter, 1 Quarter (11 11)
depending on whether the user is to be given a change of State 0 >> State 4 >> State 7
$0.05, $0.10, $0.15 or $0.20 (maximum worst case occurs Change = $0.15, Output =1 Dime, 1 Nickel
when user enters $0.55 cents).
Case 3:
Unlatch: Displays change in terms of dimes and nickels. 6 Nickels, 1 Dime (01 01 01 01 01 01 10)
State 0 >> State 1 >> State 2 >> State 3 >> State 4 >>
Loop: For press and release of input switch. State 5 >> State 6 >> State 7
Change = $0.05, Output = 1 Nickel

Case 4:
3 Dimes, 1 Quarter (10 10 10 11)
State 0 >> State 2 >> State 4 >> State 6 >> State 7
Change = $0.20, Output = 2 Dimes

12/19/2006 Rev. ** -3-


AN2404

Figure 1. Snapshot Future Scope to Improve Design and


Reduce Cost

1. A CapSense (capacitor sensitive) switch, which is


more efficient and less prone to error, can be
used in place of a mechanical switch.
2. A hardware multiplexer could be used to accept
the three inputs for nickel, dime and quarter.
3. Use of a “greedy” algorithm for change-return
optimization.
4. Implement low power mode. The module can be
equipped with hardwired controls and/or software
capable of automatically placing the machine into
a low power mode during periods of extended
inactivity while still connected to its power source
to facilitate the saving of additional energy, where
appropriate.
Waveforms
Bibliography and Web Site References
Case 1:
1 Dime, 1 Dime, 1 Dime, 1 Dime (10 10 10 10) 1. PSoC Designer(TM) Assembly Language User
Unlatch = 1, Release = 1, Change = $0.05 (NR=1, DR=0) Guide http://www.cypress.com/design/MR10220
2. PSoC(R) Technical Reference Manual (TRM)
http://www.cypress.com/design/TR10002
3. Implementation Reference
http://www.cypress.com
4. Some Advice on Writing a Technical Report:
http://www.csee.umbc.edu/%7Esherman/Course
s/documents/TR_how_to.html

Acknowledgement
Case 2: This Application Note is based on work required to meet certain
1 Quarter, 1 Quarter (11 11) requirements of a graduate level course offered at Stony Brooks
Department of Electrical and Computer Engineering by Dr. Alex
Unlatch = 1, Release = 1, Change = $0.15 (NR=1, DR=1)
Doboli, Associate, Professor.

About the Authors


Name: Anant Aggarwal
Title: Graduate Student
Background: Electrical Engineering
Stony Brook University
Contact: Anant.aggarwal@stonybrook.edu

Name: Neha Joshi


Title: Graduate Student
Background: Electrical Engineering
Stony Brook University
Contact: Neha.joshi@stonybrook.edu

Name: Sachin Keswani


Title: Graduate Student
Background: Electrical Engineering
Stony Brook University
Contact: Sachin.keswani@stonybrook.edu

Name: Shruti Richa


Title: Graduate Student
Background: Electrical Engineering
Stony Brook University
Contact: Shruti.richa@stonybrook.edu

12/19/2006 Rev. ** -4-


AN2404

Cypress Semiconductor
198 Champion Court
San Jose, CA 95134-1709
Phone: 408-943-2600
Fax: 408-943-4730
http://www.cypress.com

© Cypress Semiconductor Corporation, 2006. The information contained herein is subject to change without notice. Cypress Semiconductor Corporation assumes no responsibility for the
use of any circuitry other than circuitry embodied in a Cypress product. Nor does it convey or imply any license under patent or other rights. Cypress products are not warranted nor intended
to be used for medical, life support, life saving, critical control or safety applications, unless pursuant to an express written agreement with Cypress. Furthermore, Cypress does not authorize
its products for use as critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of
Cypress products in life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges.

12/19/2006 Rev. ** -5-

You might also like