求助IIC!断电后无法记住输入的数字,但能记住位次
时间:10-02
整理:3721RD
点击:
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda = P2^0;
sbit scl = P2^1;
sbit dula = P2^6;
sbit wela = P2^7;
uchar code tabledu[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0};
uchar code tablewe[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uint x,num,temp,yi,wan,qian,bai,shi,ge;
void delay1()
{;;}
void delay(uint z)
{
uint x, y;
for(x=z; x>0; x--)
for(y=110; y>0; y--);
}
void start()
{
sda = 1;
delay1();
scl = 1;
delay1();
sda = 0;
delay1();
}
void stop()
{
sda = 0;
delay1();
scl = 1;
delay1();
sda = 1;
delay1();
}
void response()
{
uchar i;
scl = 1;
delay1();
while((sda == 1) && (i<250)) i++;
scl = 0;
delay1();
}
void init()
{
sda = 1;
delay1();
scl = 1;
delay1();
}
void write_byte(uchar date)
{
uchar i, temp;
temp = date;
for(i=0; i<8; i++)
{
temp = temp << 1;
scl = 0;
delay1();
sda = CY;
delay1();
scl = 1;
delay1();
}
scl = 0;
delay1();
sda = 1;
delay1();
}
uchar read_byte()
{
uchar i, k;
scl = 0;
delay1();
sda = 1;
delay1();
for(i=0; i<8; i++)
{
scl = 1;
delay1();
k = (k<<1) | sda;
scl = 0;
delay1();
}
return k;
}
void write_add(uchar address, uchar date)
{
start();
write_byte(0xa0);
response();
write_byte(address);
response();
write_byte(date);
response();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
response();
write_byte(address);
response();
start();
write_byte(0xa1);
response();
date = read_byte();
stop();
return date;
}
void keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //按下第一排的按键
{
delay(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=1; break;
case 0xd0: num=2; break;
case 0xb0: num=3; break;
case 0x70: break;
}
x++;
while(P3!=0xfe);
delay(5);
while(P3!=0xfe);
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //按下第二排的按键
{
delay(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=4; break;
case 0xd0: num=5; break;
case 0xb0: num=6;break;
case 0x70: break;
}
x++;
while(P3!=0xfd);
delay(5);
while(P3!=0xfd);
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //按下第三排的按键
{
delay(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=7; break;
case 0xd0: num=8; break;
case 0xb0: num=9; break;
case 0x70: break;
}
x++;
while(P3!=0xfb);
delay(5);
while(P3!=0xfb);
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //按下第四排按键
{
delay(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: wela=1;P0=0xff;wela=0;x=0;break; //0键
case 0xd0: num=0; x++; break; //清零
}
while(P3!=0xf7);
}
}
}
void answer()
{
if(x==1)
{
yi=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
}
if(x==2)
{
wan=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
}
if(x==3)
{
qian=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
dula=1;
P0=tabledu[qian];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[2];
wela=0;
delay(1);
}
if(x==4)
{
bai=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
dula=1;
P0=tabledu[qian];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[2];
wela=0;
delay(1);
dula=1;
P0=tabledu[bai];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[3];
wela=0;
delay(1);
}
if(x==5)
{
shi=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
dula=1;
P0=tabledu[qian];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[2];
wela=0;
delay(1);
dula=1;
P0=tabledu[bai];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[3];
wela=0;
delay(1);
dula=1;
P0=tabledu[shi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[4];
wela=0;
delay(1);
}
if(x==6)
{
ge=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
dula=1;
P0=tabledu[qian];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[2];
wela=0;
delay(1);
dula=1;
P0=tabledu[bai];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[3];
wela=0;
delay(1);
dula=1;
P0=tabledu[shi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[4];
wela=0;
delay(1);
dula=1;
P0=tabledu[ge];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[5];
wela=0;
delay(1);
}
}
void main()
{
init();
x=read_add(2);
delay(100);
while(1)
{
write_add(2,x);
keyscan();
answer();
}
}
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda = P2^0;
sbit scl = P2^1;
sbit dula = P2^6;
sbit wela = P2^7;
uchar code tabledu[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0};
uchar code tablewe[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uint x,num,temp,yi,wan,qian,bai,shi,ge;
void delay1()
{;;}
void delay(uint z)
{
uint x, y;
for(x=z; x>0; x--)
for(y=110; y>0; y--);
}
void start()
{
sda = 1;
delay1();
scl = 1;
delay1();
sda = 0;
delay1();
}
void stop()
{
sda = 0;
delay1();
scl = 1;
delay1();
sda = 1;
delay1();
}
void response()
{
uchar i;
scl = 1;
delay1();
while((sda == 1) && (i<250)) i++;
scl = 0;
delay1();
}
void init()
{
sda = 1;
delay1();
scl = 1;
delay1();
}
void write_byte(uchar date)
{
uchar i, temp;
temp = date;
for(i=0; i<8; i++)
{
temp = temp << 1;
scl = 0;
delay1();
sda = CY;
delay1();
scl = 1;
delay1();
}
scl = 0;
delay1();
sda = 1;
delay1();
}
uchar read_byte()
{
uchar i, k;
scl = 0;
delay1();
sda = 1;
delay1();
for(i=0; i<8; i++)
{
scl = 1;
delay1();
k = (k<<1) | sda;
scl = 0;
delay1();
}
return k;
}
void write_add(uchar address, uchar date)
{
start();
write_byte(0xa0);
response();
write_byte(address);
response();
write_byte(date);
response();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
response();
write_byte(address);
response();
start();
write_byte(0xa1);
response();
date = read_byte();
stop();
return date;
}
void keyscan()
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //按下第一排的按键
{
delay(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=1; break;
case 0xd0: num=2; break;
case 0xb0: num=3; break;
case 0x70: break;
}
x++;
while(P3!=0xfe);
delay(5);
while(P3!=0xfe);
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //按下第二排的按键
{
delay(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=4; break;
case 0xd0: num=5; break;
case 0xb0: num=6;break;
case 0x70: break;
}
x++;
while(P3!=0xfd);
delay(5);
while(P3!=0xfd);
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //按下第三排的按键
{
delay(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=7; break;
case 0xd0: num=8; break;
case 0xb0: num=9; break;
case 0x70: break;
}
x++;
while(P3!=0xfb);
delay(5);
while(P3!=0xfb);
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //按下第四排按键
{
delay(10);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: wela=1;P0=0xff;wela=0;x=0;break; //0键
case 0xd0: num=0; x++; break; //清零
}
while(P3!=0xf7);
}
}
}
void answer()
{
if(x==1)
{
yi=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
}
if(x==2)
{
wan=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
}
if(x==3)
{
qian=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
dula=1;
P0=tabledu[qian];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[2];
wela=0;
delay(1);
}
if(x==4)
{
bai=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
dula=1;
P0=tabledu[qian];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[2];
wela=0;
delay(1);
dula=1;
P0=tabledu[bai];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[3];
wela=0;
delay(1);
}
if(x==5)
{
shi=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
dula=1;
P0=tabledu[qian];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[2];
wela=0;
delay(1);
dula=1;
P0=tabledu[bai];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[3];
wela=0;
delay(1);
dula=1;
P0=tabledu[shi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[4];
wela=0;
delay(1);
}
if(x==6)
{
ge=num;
dula=1;
P0=tabledu[yi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[0];
wela=0;
delay(1);
dula=1;
P0=tabledu[wan];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[1];
wela=0;
delay(1);
dula=1;
P0=tabledu[qian];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[2];
wela=0;
delay(1);
dula=1;
P0=tabledu[bai];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[3];
wela=0;
delay(1);
dula=1;
P0=tabledu[shi];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[4];
wela=0;
delay(1);
dula=1;
P0=tabledu[ge];
dula=0;
P0=0xff;
wela=1;
P0=tablewe[5];
wela=0;
delay(1);
}
}
void main()
{
init();
x=read_add(2);
delay(100);
while(1)
{
write_add(2,x);
keyscan();
answer();
}
}
用郭天祥的板子做的STC89C52