#include<reg52.h>
//#include"24c01.h"
#define uchar unsigned char
sbit sda=P1^6;
sbit scl=P1^5;
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
sbit P14=P1^4;
sbit P17=P1^7;
sbit P30=P3^0;
sbit P31=P3^1;
sbit P32=P3^2;
sbit P33=P3^3;
sbit P34=P3^4;
sbit P35=P3^5;
sbit P36=P3^6;
sbit P37=P3^7;
char code dis_7[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0xbf,0xc8,0xff,0xb6,0x86,0x8f};
char code scan_con[6]={0x01,0x02,0x04,0x08,0x10,0x20};
char data dia[6]={0x08,0x08,0x08,0x08,0x08,0x08};
unsigned char data dat1[2][6]={0,0,0,0,0,0};
unsigned char data dat2[6];
uchar a;
bit flag=0,flag1=0;
void delayms(int t);
void scan(void);
void sound(char a, char b,char c);
void input(char x);
void displayon(void);
void displaywrong(void);
void displayagain(void);
void displayinput(void);
void jiemi(void);
void delayms(int t)
{
int i,j;
for(i=0;i<t;i++)
for(j=0;j<120;j++)
;
}
void scan(void)
{
unsigned char k;
for(k=0;k<6;k++)
{P0=dis_7[dia[k]];
P2=scan_con[k];
delayms(1);
P0=0xff;}
}
void sound(char a, char b,char c)
{
char i, j,k;
for(k=0;k<c;k++)
{
for(i=0;i<a;i++)
for(j=0;j<100;j++)
{
P37=!P37;
delayms(b);
}
delayms(700);
}
}
void input(char x)
{
char i,j=0;
for(i=0;i<6;i++,j=0)
while(j==0)
{
scan();
if(P10==0)
{
while(P10==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=0;
j=1;
dat1[x][i]=0;
}
if(P11==0)
{
while(P11==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=1;
j=1;
dat1[x][i]=1;
}
if(P12==0)
{
while(P12==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=2;
j=1;
dat1[x][i]=2;
}
if(P13==0)
{
while(P13==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=3;
j=1;
dat1[x][i]=3;
}
if(P14==0)
{
while(P14==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=4;
j=1;
dat1[x][i]=4;
}
if(P30==0)
{
while(P30==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=5;
j=1;
dat1[x][i]=5;
}
if(P31==0)
{
while(P31==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=6;
j=1;
dat1[x][i]=6;
}
if(P32==0)
{
while(P32==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=7;
j=1;
dat1[x][i]=7;
}
if(P33==0)
{
while(P33==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=8;
j=1;
dat1[x][i]=8;
}
if(P34==0)
{
while(P34==0) scan();
if(x==0)
dia[i]=13;
else
dia[i]=9;
j=1;
dat1[x][i]=9;
}
}
void displayon(void)
{
dia[0]=0x00;dia[1]=0x0b;dia[2]=0x0c;
dia[3]=0x0c;dia[4]=0x0c;dia[5]=0x0c;
scan();
}
void displaywrong(void)
{
dia[0]=0x0e;dia[1]=0x0f;dia[2]=0x0f;
dia[3]=0x0e;dia[4]=0x0f;dia[5]=0x0f;
scan();
}
void displayinput(void)
{
dia[0]=0x0a;dia[1]=0x0a;dia[2]=0x0a;
dia[3]=0x0a;dia[4]=0x0a;dia[5]=0x0a;
scan();
}
void displayagain(void)
{
int n;
dia[0]=0x0e;dia[1]=0x0f;dia[2]=0x0f;
dia[3]=0x0c;dia[4]=0x0c;dia[5]=0x0c;
for(n=0;n<500;n++) scan();
dia[0]=0x0a;dia[1]=0x0a;dia[2]=0x0a;
dia[3]=0x0a;dia[4]=0x0a;dia[5]=0x0a;
scan();
}
void jiemi(void)
{
char i, j,m=0;
for(i=0;i<3;i++)
{
input(1);
for(j=0;j<6;j++)
{
if(dat1[0][j]!=dat1[1][j])
{
m=1;
break;
}
}
if(m==1)
{
if(i<2)
{
sound(3,3,3);
displayagain();
m =0;
}
else
{
sound(6,1,6);
flag=1;
displaywrong();
}
}
else if(m==0)
{
sound(2,2,2);
sound(5,1,1);
P36=0;
displayon();
flag1=1;
break;
}
}
}
void main(void)
{
char i;
init();
for(i=0;i<6;i++)
{
dat1[0][i]=read_add(00+i);
delay1(100);
}
while(1)
{
scan();
if(P35==0)
{
if(flag==0)
{
P36=1;
displayinput();
jiemi();
}
}
if(P17==0)
{
while(P17==0) scan;
if(flag==0)
{
displayinput();
input(0);
for(i=0;i<6;i++)
{
write_add(00+i,dat1[0][i]);
delay1(100);
}
if(flag1==1)
displayon();
else
displayinput();
}
}
}
}
//*****************************************************//
// AT24C01//
//#ifndef_24c01_H_
//#define_24c01_H_
//#include"24c01.h"
void delay()
{ ;; }
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void init()
{
sda=1;
scl=1;
}
void write_byte(uchar date)
{
uchar i;
scl=0;
for(i=0;i<8;i++)
{
date=date<<1;
sda=CY;
scl=1;
delay();
scl=0;
delay();
}
}
uchar read_byte()
{
uchar i,k;
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl = 0;
delay();
}
return k;
}
void delay1(uchar x)
{
uchar a,b;
for(a=x;a>0;a--)
for(b=100;b>0;b--)
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xal);
respons();
date=read_byte();
stop();
return date;
}
|