51单片机 24C02的读写两个变量操作,为什么第一个成功,第二个失败呢?
voiddelay(){;;}//开始信号voidstart(){sda=1;delay();scl=1;delay();sda=0;delay();}//停止信号voi...
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();
}
//向24c02写一个字节数据
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();
}
//从24c02读一个字节数据
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(unsigned int x)
{
unsigned int 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;
}
void init()
{
sda=1;
delay();
scl=1;
delay();
P1=0xff;
P3=0xff;
dip=read_add(3);
P3=0xff;
delay();
ff=read_add(4);
dip=read_add(5);
TMOD=0x11; //中断方式
TH0 =(65536-20000)/256;
TL0 =(65536-20000)%256;
TH1 =(65536-50000)/256;
TL1 =(65536-50000)%256;
EA =1;
EX0=0;
IT0=1;
ET0=1;
TR0=0;
ET1=1;
TR1=1;
}
if(bb==1)
{
TR0=0;
TR1=0;
write_add(3,dip);
write_add(4,ff);
TR1=1;
变量dip读写正常
变量ff无法正常读写何解??? 展开
{ ;; }
//开始信号
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();
}
//向24c02写一个字节数据
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();
}
//从24c02读一个字节数据
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(unsigned int x)
{
unsigned int 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;
}
void init()
{
sda=1;
delay();
scl=1;
delay();
P1=0xff;
P3=0xff;
dip=read_add(3);
P3=0xff;
delay();
ff=read_add(4);
dip=read_add(5);
TMOD=0x11; //中断方式
TH0 =(65536-20000)/256;
TL0 =(65536-20000)%256;
TH1 =(65536-50000)/256;
TL1 =(65536-50000)%256;
EA =1;
EX0=0;
IT0=1;
ET0=1;
TR0=0;
ET1=1;
TR1=1;
}
if(bb==1)
{
TR0=0;
TR1=0;
write_add(3,dip);
write_add(4,ff);
TR1=1;
变量dip读写正常
变量ff无法正常读写何解??? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询