乒乓球游戏机C程序.docx

上传人:b****6 文档编号:8035401 上传时间:2023-01-28 格式:DOCX 页数:9 大小:131.46KB
下载 相关 举报
乒乓球游戏机C程序.docx_第1页
第1页 / 共9页
乒乓球游戏机C程序.docx_第2页
第2页 / 共9页
乒乓球游戏机C程序.docx_第3页
第3页 / 共9页
乒乓球游戏机C程序.docx_第4页
第4页 / 共9页
乒乓球游戏机C程序.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

乒乓球游戏机C程序.docx

《乒乓球游戏机C程序.docx》由会员分享,可在线阅读,更多相关《乒乓球游戏机C程序.docx(9页珍藏版)》请在冰豆网上搜索。

乒乓球游戏机C程序.docx

乒乓球游戏机C程序

乒乓球游戏机C程序

基于51单片机的乒乓球游戏机设计

前一段时间田老师给我们班一些选题,让我们每个人都选一个,我选的是基于51单片机乒乓游戏设计。

我设计乒乓球游戏已有一段时间,在一个星期之前,我就已经设计好了一个能正常游戏的功能,但是我认为有些方面还是没设计好,所以就没发上来。

今天想想还是发上来吧,不好的地方以后再该。

乒乓球游戏机的设计主要偏重于软件部分,我主要也是在程序上下工夫。

我设计的乒乓球游戏机足要是在精简mini51版的平台上实现的。

乒乓球游戏的设计主要功能如下:

1、能够对乒乓球的游戏轨迹移动速度进行调节。

2、能过通过数码管显示等级难度和比赛分数。

3、比赛中若是没及时按下按键会有有报警声发出。

给出几张图如下:

(注:

这里有根跳线是因为乒乓球led轨迹用的是P1口,蜂鸣器是P1.7,我为了使两者减少干扰故用跳线将蜂鸣器接到P2.0口上。

还有就是led中的一个led总亮着是因为数码管的扫描用到这一引脚故这一引脚常亮。

图一:

按键说明

图二:

难度等级8,开始时0:

0

图三:

难度等级8,比赛结果2:

1

图四:

难度等级10,比赛结果6:

6,当然比赛是不会出现这种结果喽,因为比赛还没

结束。

程序源代码如下:

#include

#defineTIME_PER_SEC640//定义数码管扫描频率,8*40Hz#defineCLOCK22118400//定义时钟晶振,单位Hz

//按键和595的引脚定义

sbitSH_CP=P2^4;

sbitST_CP=P1^5;sbitDS=P2^5;

sbitCP_key=P3^2;

sbitC_key=P3^3;

sbitA_key=P3^4;

sbitB_key=P3^5;

//流水灯表

unsignedchardisp[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};

//数码管字表

unsignedcharcode

led_bit[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};//{0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,

0xfe};

unsignedcharcode

led_seg[17]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0x11,0xc1,0x63,0x85,0x61,

0x71,0xff};//0xfd

//全局变量

unsignedcharAscore,Bscore,mov,level,L;

unsignedcharcount0,count1,count2;

unsignedcharbuffer[8];

//key按键标志

bitflag;

bitflag1;

//蜂鸣器定义

sbitBuzzer=P2^0;

/*---------------初始化--------------*/

voidinitial595()

{

SH_CP=0;

ST_CP=0;

DS=0;

}

/*---------------发送数据---------------*/

voidsend_data(unsignedchard)

{

unsignedchari;

for(i=0;i<8;i++)

{

DS=d&0x01;//发送最低位

SH_CP=1;

SH_CP=0;//移位寄存器移位

d=d>>1;//数据移位

}

}

//延时程序

voiddelay(unsignedintt)

{

while(--t);

}

//定时器0初始化

voidtimer_init(void){

TMOD=0x11;//定时器0工作在模式1,16Bit定时器模式

TH0=(65536-CLOCK/TIME_PER_SEC/12)/256;

TL0=(65536-CLOCK/TIME_PER_SEC/12)%256;

TR0=1;

ET0=1;//开启定时器和中断

TH1=(65536-5000)/256;

TL1=(65536-5000)%256;

TR1=1;

ET1=1;

}

voidmain()

{unsignedcharj=0;

initial595();

timer_init();

count0=0;

count1=0;

count2=0;

mov=0;

Ascore=0;

Bscore=0;

flag=0;

flag1=0;

L=1;

level=20;

EA=1;

while

(1)

{

if(CP_key==0)//设置等级

{delay(200);

if(CP_key==0)

{

level=level-2;

while(CP_key==0);

//等级

if(level==18)L=2;

if(level==16)L=3;

if(level==14)L=4;

if(level==12)L=5;

if(level==10)L=6;

if(level==8)L=7;

if(level==6)L=8;

if(level==4)L=9;

if(level==2)L=10;

if(level==0||level==20)

{

level=20;

L=1;

}

}

}

if(C_key==0)//判断比赛是否开始

{

delay(200);

if(C_key==0)

{

flag=~flag;//使下面的中断的Bscore的多的一分减去

Bscore=-1;

while(C_key==0);

}

}

if(flag1==1)

{flag1=0;

if(flag==1)

{

count0++;

if(count0==level)

{

count0=0;

if(count1++>13)count1=1;

if(count1==1)

{

if(A_key)

{

delay(200);

if(A_key)

{

P1=disp[0];

Bscore++;

for(j=0;j<100;j++)

{

Buzzer=0x0;

delay(50);

Buzzer=0x1;

delay(50);

}

}

while(A_key);

}

}

if(count1==8)

{

if(B_key)

{

delay(200);

if(B_key)

{

P1=disp[7];

Ascore++;

for(j=0;j<100;j++)

{

Buzzer=0x0;

delay(50);

Buzzer=0x1;

delay(50);

}

}

while(B_key);

}

}

P1=disp[mov];

if(count1<8)mov++;

elsemov--;

}

}

}

}

}

voidtimer0_int(void)interrupt1{

staticunsignedchari=0;

TR0=0;

TH0=(65536-CLOCK/TIME_PER_SEC/12)/256;

TL0=(65536-CLOCK/TIME_PER_SEC/12)%256;

TR0=1;

send_data(led_seg[buffer[i]]);

send_data(led_bit[i]);

ST_CP=1;

ST_CP=0;

i=++i&0x07;

buffer[7]=L;

buffer[6]=16;

buffer[5]=16;

buffer[4]=Ascore/10;

buffer[3]=Ascore%10;

buffer[2]=16;

buffer[1]=Bscore/10;

buffer[0]=Bscore%10;}

voidtimer1_int(void)interrupt3{

unsignedcharj=0;

TR1=0;

TH1=(65536-50000)/256;

TL1=(65536-50000)%256;

TR1=1;

flag1=1;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1