OpenCores
URL https://opencores.org/ocsvn/mcu8/mcu8/trunk

Subversion Repositories mcu8

[/] [mcu8/] [trunk/] [src/] [EXAMPLES/] [candy_machine.vhd] - Blame information for rev 24

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dimo
library ieee;
2
use ieee.std_logic_1164.all;
3
use ieee.numeric_std.all;
4
use work.asci_types.all;
5
 
6
entity candy_machine is
7
  generic( one_sec_factor : INTEGER := 1e5/2-1;  -- 1e8/2-1 for 1s; change to 5
8
-- for synthesis (1 ms period)
9
           ok_factor : INTEGER := 6000;  --change to 5000 for synthesis
10
           period_factor : INTEGER := 300;  --50ms
11
           problem_factor : INTEGER := 1000);
12
  port( clk : in std_logic;
13
-- j_left=5cent, j_up=10cent, j_right=20cent, j_down=rst
14
        j_down, j_up, j_left, j_right : IN std_logic;
15
--        money_rest : out STD_LOGIC_VECTOR (2 DOWNTO 0);
16
-- if '1' the machine gives all the money in the temp. save out
17
--        money_error : OUT std_vector;
18
-- if '1' gives a candy out
19
--        candy : out std_logic;
20
-- "0001"=candy, "0010"=5cent, "0100"=10cent, "1000"=20cent, "1110"=error
21
        led : OUT STD_LOGIC_VECTOR (3 DOWNTO 0);
22
        lcd_print  : OUT lcd_matrix );
23
end candy_machine;
24
 
25
 
26
architecture behavioral of candy_machine is
27
  TYPE state IS (s0, s5, s10, s15, s20, s25, s30, s35, s40, problem, wait_before_s5,
28
                 wait_before_s10, wait_before_s15, wait_before_s20);
29
  signal pr_state, nxt_state : state;
30
  signal money : STD_LOGIC_VECTOR (2 DOWNTO 0);  -- "100"=5cent "010"=10cent "001"=20cent
31
  signal one_sec, rst_int : STD_LOGIC := '0';
32
  signal counter : INTEGER RANGE 0 TO ok_factor;
33
  SIGNAL rst : STD_LOGIC := '1';
34
begin
35
 
36
 
37
time_p: process(clk)
38
  variable temp0 : integer RANGE 0 TO ok_factor;
39
  VARIABLE flag : STD_LOGIC := '0';
40
BEGIN
41
    IF clk'EVENT AND clk='1' THEN
42
      IF rst_int='0' THEN
43
        temp0 := 0;
44
        flag := '1';                    -- because of the one_sec_p process
45
      else
46
        IF one_sec='0' THEN
47
          flag := '0';
48
--this part is executed only on a
49
--positive transition of the one_sec signal, the counter factors multiply the
50
--period of the one_sec signal. If you need to speed up the execution change
51
--the on_sec_factor to a lower VALUE
52
        elsif flag='0' THEN
53
          flag := '1';
54
          IF
55
            temp0>=ok_factor THEN
56
            temp0 := 0;
57
          ELSE
58
            temp0 := temp0 + 1;
59
          end if;
60
        END if;
61
      END if;
62
    END if;
63
    counter <= temp0;
64
END process;
65
 
66
--------------------------------------------------------------------------------------
67
-- generates the one_sec signal. Period of the signal is 1s if one_sec_factor=1e8/2-1
68
-- for 100MHz oszillator
69
--------------------------------------------------------------------------------------
70
one_sec_p: process(clk)
71
  VARIABLE temp : integer RANGE 0 TO one_sec_factor;
72
  begin
73
    IF clk'event AND clk='1' THEN
74
      IF rst_int='0' THEN
75
        temp := 0;
76
        one_sec <= '1';                 -- take a look at lcd1.vhd to see why
77
      else
78
        iF temp>=one_sec_factor THEN
79
          temp := 0;
80
          one_sec <= NOT one_sec;
81
        else
82
          temp := temp + 1;
83
        END if;
84
      END if;
85
    END IF;
86
  END process;
87
 
88
-------------------------------------------------------------------------------
89
-- LCD matrix
90
-------------------------------------------------------------------------------
91
lcd_m: process(clk)
92
  BEGIN
93
    IF clk'EVENT AND clk='1' THEN
94
      CASE pr_state IS
95
        WHEN s0 =>
