-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendingMachine.v
More file actions
160 lines (135 loc) · 4.6 KB
/
VendingMachine.v
File metadata and controls
160 lines (135 loc) · 4.6 KB
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
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module VendingMachine #(
parameter ClockFrequency = 50000000 // Clock Frequency
) (
input Clk,
input nRst,
input button1, //Buttons for input
input button2,
input switch, //Dud switch to act as coins being inputted
output reg led1, //LED simulates the output signal
output reg led2,
output [6:0] seg1, //Display out
output [6:0] seg2,
output [6:0] seg3,
output [6:0] seg4
);
// Parameters
parameter COST = 100; // Cost of supposed items
parameter CREDIT_INCREMENT = 5; // Dud value for coins, when a coin acceptor is attached, this pin will be treated as the coin acceptor
// Internal Registers
reg [31:0] Ticks_Internal; // Counter for timing
reg [15:0] credits; // Credit counter
reg [31:0] led1_timer; // Timer for LED1
reg [31:0] led2_timer; // Timer for LED2
// Outputs for 7-segment displays
wire [3:0] thousands, hundreds, tens, ones;
// Debounced button signals
wire debounced_button1, debounced_button2;
// Button press edge detection outputs
wire button1_pressed, button2_pressed;
// Declaring debounce modules
debounce #(
.ClockFrequency(ClockFrequency),
.DebounceTimeMs(20)
) debounce_button1 (
.Clk(Clk),
.nRst(nRst),
.ButtonIn(button1),
.ButtonOut(debounced_button1)
);
debounce #(
.ClockFrequency(ClockFrequency),
.DebounceTimeMs(20)
) debounce_button2 (
.Clk(Clk),
.nRst(nRst),
.ButtonIn(button2),
.ButtonOut(debounced_button2)
);
// Declaring edge detectors
edge_detector edge_detector_button1 (
.Clk(Clk),
.nRst(nRst),
.SignalIn(debounced_button1),
.PulseOut(button1_pressed)
);
edge_detector edge_detector_button2 (
.Clk(Clk),
.nRst(nRst),
.SignalIn(debounced_button2),
.PulseOut(button2_pressed)
);
// Declaring 7 seg displays
SevenSegDecoder seven_seg1 (
.digit(ones),
.segments(seg1)
);
SevenSegDecoder seven_seg2 (
.digit(tens),
.segments(seg2)
);
SevenSegDecoder seven_seg3 (
.digit(hundreds),
.segments(seg3)
);
SevenSegDecoder seven_seg4 (
.digit(thousands),
.segments(seg4)
);
// Break credits into BCD digits
assign thousands = (credits >= 1000) ? (credits / 1000) % 10 : 0;
assign hundreds = (credits >= 100) ? (credits / 100) % 10 : 0;
assign tens = (credits >= 10) ? (credits / 10) % 10 : 0;
assign ones = credits % 10;
// Main Process: Clock-driven Logic
always @(posedge Clk or negedge nRst) begin
if (!nRst) begin
// Reset logic
Ticks_Internal <= 0;
credits <= 16'd0;
led1 <= 0;
led2 <= 0;
led1_timer <= 0;
led2_timer <= 0;
end else begin
// Limit credits under 100 dollars
if (credits > 9999)
credits <= 9999;
// Timing Logic
if (Ticks_Internal == ClockFrequency - 1) begin
Ticks_Internal <= 0;
// Decrement timers if active
if (led1_timer > 0)
led1_timer <= led1_timer - 1;
if (led2_timer > 0)
led2_timer <= led2_timer - 1;
// Turn off LEDs when timers reach 0
if (led1_timer == 1)
led1 <= 0;
if (led2_timer == 1)
led2 <= 0;
// Add credits when switch is toggled
if (switch)
credits <= credits + CREDIT_INCREMENT;
end else begin
Ticks_Internal <= Ticks_Internal + 1;
end
// Button1 Logic: Control LED1 timer
if (button1_pressed) begin
if (credits >= COST) begin
credits <= credits - COST;
led1 <= 1;
led1_timer <= led1_timer + ClockFrequency; // Add 1 second per press
end
end
// Button2 Logic: Control LED2 timer
if (button2_pressed) begin
if (credits >= COST) begin
credits <= credits - COST;
led2 <= 1;
led2_timer <= led2_timer + ClockFrequency; // Add 1 second per press
end
end
end
end
endmodule