乒乓球比赛游戏机.docx
《乒乓球比赛游戏机.docx》由会员分享,可在线阅读,更多相关《乒乓球比赛游戏机.docx(17页珍藏版)》请在冰豆网上搜索。
乒乓球比赛游戏机
课程名称:
EDA技术及应用成绩:
课程设计
课程设计题目:
乒乓球比赛游戏机
学生姓名:
学号:
学校指导教师姓名:
所在班级:
日期:
摘要
用VHDL编程模拟乒乓球比赛,电路模块由分频模块、译码器、数码管、7段译码器、音乐模块等部分组成,对各部分编写VHDL算法,进行编译、仿真、逻辑综合、逻辑适配及程序下载,实现乒乓球游戏机模拟乒乓球比赛的基本过程和规则,并能自动裁判和记分的目的。
关键字:
VHDL语言;乒乓球游戏机;仿真
引言
乒乓球运动是一项受大众非常喜爱的运动,它是一项集健身性,竞技性和娱乐性为一体的运动。
乒乓球是一项非常受大众喜欢的运动,几十年来,乒乓球运动在中国迅速兴起,很重要的原因就是对客观条件要求不高,随时随处就能玩起来。
乒乓球运动越来越多地被作为增强智力、提高工作效率以及保健、医疗和康复的极佳手段而引起各方面的重视。
如今,人们生活太忙碌,很多的人只能通过网络来玩乒乓球游戏,而乒乓球游戏机正好解决了人们的困扰。
它简单易行,只要简简单单几个手指的操作就能达到同样娱乐跟锻炼的效果。
1项目名称、内容与要求
1.1项目名称
乒乓球比赛游戏机
1.2设计目的
这次的设计主要是学习使用EDA集成设计软件Max+PlusII,电路描述,综合,模拟过程。
掌握使用EDA工具设计乒乓游戏机的的设计思路和设计方法。
体会使用EDA综合过程中电路设计方法和设计思路的不同。
1.3设计内容
设计一个由甲乙双方参赛,有裁判三人乒乓球游戏机。
用8个(或更多个)LED排成一条直线,以中点为界,两边各代表参赛双方的位置,其中一只点亮的LED指示球的当前位置,点亮的LED依次从左到右,或从右到左,其移动的速度应能调节。
当“球”(点亮的那只LED)运动到某方的最后一位时,参赛者应能果断地按下位于自己一方的按纽开关,即表示启动球拍击球,若击中,则球向相反方向运动;若未中,球掉出桌外,则对方得一分。
设置自动记分电路,甲乙双方各用两位数码管进行记分显示,每计满11分为
1局。
甲乙双方各设一个发光二极管表示拥有发球权,每隔2次自动交换发球权,拥有发球权的一方发球才有效。
1.4具体要求
(1)使用乒乓球游戏机的双方在不同位置发球或击球。
(2)乒乓球的位置和移动方向由灯亮和依次亮的方向决定。
使用者根据球的位置发出相应的动作。
(3)比赛用11分为一局来进行,双方设置各自的记分牌,任意一方先记满21分就获胜此局。
当记分牌清零后,开始新的一局比赛。
(4)比赛结束后音乐自动响起。
2系统整体架构
2.1设计思路
根据系统设计的要求,乒乓球比赛游戏机的电路原理框图如下:
图1乒乓球游戏机原理图
3系统设计
3.1系统整体电路图
图2系统整体电路图
3.2VHDL代码
比赛模块
libraryieee;
useieee.std_logic_1164.all;
useieee.std_logic_arith.all;
useieee.std_logic_unsigned.all;--引用必要的库函数和包集合
entitycompeteis--实体名为pingpong
port(reset:
instd_logic;
clk_1:
instd_logic;
startbutton:
instd_logic;--开始游戏输入端口
serve:
instd_logic_vector(1downto0);--发球输入端口
hit1,hit2:
instd_logic;--甲和乙的击球输入端口
light:
outstd_logic_vector(1to8);--控制8个发光二极管的输出端口
music_begin:
outstd_logic;--控制音乐开始的输出端口
counta,countb:
outstd_logic_vector(3downto0));--2个用于控制4个7段译码器的输出端口
endcompete;
architectureoneofcompeteis
typepingpongis(waitserve,light1on,ballmoveto2,allow2hit,
light8on,ballmoveto1,allow1hit);
---设置7个状态,为枚举数据类型,记为pingpong
signalstate:
pingpong;
signali:
integerrange0to8;
signalcount1,count2,count3,count4:
std_logic_vector(3downto0):
="0000";
---内部计数器,是4位二进制变量
begin
process(clk_1)
begin
if(clk_1'eventandclk_1='1')then
ifcount3="0001"orcount4="0001"then
music_begin<='1';
endif;
if(reset='1')then
music_begin<='0';
endif;
endif;
endprocess;
process(clk_1)--状态机进程
--clk_1作为敏感信号触发进程
begin--进程开始
ifreset='1'then--异步置位
i<=0;count1<="0000";count2<="0000";count3<="0000";count4<="0000";
elsifclk_1'eventandclk_1='1'then--当处于时钟inclock上升沿时
ifcount1="1011"then
i<=0;count1<="0000";count2<="0000";count3<=count3+1;
elsifcount2="1011"then
i<=0;count1<="0000";count2<="0000";count4<=count4+1;
elsifstartbutton='0'then
i<=0;count1<="0000";count2<="0000";count3<="0000";count4<="0000";
else--以下case语句是程序中最关键的状态机部分
casestateis
whenwaitserve=>--进程处于等待发球状态
caseserveis
when"01"=>i<=1;state<=light1on;
when"10"=>i<=8;state<=light8on;
when"11"=>i<=0;
whenothers=>i<=0;
endcase;
whenlight1on=>--进程处于第一盏灯亮状态
i<=2;
ifhit2='1'then
i<=0;
count1<=count1+1;state<=waitserve;
else
state<=ballmoveto2;
endif;
whenlight8on=>--进程处于第八盏灯亮状态
i<=7;
ifhit1='1'then
i<=0;
count2<=count2+1;state<=waitserve;
else
state<=ballmoveto1;
endif;
whenballmoveto1=>--进程处于球向乙移动状态
ifhit1='1'then
i<=0;
count2<=count2+1;state<=waitserve;
elsifi=2theni<=1;
state<=allow1hit;
elsei<=i-1;
endif;
whenballmoveto2=>--进程处于球向乙移动状态
ifhit2='1'then
i<=0;
count1<=count1+1;state<=waitserve;
elsifi=7theni<=8;
state<=allow2hit;
elsei<=i+1;
endif;
whenallow1hit=>--进程处于允许甲击球状态
ifhit1='1'theni<=2;
state<=ballmoveto2;
elsecount2<=count2+1;i<=0;
state<=waitserve;
endif;
whenallow2hit=>--进程处于允许乙击球状态
ifhit2='1'theni<=7;state<=ballmoveto1;
elsecount1<=count1+1;i<=0;
state<=waitserve;
endif;
endcase;
endif;
endif;
endprocess;
counta<=count1;countb<=count2;
--进程处i信号控制发光二极管的亮暗
light<="10000000"when(i=1)else
"01000000"when(i=2)else
"00100000"when(i=3)else
"00010000"when(i=4)else
"00001000"when(i=5)else
"00000100"when(i=6)else
"00000010"when(i=7)else
"00000001"when(i=8)else
"00000000";--其他情况所有发光二极管都暗
endone;
分频模块
libraryIEEE;
useIEEE.std_logic_1164.all;
useieee.std_logic_arith.all;
useIEEE.std_logic_unsigned.all;
entitydivision16is
port(cp:
instd_logic;
clk_4:
outstd_logic);
enddivision16;
architecturedivision_bodyofdivision16is
signalcount:
std_logic_vector(3downto0);
begin
process(cp)
begin
if(cp'eventandcp='1')then
if(count="1111")then
count<=(others=>'0');
else
count<=count+1;
endif;
endif;
endprocess;
process(cp)
begin
if(cp'eventandcp='1')then
if(count="1111")then
clk_4<='1';
else
clk_4<='0';
endif;
endif;
endprocess;
enddivision_body;
译码器部分
libraryieee;
useieee.std_logic_1164.all;
entitydispis
port(d:
instd_logic_vector(3downto0);
q:
outstd_logic_vector(6downto0));
enddisp;
architecturedisp_arcofdispis
begin
process(d)
begin
casedis
when"0000"=>q<="0111111";
when"0001"=>q<="0000110";
when"0010"=>q<="1011011";
when"0011"=>q<="1001111";
when"0100"=>q<="1100110";
when"0101"=>q<="1101101";
when"0110"=>q<="1111101";
when"0111"=>q<="0100111";
when"1000"=>q<="1111111";
when"1001"=>q<="1101111";
whenothers=>q<="0000000";
endcase;
endprocess;
enddisp_arc;
数码管选择
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
ENTITYmux2IS
PORT(a,b:
INSTD_LOGIC_VECTOR(6downto0);
sel:
INSTD_LOGIC;
c:
OUTSTD_LOGIC_VECTOR(6downto0));
ENDmux2;
ARCHITECTUREexampleOFmux2IS
BEGIN
PROCESS(sel)
BEGIN
IF(SEL='1')THEN
c<=a;
ELSE
c<=b;
ENDIF;
ENDPROCESS;
ENDexample;
音乐模块(两只老虎)
libraryIEEE;
useIEEE.std_logic_1164.all;
useIEEE.std_logic_unsigned.all;
entitymusicis
port(music_begin:
instd_logic;
clk_4:
instd_logic;
clk:
instd_logic;
music_out:
outstd_logic);
endmusic;
architecturemusic_bodyofmusicis
constantm1:
integer:
=637;--955;
constantm2:
integer:
=587;--851;
constantm3:
integer:
=505;--758;
constantm4:
integer:
=468;--716;
constantm5:
integer:
=425;--639;
constantm6:
integer:
=379;--569;
constantm7:
integer:
=330;--506;
constantm0:
integer:
=0;
signalcounter:
integerrange0to67;
signalcount:
integerrange0to1000;
signalsub:
integerrange0to1000;
signalcarrier:
std_logic;
signalpat,pat1,pat2:
std_logic;
begin
process(clk)
begin
if(clk'eventandclk='1')then
if(carrier='1')then
sub<=count;
else
sub<=sub-1;
endif;
endif;
endprocess;
process(sub)
begin
if(sub=0)then
pat<='1';
else
pat<='0';
endif;
carrier<=pat;
endprocess;
process(clk)
begin
if(clk'eventandclk='1')then
pat1<=pat;
endif;
endprocess;
process(pat1)
begin
if(pat1'eventandpat1='1')then
pat2<=notpat2;
endif;
music_out<=pat2;
endprocess;
process(clk_4)
begin
if(clk_4'eventandclk_4='1')then
if(music_begin='1')then
counter<=counter+1;
else
counter<=0;
endif;
endif;
endprocess;
process(counter)
begin
casecounteris
when0=>count<=m0;when1=>count<=m1;
when2=>count<=m1;when3=>count<=m2;
when4=>count<=m2;when5=>count<=m3;
when6=>count<=m3;when7=>count<=m1;
when8=>count<=m1;when9=>count<=m1;
when10=>count<=m2;when11=>count<=m2;
when12=>count<=m3;when13=>count<=m3;
when14=>count<=m1;when15=>count<=m1;
when16=>count<=m3;when17=>count<=m3;
when18=>count<=m4;when19=>count<=m4;
when20=>count<=m5;when21=>count<=m5;
when22=>count<=m5;when23=>count<=m5;
when24=>count<=m3;when25=>count<=m3;
when26=>count<=m4;when27=>count<=m5;
when28=>count<=m5;when29=>count<=m5;
when30=>count<=m5;when31=>count<=m5;
when32=>count<=m5;when33=>count<=m6;
when34=>count<=m5;when35=>count<=m4;
when36=>count<=m3;when37=>count<=m3;
when38=>count<=m1;when39=>count<=m1;
when40=>count<=m5;when41=>count<=m6;
when42=>count<=m5;when43=>count<=m4;
when44=>count<=m3;when45=>count<=m3;
when46=>count<=m1;when47=>count<=m1;
when48=>count<=m0;when49=>count<=m1;
when50=>count<=m1;when51=>count<=m5;
when52=>count<=m5;when53=>count<=m1;
when54=>count<=m1;when55=>count<=m1;
when56=>count<=m1;when57=>count<=m0;
when58=>count<=m1;when59=>count<=m1;
when60=>count<=m5;when61=>count<=m5;
when62=>count<=m1;when63=>count<=m1;
when64=>count<=m1;when65=>count<=m5;
when66=>count<=m0;when67=>count<=m0;
endcase;
endprocess;
endmusic_body;
4系统仿真
图3功能仿真波形图
图4时序仿真波形图
5结论及心得体会
在大三第一学期课程即将接近尾声的时候,学院最后为我们安排了EDA课程设计,通过这次课程设计使我懂得了理论与实际相结合是很重要的,只有理论知识是远远不够的,只有把所学的理论知识与实践相结合起来,从理论中得出结论,才能真正为社会服务,从而提高自己的实际动手能力和独立思考的能力。
在设计的过程中遇到问题,可以说得是困难重重,这毕竟第一次做的,难免会遇到过各种各样的问题,同时在设计的过程中发现了自己的不足之处,对以前所学过的知识理解得不够深刻,掌握得不够牢固。
总的来说,这次设计的数还是比较成功的,在设计中遇到了很多问题,最后在老师的辛勤的指导下,终于游逆而解,有点小小的成就感,终于觉得平时所学的知识有了实用的价值,达到了理论与实际相结合的目的,不仅学到了不少知识,而且锻炼了自己的能力,使自己对以后的路有了更加清楚的认识,同时,对未来有了更多的信心。
最后,对给过我帮助的所有同学和各位指导老师再次表示衷心的感谢!
致谢
通过这一周的忙碌和学习,本次课程设计设计已接近尾声,作为一个大三的学生,由于经验的匮乏,难免有许多考虑不周全的地方,在这里衷心感谢指导老师的督促指导,以及一起学习的同学们的支持,让我按时完成了这次课程设计。
在课程设计过程中,我遇到了许许多多的困难。
在此我要感谢我的指导老师颜鲁薪老师给我悉心的帮助和对我耐心而细致的指导,我的课程设计较为复杂烦琐,但是颜鲁薪老师仍然细心地纠正图中的错误。
除了敬佩颜鲁薪老师的专业水平以外,他的治学严谨和科学研究的精神也是我永远学习的榜样,并将积极影响我今后的学习和工作,我才得以解决课程设计中遇到的种种问题。
最后,再次向我的指导老师致以衷心的感谢!
参考文献
[1]吴延海.EDA技术及应用.西安:
西安电子科技大学出版社,2012
[2]罗映红.电子技术.北京:
中国电力出版社,2010