96
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
97
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
98
                      ' ','P','l','e','a','s','e',' ','i','n','s','e','r','t',':',' ','2','5','c',' ',
99
                      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' );
100
        WHEN s5 | wait_before_s5 =>
101
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
102
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
103
                      ' ','P','l','e','a','s','e',' ','i','n','s','e','r','t',':',' ','2','0','c',' ',
104
                      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' );
105
        WHEN s10 | wait_before_s10 =>
106
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
107
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
108
                      ' ','P','l','e','a','s','e',' ','i','n','s','e','r','t',':',' ','1','5','c',' ',
109
                      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' );
110
        WHEN s15 | wait_before_s15 =>
111
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
112
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
113
                      ' ','P','l','e','a','s','e',' ','i','n','s','e','r','t',':',' ','1','0','c',' ',
114
                      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' );
115
        WHEN s20 | wait_before_s20 =>
116
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
117
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
118
                      ' ','P','l','e','a','s','e',' ','i','n','s','e','r','t',':',' ',' ','5','c',' ',
119
                      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' );
120
        WHEN s25 =>
121
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
122
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
123
                      'Y','o','u','r',' ','c','o','f','f','e',' ','i','s',' ','r','e','a','d','y','!',
124
                      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' );
125
        WHEN s30 =>
126
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
127
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
128
                      'Y','o','u','r',' ','c','o','f','f','e',' ','i','s',' ','r','e','a','d','y','!',
129
                      'Y','o','u',' ','h','a','v','e',':',' ','5','c',' ',' ','c','h','a','n','g','e' );
130
        WHEN s35 =>
131
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
132
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
133
                      'Y','o','u','r',' ','c','o','f','f','e',' ','i','s',' ','r','e','a','d','y','!',
134
                      'Y','o','u',' ','h','a','v','e',':',' ','1','0','c',' ','c','h','a','n','g','e' );
135
        WHEN s40 =>
136
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
137
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
138
                      'Y','o','u','r',' ','c','o','f','f','e',' ','i','s',' ','r','e','a','d','y','!',
139
                      'Y','o','u',' ','h','a','v','e',':',' ','1','5','c',' ','c','h','a','n','g','e' );
140
        WHEN OTHERS =>
