|
向epprom写入数值2,读取之后数码管显示,再把数值加1,重复写入读取,显示这个步骤,一直加到99,为什么数码管却一直显示刚开始的数值
2,求各位前辈指点!
下面把代码附上:
————————————————————————————————————————————————————————
#include <reg52.h>
#include <intrins.h>
sbit SCL=P3^4;
sbit SDA=P3^5;
bit Ackflag;
char k;
char code duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
char code we[]={0xfe,0xfd};
void delay5us()
{
_nop_();
}
void init()
{
SCL=1;
delay5us();
SDA=1;
delay5us();
}
void I2c_start()
{
SCL=1;
delay5us();
SDA=1;
delay5us();
SDA=0;
delay5us();
}
void I2c_stop()
{
SCL=0;
SDA=0;
delay5us();
SCL=1;
delay5us();
SDA=1;
delay5us();
}
bit Read_Ack()
{
SCL=0;
SCL=1;
delay5us();
if(SDA)
{
SCL=0;
return 1;
}
else
{
SCL=0;
return 0;
}
}
void Send_Ack(bit i)
{
SCL=0;
if(i)
SDA=1;
else
SDA=0;
SCL=1;
delay5us();
SCL=0;
SDA=1;
}
void I2c_sendbyte(char dat)
{
char i;
for(i=0;i<8;i++)
{
SCL=0;
if(dat&0x80)
SDA=1;
else
SDA=0;
SCL=1;
dat=dat<<1;
}
SCL=0;
SDA=1;
}
char I2c_readbyte()
{
char i,dat;
for(i=0;i<8;i++)
{
dat<<=1;
SCL=0;
SCL=1;
if(SDA)
dat|=0x01;
}
return(dat);
}
void Write(char addr,char dat)
{
I2c_start();
I2c_sendbyte(0xa0);//send a byte address
if(Read_Ack())
Ackflag=1;
else
Ackflag=0;
I2c_sendbyte(addr);
if(Read_Ack())
Ackflag=1;
else
Ackflag=0;
I2c_sendbyte(dat);
if(Read_Ack())
Ackflag=1;
else
Ackflag=0;
I2c_stop();
}
char Read(char addr)
{
char dat;
I2c_start();
I2c_sendbyte(0xa0);//send a byte address
if(Read_Ack())
Ackflag=1;
else
Ackflag=0;
I2c_sendbyte(addr);
Read_Ack();
I2c_start();
I2c_sendbyte(0xa1); //read 1
if(Read_Ack())
Ackflag=1;
else
Ackflag=0;
dat=I2c_readbyte();
Send_Ack(1);
I2c_stop();
return (dat);
}
void delay(char z)
{
char x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(char shi,char ge)
{
while(1)
{
P2=0xfd;
P0=duan[ge];
delay(5);
P2=0xfe;
P0=duan[shi];
delay(5);
}
}
void main()
{
char num;
init();
TMOD=0x01;
EA=1;
ET0=1;
TH0=(65536-46080)/256;
TL0=(65536-46080)%256;
TR0=1;
for(k=2;k<100;k++)
{
Write(2,k);
delay(1);
num=Read(2);
if(Ackflag)
P1=0;
else
P1=0xff;
display(num/10,num%10);
}
}
|
|