|
#include <reg52.h>
#define START 0X01
#define TIME_OUT 0x02
#define BEGIN 0x03
//#define END 0x04
//#define RIGHT 0X05
#define ERROR 0x06
#define ACK 0x07
#define WAIT 0X08
unsigned char table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
unsigned char state=START;
unsigned char last_time=20,ack_time=30,num;
unsigned char i,tmp,count,count1,time;
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
sbit led4=P2^3;
sbit LIGHT=P3^2;
sbit SPK=P3^1;
sbit STR=P3^3;
sbit CLR=P3^4;
sbit ADD=P3^5;
sbit SUB=P3^6;
void init();
void display();
void delayms(unsigned char a);
void keyscan();
void main()
{
init();
while(1)
{
switch(state)
{
case START:
while(P1==0xff)
{
keyscan();
display();
if(state!=START)
break;
num=0;
}
tmp=P1;
for(i=0;i<8;i++)
{
if((~tmp)&(1<<i))
{
num=i+1;
state=ERROR;
P1=0XFF;
break;
}
}
break;
case BEGIN:
while(P1==0xff)
{
display();
// keyscan();
if(state!=BEGIN)
break;
}
tmp=P1;
TR0=0;
for(i=0;i<8;i++)
{
if((~tmp)&(1<<i))
{
num=i+1;
state=WAIT;
TR0=1;
break;
}
}
break;
case WAIT:
keyscan();
display();
case ACK:
// num=0xff;
keyscan();
display();
break;
case TIME_OUT:
last_time=0;
display();
keyscan();
SPK=0;
LIGHT=0;
break;
case ERROR:
keyscan();
display();
SPK=0;
LIGHT=0;
break;
default:
break;
}
}
}
void display()
{
unsigned char temp;
if(state==BEGIN)
temp=last_time;
else
temp=ack_time;
P0=table[temp/10];
led1=0;
led2=1;
led3=1;
led4=1;
delayms(5);
P0=table[temp%10];
led1=1;
led2=0;
led3=1;
led4=1;
delayms(5);
P0=table[num>>4];
led1=1;
led2=1;
led3=0;
led4=1;
delayms(5);
P0=table[num&0x0f];
led1=1;
led2=1;
led3=1;
led4=0;
delayms(5);
led1=1;
led2=1;
led3=1;
led4=1;
}
void keyscan()
{
if(state==START)
{
if(STR==0)
{
delayms(10);
if(STR==0)
{
state=BEGIN;
TR0=1;
}
while(!STR);
}
if(CLR==0)
{
delayms(10);
if(CLR==0)
{
last_time=20;
ack_time=30;
num=0;
SPK=1;
LIGHT=1;
}
while(!CLR);
}
if(ADD==0)
{
delayms(10);
if(ADD==0)
{
ack_time++;
if(ack_time==100)
{
ack_time=10;
}
}
while(!ADD);
}
if(SUB==0)
{
delayms(10);
if(SUB==0)
{
ack_time--;
if(ack_time==10)
{
ack_time=99;
}
while(!SUB);
}
}
}
if(state==TIME_OUT||state==ACK||state==ERROR)
{
if(CLR==0)
{
delayms(10);
if(CLR==0)
{
state=START;
last_time=20;
num=0;
TR0=0;
SPK=1;
LIGHT=1;
}
}
// while(!CLR);
}
}
void delayms(unsigned char a)
{
unsigned char j;
for(a;a>0;a--)
for(j=110;j>0;j--);
}
|
|