#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit p00=P0^0;sbit p01=P0^1;sbit p02=P0^2;sbit p03=P0^3;sbit p04=P0^4;sbit p05=P0^5;sbit p06=P0^6;sbit p07=P0^7;
sbit p10=P1^0;sbit p11=P1^1;sbit p12=P1^2;sbit p13=P1^3;sbit p14=P1^4;sbit p15=P1^5;sbit p16=P1^6;sbit p17=P1^7;
sbit p20=P2^0;sbit p21=P2^1;sbit p22=P2^2;sbit p23=P2^3;sbit p24=P2^4;sbit p25=P2^5;sbit p26=P2^6;sbit p27=P2^7;
sbit sda=P3^0;sbit scl=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;
uchar num,temp,num1;
uchar a,cf,flag,ra;
void ledscan();
void delay() //空操作5us延时
{ ;; }
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() //初始化
{
EA=1;
IT0=0;
IT1=0;
EX0=1;
EX1=1;
flag=0;
sda=1;
cf=1;
delay();
scl=1;
delay();
}
void write_byte(uchar date) //写数据
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
// scl=0;
// delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte() //读数据
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void delay1(uchar x) //延时程序,防止读写之前的冲突。1ms,,,延时时取100
{
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(0xa1);
respons();
date=read_byte();
stop();
return date;
}
uchar keyscan();
void display(uchar aa);
void main()
{
init();
while(1)
{
keyscan();
ledscan();
}
}
uchar keyscan()
{
P2=0xfe;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay1(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xee:
{
p00=0;write_add(23,0x11);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0xde:
{
p01=0;write_add(23,0x12);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0xbe:
{
p02=0;write_add(23,0x22);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0x7e:
{
p03=0;write_add(23,0x13);delay1(100);p36=0;delay1(100);p36=1;
}
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
P2=0xfd;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay1(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xed:
{
p04=0;write_add(23,0x23);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0xdd:
{
p05=0;write_add(23,0x14);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0xbd:
{
p06=0;write_add(23,0x24);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0x7d:
{
p07=0;write_add(23,0x15);delay1(100);p36=0;delay1(100);p36=1;
}
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
P2=0xfb;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay1(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xeb:
{
p10=0;write_add(23,0x25);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0xdb:
{
p11=0;write_add(23,0x26);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0xbb:
{
p12=0;write_add(23,0x31);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0x7b:
{
p13=0;write_add(23,0x32);delay1(100);p36=0;delay1(100);p36=1;
}
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
P2=0xf7;
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay1(5);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P2;
switch(temp)
{
case 0xe7:
{
p14=0;write_add(23,0x33);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0xd7:
{
p15=0;write_add(23,0x34);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0xb7:
{
p16=0;write_add(23,0x35);delay1(100);p36=0;delay1(100);p36=1;
}
break;
case 0x77:
{
p17=0;write_add(23,0x36);delay1(100);p36=0;delay1(100);p36=1;
}
break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
}
return num;
}
void ledscan ()//扫描熄灯
{
switch (cf)
{
case 1: {p12=1;p00=1;}break;
case 2: p13=1;break;
case 3: p14=1;break;
case 4: p15=1;break;
case 5: p16=1;break;
case 6: {p17=1;p11=1;}break;
}
if (flag==1)
{
switch (cf)
{
case 2: p01=1;break;
case 3: p03=1;break;
case 4: p05=1;break;
case 5: p07=1;break;
}
}
if (flag==2)
{
switch (cf)
{
case 2: p02=1;break;
case 3: p04=1;break;
case 4: p06=1;break;
case 5: p10=1;break;
}
}
}
void currentfloor() interrupt 0
{
delay1(100);
ra=read_add(22);
cf=(ra&0x0f);
switch(ra&0xf0)
{
case 0x10:flag=1;break;
case 0x20:flag=2;break;
case 0x00:flag=0;break;
}
while(INT0==0);
}