141
          lcd_print <=  ( '-','-','-','C','o','f','f','e','e',' ','A','u','t','o','m','a','t','-','-','-',
142
                      ' ',' ',' ',' ',' ',' ',' ',' ','-','-','-','-',' ',' ',' ',' ',' ',' ',' ',' ',
143
                      ' ',' ',' ',' ',' ',' ',' ','E','r','r','o','r','!',' ',' ',' ',' ',' ',' ',' ',
144
                      ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' );
145
      END CASE;
146
    END IF;
147
  END process;
148
 
149
---------------------------------------------------------------------------------
150
-- this is a MORE automat, in order to save some registers, you can use MAELY too
151
---------------------------------------------------------------------------------
152
main_s_p: process(clk)
153
  begin
154
    if clk'event and clk='1' then
155
      IF rst='0' THEN
156
        pr_state <= s0;
157
      else
158
        pr_state <= nxt_state;
159
      end if;
160
    END if;
161
  end process;
162
 
163
 
164
main_c_p: process(pr_state,money,counter)
165
begin
166
  case pr_state is
167
      WHEN s0 =>
168
        CASE money IS
169
          WHEN "000" => nxt_state <= s0;
170
          WHEN "100" => nxt_state <= wait_before_s5;
171
          WHEN "010" => nxt_state <= wait_before_s10;
172
          WHEN "001" => nxt_state <= wait_before_s20;
173
          WHEN OTHERS => nxt_state <= problem;
174
        END CASE;
175
        rst_int <= '0';
176
        led <= "0000";
177
      WHEN s5 =>
178
        CASE money IS
179
          WHEN "000" => nxt_state <= s5;
180
          WHEN "100" => nxt_state <= wait_before_s10;
181
          WHEN "010" => nxt_state <= wait_before_s15;
182
          WHEN "001" => nxt_state <= s25;
183
          WHEN OTHERS => nxt_state <= problem;
184
        END CASE;
185
        rst_int <= '0';
186
        led <= "1000";
187
      WHEN s10 =>
188
        CASE money IS
189
          WHEN "000" => nxt_state <= s10;
190
          WHEN "100" => nxt_state <= wait_before_s15;
191
          WHEN "010" => nxt_state <= wait_before_s20;
192
          WHEN "001" => nxt_state <= s30;
193
          WHEN OTHERS => nxt_state <= problem;
194
        END CASE;
195
        rst_int <= '0';
196
        led <= "0100";
197
      WHEN s15 =>
198
        CASE money IS
199
          WHEN "000" => nxt_state <= s15;
200
          WHEN "100" => nxt_state <= wait_before_s20;
201
          WHEN "010" => nxt_state <= s25;
202
          WHEN "001" => nxt_state <= s35;
203
          WHEN OTHERS => nxt_state <= problem;
204
        END CASE;
205
        rst_int <= '0';
206
        led <= "1100";
207
      WHEN s20 =>
208
        CASE money IS
209
          WHEN "000" => nxt_state <= s20;
210
          WHEN "100" => nxt_state <= s25;
211
          WHEN "010" => nxt_state <= s30;
212
          WHEN "001" => nxt_state <= s40;
213
          WHEN OTHERS => nxt_state <= problem;
214
        END CASE;
215
        led <= "0010";
216
        rst_int <= '0';
217
      WHEN s25 =>
218
        IF counter>=ok_factor THEN
219
          nxt_state <= s0;
220
        ELSE
221
          nxt_state <= s25;
222
        END IF;
223
        led <= "0001";
224
        rst_int <= '1';
225
      WHEN s30 =>
226
        IF counter>=ok_factor THEN
227
          nxt_state <= s0;
228
        ELSE
229
          nxt_state <= s30;
230
        END IF;
231
        led <= "1001";
232
        rst_int <= '1';
233
      WHEN s35 =>
234
        IF counter>=ok_factor THEN
235
          nxt_state <= s0;
236
        ELSE
237
          nxt_state <= s35;
238
        END IF;
239
        led <= "0101";
240
        rst_int <= '1';
241
      WHEN s40 =>
242
        IF counter>=ok_factor THEN
243
          nxt_state <= s0;
244
        ELSE
245
          nxt_state <= s40;
246
        END IF;
247
        led <= "1101";
248
        rst_int <= '1';
249
--      WHEN rst_before_s5 =>
250
--        nxt_state <= wait_before_s5;
251
--        led <= "0000";
252
--        rst_int <= '0';
253
--      WHEN rst_before_s10 =>
254
--        nxt_state <= wait_before_s10;
255
--        led <= "0000";
256
--        rst_int <= '0';
257
--      WHEN rst_before_s15 =>
258
--        nxt_state <= wait_before_s15;
259
--        led <= "0000";
260
--        rst_int <= '0';
261
--      WHEN rst_before_s20 =>
262
--        nxt_state <= wait_before_s20;
263
--        led <= "0000";
264
--        rst_int <= '0';
265
      WHEN wait_before_s5 =>
266
        IF counter>=period_factor THEN
267
          nxt_state <= s5;
268
        ELSE
269
          nxt_state <= wait_before_s5;
270
        END IF;
271
        led <= "1000";
272
        rst_int <= '1';
273
      WHEN wait_before_s10 =>
274
        IF counter>=period_factor THEN
275
          nxt_state <= s10;
276
        ELSE
277
          nxt_state <= wait_before_s10;
278
        END IF;
279
        led <= "0100";
280
        rst_int <= '1';
281
      WHEN wait_before_s15 =>
282
        IF counter>=period_factor THEN
283
          nxt_state <= s15;
284
        ELSE
285
          nxt_state <= wait_before_s15;
286
        END IF;
287
        led <= "1100";
288
        rst_int <= '1';
289
      WHEN wait_before_s20 =>
290
        IF counter>=period_factor THEN
291
          nxt_state <= s20;
292
        ELSE
293
          nxt_state <= wait_before_s20;
294
        END IF;
295
        led <= "0010";
296
        rst_int <= '1';
297
      WHEN problem =>
298
        IF counter>=problem_factor THEN
299
          nxt_state <= s0;
300
        ELSE
301
          nxt_state <= problem;
302
        END IF;
303
        led <= "1110";
304
        rst_int <= '1';
305
      WHEN OTHERS =>
306
        nxt_state <= problem;
307
        led <= "1110";
308
        rst_int <= '0';
309
    END case;
310
  END process;
311
 
312
  rst <= j_down;
313
  money(2) <= NOT j_left;
314
  money(1) <= NOT j_up;
315
  money(0) <= NOT j_right;
316
 
317
END behavioral;

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.