我用VHDL写了个乒乓球游戏机 也挺有意思的 搞了一下午了
时间:10-02
整理:3721RD
点击:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity ping_pang is
port(clk1khz:in std_logic;------1khz时钟信号
rst:in std_logic;----------系统复位
af,aj:in std_logic;--------A方发球,A方击球
bf,bj:in std_logic;--------B方发球,B方击球
shift:out std_logic_vector(15 downto 0);----16个led代表乒乓球台
scan:out std_logic_vector(3 downto 0);------数码管地址选择信号
seg7:out std_logic_vector(6 downto 0));-----7段显示控制信号(abcdefg)
end;
architecture a_one of ping_pang is
signal clk1_2hz:std_logic;
signal a_score,b_score:integer range 0 to 11;
signal cnt:integer range 0 to 3;
signal data:std_logic_vector(3 downto 0);
signal a_one,a_ten,b_one,b_ten:std_logic_vector(3 downto 0);
begin
-------------------------------------2Hz分频-----
process(clk1khz)
variable count:integer range 0 to 2;
begin
if clk1khz'event and clk1khz='1' then
if count=1 then clk1_2hz '0');
elsif clk1_2hz'event and clk1_2hz='1' then
if a='0' and b='0' and af='1' then ---------如果a发球
a:='1';
shift_1:="1000000000000000";------a的控制位置1
elsif a='0' and b='0' and bf='1' then ------如果b发球
b:='1';
shift_1:="0000000000000001";-------b的控制位置1
elsif a='1' and b='0' then -----------------球从a向b移动
if shift_1>128 then----------------------------如果没到球网b击球则a加分
if bj='1' then
a_score a_one a_one a_one a_one a_one a_one a_one a_one a_one a_one null;
end case;
case a_score is
when 0|1|2|3|4|5|6|7|8|9 =>a_ten a_ten null;
end case;
case b_score is
when 0|10 =>b_one b_one b_one b_one b_one b_one b_one b_one b_one b_one null;
end case;
case b_score is
when 0|1|2|3|4|5|6|7|8|9 =>b_ten b_ten null;
end case;
end process;
------------------------------------数码管动态扫描计数--------
process(clk1khz)
begin
if clk1khz'event and clk1khz='1' then
if cnt=3 then cnt data data data data null;
end case;
end process;
-----------------------------------------七段译码--------------------
process(data)
begin
case data is
when"0000"=>seg7 seg7 seg7 seg7 seg7 seg7 seg7 seg7 seg7 seg7 seg7<="1001111";
end case;
end process;
end;
2赫兹分频那里 我改动了 因为要仿真的时候时间太长仿真不出来 所以把249改成了1.
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity ping_pang is
port(clk1khz:in std_logic;------1khz时钟信号
rst:in std_logic;----------系统复位
af,aj:in std_logic;--------A方发球,A方击球
bf,bj:in std_logic;--------B方发球,B方击球
shift:out std_logic_vector(15 downto 0);----16个led代表乒乓球台
scan:out std_logic_vector(3 downto 0);------数码管地址选择信号
seg7:out std_logic_vector(6 downto 0));-----7段显示控制信号(abcdefg)
end;
architecture a_one of ping_pang is
signal clk1_2hz:std_logic;
signal a_score,b_score:integer range 0 to 11;
signal cnt:integer range 0 to 3;
signal data:std_logic_vector(3 downto 0);
signal a_one,a_ten,b_one,b_ten:std_logic_vector(3 downto 0);
begin
-------------------------------------2Hz分频-----
process(clk1khz)
variable count:integer range 0 to 2;
begin
if clk1khz'event and clk1khz='1' then
if count=1 then clk1_2hz '0');
elsif clk1_2hz'event and clk1_2hz='1' then
if a='0' and b='0' and af='1' then ---------如果a发球
a:='1';
shift_1:="1000000000000000";------a的控制位置1
elsif a='0' and b='0' and bf='1' then ------如果b发球
b:='1';
shift_1:="0000000000000001";-------b的控制位置1
elsif a='1' and b='0' then -----------------球从a向b移动
if shift_1>128 then----------------------------如果没到球网b击球则a加分
if bj='1' then
a_score a_one a_one a_one a_one a_one a_one a_one a_one a_one a_one null;
end case;
case a_score is
when 0|1|2|3|4|5|6|7|8|9 =>a_ten a_ten null;
end case;
case b_score is
when 0|10 =>b_one b_one b_one b_one b_one b_one b_one b_one b_one b_one null;
end case;
case b_score is
when 0|1|2|3|4|5|6|7|8|9 =>b_ten b_ten null;
end case;
end process;
------------------------------------数码管动态扫描计数--------
process(clk1khz)
begin
if clk1khz'event and clk1khz='1' then
if cnt=3 then cnt data data data data null;
end case;
end process;
-----------------------------------------七段译码--------------------
process(data)
begin
case data is
when"0000"=>seg7 seg7 seg7 seg7 seg7 seg7 seg7 seg7 seg7 seg7 seg7<="1001111";
end case;
end process;
end;
2赫兹分频那里 我改动了 因为要仿真的时候时间太长仿真不出来 所以把249改成了1.
