完整版 电路图和程序代码 下载地址
https://pan.baidu.com/s/1Y7ZIzqyffREb05C3yPNLlA?pwd=8888
部分代码展示
#include<at89x51.h>
#define KeyProt P1 //按键引脚定义
#define LedAlarm P3 //报警指示灯
#define LED_Data P0 //数码管引脚定义
sbit LED_Bit = P2^1; //数码管位选1
sbit LED_Dat = P2^0; //数码管位选2
sbit SoundProt = P2^2; //报警引脚定义
sbit StartProt = P2^3; //开始引脚定义
sbit TrueProt = P2^4; //正确引脚定义
sbit FalseProt = P2^5; //错误引脚定义
sbit Led_Red = P2^6;
sbit Led_Green = P2^7;
unsigned char code DataMAC[11]=
{
0x3F,0x06,0x5B,0x4F,0x66,
0x6D,0x7d,0x07,0x7f,0x6F,
0x00
};
unsigned char TimeCounter=0,SecondCount=0,AckFlag=2,memberCount=10;
unsigned char dis_num[8]={0};
void delay_Led(unsigned int i)//简单延时函数
{
unsigned char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}
void delayms(void)
{
unsigned char i=0,j=0;
for(i = 2; i > 0; i--)
for(j = 150; j > 0; j--);
}
void Display_LED(unsigned char DataVal[])
{
unsigned char i=0,Value=1;
for(i = 0; i < 8; i++)
{
if(DataVal[i]&0x80)
{
DataVal[i]-=0x80;
LED_Data = ~(DataMAC[DataVal[i]]|0x80);
}
else
{
LED_Data = ~DataMAC[DataVal[i]];
}
LED_Dat = 1;
LED_Dat = 0;
LED_Data = Value;
LED_Bit = 1;
LED_Bit = 0;
Value *= 2;
delayms();
LED_Data = 0;
LED_Bit = 1;
LED_Bit = 0;
}
}
void SoundAlarm(void) //蜂鸣器按键声
{
SoundProt=0;
delay_Led(100);
SoundProt=1;
}
void system_Ini() //定时器设置
{
TMOD= 0x01; //定时器0设置
TH1=(65536-20000)/256; //装载计数值
TL1=(65536-20000)%256; //装载计数值
EA=1; //开中断
ET0=1; //开定时器中断
TR0=0; //关闭计时
}
void LogicProcess(void)
{
if(!StartProt) //检测开始键是否按下
{
delay_Led(50); //消除抖动
if(!StartProt) //再次检测开始键是否按下
{
while(!StartProt); //等待按键释放
TR0=1; //启动定时器
AckFlag=1; //开始抢答
LedAlarm=0xFF; //报警灯消除
SecondCount=30; //报警时间设置
SoundAlarm(); //按键伴音
memberCount=10;
Led_Red = 1;
Led_Green = 1;
}
}
if(!AckFlag)
{
if(!TrueProt) //检测确定键是否按下
{
delay_Led(50); //消除抖动
if(!TrueProt) //再次检测确定键是否按下
{
while(!TrueProt); //等待按键释放
TR0=0; //关闭定时器
AckFlag=2; //关闭抢答
SoundAlarm(); //按键伴音
Led_Red = 0;
Led_Green = 1;
}
}
if(!FalseProt) //检测确定键是否按下
{
delay_Led(50); //消除抖动
if(!FalseProt) //再次检测确定键是否按下
{
while(!FalseProt); //等待按键释放
TR0=0; //关闭定时器
AckFlag=2; //关闭抢答
SoundAlarm(); //按键伴音
Led_Red = 1;
Led_Green = 0;
}
}
}
if(AckFlag==1) //开始抢答
{
switch(KeyProt)
{
case 0xFE:
while(KeyProt==0xFE);
AckFlag=0;
SoundAlarm();
memberCount=0x01;
LedAlarm&=0x7F;
SecondCount=60;
break;
case 0xFD:
while(KeyProt==0xFD);
AckFlag=0;
SoundAlarm();
memberCount=0x02;
LedAlarm&=0xBF;
SecondCount=60;
break;
case 0xFB:
while(KeyProt==0xFB);
AckFlag=0;
SoundAlarm();
memberCount=0x03;
LedAlarm&=0xDF;
SecondCount=60;
break;