|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
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方击球
shiftut std_logic_vector(15 downto 0);----16个led代表乒乓球台
scanut std_logic_vector(3 downto 0);------数码管地址选择信号
seg7ut 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<=not clk1_2hz;count:=0;
else count:=count+1;
end if;
end if;
end process;
---------------------------------乒乓球比赛规则----------
process(rst,clk1_2hz)
variable a,b:std_logic;---a和b的控制位
variable shift_1:std_logic_vector(15 downto 0);
begin
if rst='1' then
a_score<=0;
b_score<=0;
a:='0';b:='0';
shift_1:=(others=>'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_score+1;
a:='0';b:='0';
shift_1:="0000000000000000";
else shift_1:='0'& shift_1(15 downto 1);----如果b没有击球则继续向b移动
end if;
elsif shift_1=0 then-------------如果b一直没接球则a加分
a_score<=a_score+1;
a:='0';b:='0';
else
if bj='1' then----如果b击球成功则b的控制位置1,a的控制位清0
a:='0';
b:='1';
else shift_1:='0'& shift_1(15 downto 1);
end if;
end if;
elsif b='1' and a='0' then ----------------球从b向a移动
if shift_1<256 and shift_1/=0 then
if aj='1' then b_score<=b_score+1;---如果没到球网a击球则b加分
a:='0';
b:='0';
shift_1:="0000000000000000";
else shift_1:=shift_1(14 downto 0)&'0';
end if;
elsif shift_1=0 then
b_score<=b_score+1;---------如果b一直没接球则a加分
a:='0';
b:='0';
else
if aj='1' then ---如果b击球成功则a的控制位置1,b的控制位清0
a:='1';
b:='0';
else shift_1:=shift_1(14 downto 0)&'0';
end if;
end if;
end if;
end if;
shift<=shift_1;
end process;
----------------------------------将a和b的计分换成bcd码------------------
process(a_score,b_score)
begin
case a_score is
when 0|10 =>a_one<="0000";
when 1|11 =>a_one<="0001";
when 2 =>a_one<="0010";
when 3 =>a_one<="0011";
when 4 =>a_one<="0100";
when 5 =>a_one<="0101";
when 6 =>a_one<="0110";
when 7 =>a_one<="0111";
when 8 =>a_one<="1000";
when 9 =>a_one<="1001";
when others=>null;
end case;
case a_score is
when 0|1|2|3|4|5|6|7|8|9 =>a_ten<="0000";
when 10|11=>a_ten<="0001";
when others=>null;
end case;
case b_score is
when 0|10 =>b_one<="0000";
when 1|11 =>b_one<="0001";
when 2 =>b_one<="0010";
when 3 =>b_one<="0011";
when 4 =>b_one<="0100";
when 5 =>b_one<="0101";
when 6 =>b_one<="0110";
when 7 =>b_one<="0111";
when 8 =>b_one<="1000";
when 9 =>b_one<="1001";
when others=>null;
end case;
case b_score is
when 0|1|2|3|4|5|6|7|8|9 =>b_ten<="0000";
when 10|11=>b_ten<="0001";
when others=>null;
end case;
end process;
------------------------------------数码管动态扫描计数--------
process(clk1khz)
begin
if clk1khz'event and clk1khz='1' then
if cnt=3 then cnt<=0;
else cnt<=cnt+1;
end if;
end if;
end process;
-------------------------------------数码管动态扫描-----------
process(cnt,a_ten,a_one,b_one,b_ten)
begin
case cnt is
when 0=> data<=b_one;scan<="0001";
when 1=> data<=b_ten;scan<="0010";
when 2=> data<=a_one;scan<="0100";
when 3=> data<=a_ten;scan<="1000";
when others=>null;
end case;
end process;
-----------------------------------------七段译码--------------------
process(data)
begin
case data is
when"0000"=>seg7<="1111110";
when"0001"=>seg7<="0110000";
when"0010"=>seg7<="1101101";
when"0011"=>seg7<="1111001";
when"0100"=>seg7<="0110011";
when"0101"=>seg7<="1011011";
when"0110"=>seg7<="1011111";
when"0111"=>seg7<="1110000";
when"1000"=>seg7<="1111111";
when"1001"=>seg7<="1111011";
when others=>seg7<="1001111";
end case;
end process;
end; |
|