技术交流 · 2011年 2月 24日 4

【FPGA】篮球计分计时系统

本设计是基于FPGA的篮球计时计分器,利用7段共阴LED作为显示器件。在此设计中共接入了1个四位一体7段共阴LED显示器,2个三位一体7段共阴LED显示器,前者用来记录赛程时间,其中2位用于显示分钟,2位用于显示秒钟,后者用于记录甲乙队的分数,每队3个LED显示器显示范围可达到0~999分。赛程计时采用倒计时方式,比赛开始时启动计时,直至计时到零为止。

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
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
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
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
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
341
342
343
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
 
entity basket is
    port(   clk     :in  std_logic;
            key     :in  std_logic_vector(7 downto 0);
            duan    :out std_logic_vector(6 downto 0);
            wei     :out std_logic_vector(9 downto 0)
        );
end entity;
 
architecture behavior of basket is
 
    signal clk_5k      :std_logic;
    signal firb        :integer range 0 to 9:=0;
    signal firs        :integer range 0 to 9:=0;
    signal firg        :integer range 0 to 9:=0;
    signal secb        :integer range 0 to 9:=0;
    signal secs        :integer range 0 to 9:=0;
    signal secg        :integer range 0 to 9:=0;
    signal fens        :integer range 0 to 9:=9;
    signal feng        :integer range 0 to 9:=9;
    signal mios        :integer range 0 to 9:=5;
    signal miog        :integer range 0 to 9:=9;
    signal we          :integer range 0 to 9:=0;
    signal state       :integer range 0 to 2:=0;
    signal run         :std_logic;
 
    begin
 
    process(clk)
        variable cnt1    :integer range 0 to 100 :=0;
        variable cnt2    :integer range 0 to 100 :=0;
        begin
            if clk'event and clk='1' then
                if cnt1=100 then
                    cnt1:=0;
                    if cnt2=100 then
                        cnt2:=0;
                        clk_5k<=not clk_5k;
                    else
                        cnt2:=cnt2+1;
                    end if;
                else
                    cnt1:=cnt1+1;
                end if;
            end if;
    end process;
 
   process(clk_5k,we,firb,firs,firg,secb,secs,
            secg,fens,feng,mios,miog)
        function dat_to_seg(datout:integer range 0 to 9)
            return std_logic_vector is
            variable seg :std_logic_vector(6 downto 0);
            begin
                case datout is
                    when 0=>
                        seg:="0111111";
                    when 1=>
                        seg:="0000110";
                    when 2=>
                        seg:="1011011";
                    when 3=>
                        seg:="1001111";
                    when 4=>
                        seg:="1100110";
                    when 5=>
                        seg:="1101101";
                    when 6=>
                        seg:="1111101";
                    when 7=>
                        seg:="0000111";
                    when 8=>
                        seg:="1111111";
                    when 9=>
                        seg:="1101111";
                end case;
                return seg;
            end dat_to_seg;
        begin
            if clk_5k'event and clk_5k='1' then
                if we=9 then
                    we<=0;
                else
                    we<=we+1;
                end if;
            end if;
            case we is
                when 0=>
                    wei<="0111111111";
                    duan<=dat_to_seg(firb);
                when 1=>
                    wei<="1011111111";
                    duan<=dat_to_seg(firs);
                when 2=>
                    wei<="1101111111";
                    duan<=dat_to_seg(firg);
                when 3=>
                    wei<="1110111111";
                    duan<=dat_to_seg(secb);
                when 4=>
                    wei<="1111011111";
                    duan<=dat_to_seg(secs);
                when 5=>
                    wei<="1111101111";
                    duan<=dat_to_seg(secg);
                when 6=>
                    wei<="1111110111";
                    duan<=dat_to_seg(fens);
                when 7=>
                    wei<="1111111011";
                    duan<=dat_to_seg(feng);
                when 8=>
                    wei<="1111111101";
                    duan<=dat_to_seg(mios);
                when 9=>
                    wei<="1111111110";
                    duan<=dat_to_seg(miog);
            end case;
    end process;
 
    process(clk_5k)
    variable cnt3 :integer range 0 to 2999;
    variable cnt4 :integer range 0 to 500;
        begin
            if clk_5k'event and clk_5k='1' then
                if run='1' and state=0 then
                    if miog=0 and mios=0 
                       and fens=0 and feng=0 then
                        run<='0';
                    end if;
                    if cnt3=2999 then
                        cnt3:=0;
                        if miog=0 then
                            if mios=0 then
                                if feng=0 then
                                    if fens=0 then
                                        fens<=9;
                                    else
                                        fens<=fens-1;
                                    end if;
                                    feng<=9;
                                else
                                    feng<=feng-1;
                                end if;
                                mios<=5;
                            else
                                mios<=mios-1;
                            end if;
                            miog<=9;
                        else
                            miog<=miog-1;
                        end if;
                    else
                        cnt3:=cnt3+1;
                    end if;
                end if;
                case key is
                    when "01111111" =>
                        if cnt4=500 then
                            cnt4:=0;
                            if firg=9 then
                                firg<=0;
                                if firs=9 then
                                    firs<=0;
                                    if firb=9 then
                                        firb<=0;
                                    else
                                        firb<=firb+1;
                                    end if;
                                else
                                    firs<=firs+1;
                                end if;
                            else
                                firg<=firg+1;
                            end if;
                        else
                            cnt4:=cnt4+1;
                        end if;
                    when "10111111"=>
                        if cnt4=500 then
                            cnt4:=0;
                            if firg=0 then
                                firg<=9;
                                if firs=0 then
                                    firs<=9;
                                    if firb=0 then
                                        firb<=9;
                                    else
                                        firb<=firb-1;
                                    end if;
                                else
                                    firs<=firs-1;
                                end if;
                            else
                                firg<=firg-1;
                            end if;
                        else
                            cnt4:=cnt4+1;
                        end if;
                    when "11011111"=>
                        if cnt4=500 then
                            cnt4:=0;
                            if secg=9 then
                                secg<=0;
                                if secs=9 then
                                    secs<=0;
                                    if secb=9 then
                                        secb<=0;
                                    else
                                        secb<=firb+1;
                                    end if;
                                else
                                    secs<=secs+1;
                                end if;
                            else
                                secg<=secg+1;
                            end if;
                        else
                            cnt4:=cnt4+1;
                        end if;
                    when "11101111"=>
                        if cnt4=500 then
                            cnt4:=0;
                            if secg=0 then
                                secg<=9;
                                if secs=0 then
                                    secs<=9;
                                    if secb=0 then
                                        secb<=9;
                                    else
                                        secb<=secb-1;
                                    end if;
                                else
                                    secs<=secs-1;
                                end if;
                            else
                                secg<=secg-1;
                            end if;
                        else
                            cnt4:=cnt4+1;
                        end if;
                    when "11110111"=>
                        if cnt4=500 then
                            cnt4:=0;
                            if state=2 then
                                state<=0;
                            else
                                state<=state+1;
                                run<='0';
                            end if;
                        else
                            cnt4:=cnt4+1;
                        end if;
                    when "11111011"=>
                        if cnt4=500 then
                            cnt4:=0;
                            case state is
                                when 0 =>
                                    NULL;
                                when 1 =>
                                    if run='0' then
                                        if feng=9 then
                                            feng<=0;
                                            if fens=9 then
                                                fens<=0;
                                            else
                                                fens<=fens+1;
                                            end if;
                                        else
                                            feng<=feng+1;
                                        end if;
                                    end if;
                                when 2 =>
                                    if run='0' then
                                        if miog=9 then
                                            miog<=0;
                                            if mios=5 then
                                                mios<=0;
                                            else
                                                mios<=mios+1;
                                            end if;
                                        else
                                            miog<=miog+1;
                                        end if;
                                    end if;
                            end case;
                        else
                            cnt4:=cnt4+1;
                        end if;
                    when "11111101"=>
                        if cnt4=500 then
                            cnt4:=0;
                            case state is
                                when 0 =>
                                    NULL;
                                when 1 =>
                                    if run='0' then
                                        if feng=0 then
                                            feng<=9;
                                            if fens=0 then
                                                fens<=9;
                                            else
                                                fens<=fens-1;
                                            end if;
                                        else
                                            feng<=feng-1;
                                        end if;
                                    end if;
                                when 2 =>
                                    if run='0' then
                                        if miog=0 then
                                            miog<=9;
                                            if mios=0 then
                                                mios<=5;
                                            else
                                                mios<=mios-1;
                                            end if;
                                        else
                                            miog<=miog-1;
                                        end if;
                                    end if;
                            end case;
                        else
                            cnt4:=cnt4+1;
                        end if;
                    when "11111110"=>
                        if cnt4=500 then
                            cnt4:=0;
                            run<=not run;
                            state<=0;
                        else
                            cnt4:=cnt4+1;
                        end if;
                    when others =>
                        NULL;
                end case;
            end if;
    end process;
 
end behavior;