用数码管和独立键盘做拔河游戏机

//拔河游戏机
//唯一不能解决的是当玩家1按下按键的时候,玩家2按键无效即不能加1

#include<reg52.h>

sbit key1 = P3^4 ;	//玩家1
sbit key2 = P3^7 ;	//玩家2
sbit key3 = P3^5 ;	//中止游戏按键
sbit key4 = P3^6 ;	//重新开始游戏按键
uint n1 = 0 , n2 = 0 ;
uchar t0 = 0 ;
uchar bai1 , shi1 , ge1 , bai2 , shi2 , ge2 ;

uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

uchar code tablewe[]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf};

void init()
{
	TH0 = ( 65536 - 4608 ) / 256 ;
	TL0 = ( 65536 - 4608 ) % 256 ;
	TMOD = 0x01 ;
	EA = 1 ;
	ET0 = 1 ;
	P0 = 0 ;
	wela = 1 ;
	wela = 0 ;
	P0 = 0x3f ;
	dula = 1 ;
	dula = 0 ;
}

void delay()
{
	TR0 = 1 ;
	while( !t0 ) ;
	t0 = 0 ;
	TR0 = 0 ;
}

void display()
{
//	panduan() ;
	bai1 = n1 / 100 ;
	shi1 = n1 % 100 / 10 ;
	ge1  = n1 % 10 ;					   
	bai2 = n2 / 100 ;
	shi2 = n2 % 100 / 10 ;
	ge2  = n2 % 10 ;

	P0 = tablewe[0] ;
	wela = 1 ;
	wela = 0 ;
	P0 = table[bai1] ;
	dula = 1 ;
	dula = 0 ;
	TR0 = 1 ;
	while( !t0 ) ;
	t0 = 0 ;
	TR0 = 0 ;
	
	P0 = tablewe[1] ;
	wela = 1 ;
	wela = 0 ;
	P0 = table[shi1] ;
	dula = 1 ;
	dula = 0 ;
	TR0 = 1 ;
	while( !t0 ) ;
	t0 = 0 ;
	TR0 = 0 ;
	
	P0 = tablewe[2] ;
	wela = 1 ;
	wela = 0 ;
	P0 = table[ge1] ;
	dula = 1 ;
	dula = 0 ;
	TR0 = 1 ;
	while( !t0 ) ;
	t0 = 0 ;
	TR0 = 0 ;
	
	P0 = tablewe[3] ;
	wela = 1 ;
	wela = 0 ;
	P0 = table[bai2] ;
	dula = 1 ;
	dula = 0 ;
	TR0 = 1 ;
	while( !t0 ) ;
	t0 = 0 ;
	TR0 = 0 ;
	
	P0 = tablewe[4] ;
	wela = 1 ;
	wela = 0 ;
	P0 = table[shi2] ;
	dula = 1 ;
	dula = 0 ;
	TR0 = 1 ;
	while( !t0 ) ;
	t0 = 0 ;
	TR0 = 0 ;
	
	P0 = tablewe[5] ;
	wela = 1 ;
	wela = 0 ;
	P0 = table[ge2] ;
	dula = 1 ;
	dula = 0 ;
	TR0 = 1 ;
	while( !t0 ) ;
	t0 = 0 ;
	TR0 = 0 ;
}

void panduan()
{
	if( key1 == 0 )
	{
		delay() ;
		if( key1 == 0 )
		{
			n1++ ;
			P1 = ~P1 ;
//			display() ;
			while( !key1 ) display() ;
			delay() ;
			while( !key1 ) display() ;
//			display() ;
			P1 = ~P1 ;
		}
//		display() ;
	}
//	display() ;
	if( key2 == 0 )
	{
		delay() ;
		if( key2 == 0 )
		{
			n2++ ;
			P1 = ~P1 ;
//			display() ;
			while( !key2 ) display() ;
			delay() ;
			while( !key2 ) display();
//			display() ;
			P1 = ~P1 ;
		}
//		display() ;
	}
//	display() ;
}

void main()
{
	init() ;
	while( 1 )
	{
		display() ;
		panduan() ;
		if( key3 == 0 ) break ;
	}
	while(1)
	{
		display() ;
		if( key4 == 0 ) break ;
	}
}

void timer0() interrupt 1	// 5ms
{
	TH0 = ( 65536 - 4608 ) / 256 ;
	TL0 = ( 65536 - 4608 ) % 256 ;
	t0++ ;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值