51单片机 16路数字抢答器
源代码在线查看: 抢答器程序.txt
#include
#define uchar unsigned char
#define uint unsigned int
#define DB P1
void count_time(uint);
uint t1;
uint t2=0;
uchar j=10;
//void delay (void);
sbit DB0=P1^2;
sbit DB1=P1^3;
sbit DB2=P1^4;
sbit DB3=P1^5;
sbit DB4=P1^6;
sbit DB5=P1^0;
sbit DB6=P1^1;
sbit c0=P3^0;
sbit c1=P3^1;
sbit c2=P3^2;
sbit c3=P3^3;
sbit c4=P3^4;
sbit c5=P3^5;
sbit c6=P3^6;
sbit c7=P3^7;
uchar qd_time[]={80,50,30,10 };//设定抢答时间
uchar display_code0[]={
0xED,/*0*/ //两位一体的代码
0x88,/*1*/
0xD5,/*2*/
0xD9,/*3*/
0xB8,/*4*/
0x79,/*5*/
0x7D,/*6*/
0xC8,/*7*/
0xFD,/*8*/
0xF9,/*9*/
};
uchar display_code1[]={
// 0x7E,/*0*/ //一位的代码
0x12,/*1*/
0xBC,/*2*/
0xB6,/*3*/
0xD2,/*4*/
0xE6,/*5*/
0xEE,/*6*/
0x32,/*7*/
0xFE,/*8*/
0xF6,/*9*/
};
void timer0(void) interrupt 1 using 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t2++;
if(t2>20)
{
t2=0;
t1--;
}
}
void delay (uint tt)
{
while(tt)
tt--;
}
void key (uint t )
{
uchar i;
i=t/10; //开始显示倒计时间
DB5=DB6=1;
P0=display_code0[i];
DB5=0;
delay(500);
DB5=DB6=1;
i=t%10;
P0=display_code0[i];
DB6=0;
delay(500);
}
void yrqd(void)
{
DB0=0;
// P3=1;
delay(10000);
DB0=1;
P2=0;
if(!c0)
{ P2=0x12;
while(1)
key(t1);
}
else if(!c1)
{ P2=0xBC;
while(1)
key(t1);}
else if(!c2)
{ P2=0xB6;
while(1)
key(t1);
}
else if(!c3)
{ P2=0xD2;
while(1)key(t1);}
else if(!c4)
{ P2=0xE6;
while(1)key(t1);}
else if(!c5)
{ P2=0xEE;
while(1)key(t1);}
else if(!c6)
{ P2=0x32;
while(1)key(t1);}
else if(!c7)
{ P2=0xFE;
while(1)key(t1);}
}
void count_time(uint t1 )
{
/* if(t1 {
DB0=0;
delay(1);
DB0=1; }
//for(j=0;j
/* TMOD=0x61;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=256-5;
TL1=256-5;
IP=0X08;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
EA=1; */
if((P3&0xff)!=0xff) //有人抢答
{ TR0=0;
yrqd();
}
else if(t1==j)
{ DB0=0;
delay(500);
DB0=1;
j--;
}
else if(t1==0)
{
// DB=1;
DB0=0;
delay(10000);
DB0=1;
while(1)
key(0);
}
/* if(t1 {
DB0=0;
delay(500);
DB0=1;
} */
key(t1);
/* i=t/10; //开始显示倒计时间
DB5=DB6=1;
P0=display_code0[i];
DB5=0;
delay(500);
DB5=DB6=1;
i=t%10;
P0=display_code0[i];
DB6=0;
delay(500);
/* if(t1==0) //这个程序要修改
{ DB0=0;
delay(1000);
DB=1;
} */
/* if((P3&0xff)!=0xff)
{ TR0=0;
yrqd();
P3=1;
} //有人抢答 */
// }
}
void main(void )
{
// uint i;
// while(1)
// {
DB0=1;
DB0=0; //复位响喇叭
delay(10000);
DB0=1;
P2=0;
if(!DB1)
t1=80;
else if(!DB2)
t1=50;
else if(!DB3)
t1=30;
else if(!DB4)
t1=10;
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
// TH1=256-5;
// TL1=256-5;
// IP=0X08;
EA=1;
ET0=1;
// ET1=1;
TR0=1;
// TR1=1;
// for(i=t1;i>0;i--)
while(1)
{
count_time( t1 );
}
// }
}