我们从2011年坚守至今,只想做存粹的技术论坛。  由于网站在外面,点击附件后要很长世间才弹出下载,请耐心等待,勿重复点击不要用Edge和IE浏览器下载,否则提示不安全下载不了

 找回密码
 立即注册
搜索
查看: 716|回复: 0

我用VHDL写了个乒乓球游戏机 也挺有意思的 搞了一下午了

[复制链接]

该用户从未签到

47

主题

34

回帖

4

积分

一级逆天

活出自我

积分
4

社区居民终身成就奖

QQ
发表于 2015-10-9 18:09:26 | 显示全部楼层 |阅读模式
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;
因为梦想,所以指着
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

论坛开启做任务可以
额外奖励金币快速赚
积分升级了


Copyright ©2011-2024 NTpcb.com All Right Reserved.  Powered by Discuz! (NTpcb)

本站信息均由会员发表,不代表NTpcb立场,如侵犯了您的权利请发帖投诉

平平安安
TOP
快速回复 返回顶部 返回列表