求大神帮忙验证一下这个程序对不对
时间:10-02
整理:3721RD
点击:
这是个利用IIC总线EEPROM的程序,以前写得,记得当初写得时候程序是管用的,现在怎么不管用的。难道是我的EEPROM芯片坏了吗? 大家帮忙看一下或试一下这个程序对不对?谢谢啦~~~PS:单片机是stc89c52
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
uchar a;
sbit SDA=P1^2;
sbit SCL=P1^7;
void delay()
{ _nop_();_nop_(); }
void delay1(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
void init()
{
SDA=1;
SCL=1;
}
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 urespons()
{
SDA=1;
delay();
SCL=1;
delay();
SCL=0;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
SCL=0;
delay();
for(i=0;i<8;i++)
{
temp=temp<<1;
SDA=CY;
delay();
SCL=1;
delay();
SCL=0;
delay();
}
SDA=1;
delay();
}
uchar read_byte()
{
uchar i,j,k;
SCL=0;
delay();
SDA=1;
delay();
for(i=0;i<8;i++)
{
SCL=1;
delay();
if(SDA==1)
j=1;
else
j=0;
k=(k<<1)|j;
SCL=0;
delay();
}
return k;
}
void main()
{
init();
start();
write_byte(0xa0);
respons();
write_byte(3);
respons();
write_byte(0x55);
respons();
stop();
delay1(100);
init();
start();
write_byte(0xa0);
respons();
write_byte(3);
respons();
start();
write_byte(0xa1);
respons();
a=read_byte();
urespons();
stop();
P1=a;
while(1);
}
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
uchar a;
sbit SDA=P1^2;
sbit SCL=P1^7;
void delay()
{ _nop_();_nop_(); }
void delay1(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=100;y>0;y--);
}
void init()
{
SDA=1;
SCL=1;
}
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 urespons()
{
SDA=1;
delay();
SCL=1;
delay();
SCL=0;
delay();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
SCL=0;
delay();
for(i=0;i<8;i++)
{
temp=temp<<1;
SDA=CY;
delay();
SCL=1;
delay();
SCL=0;
delay();
}
SDA=1;
delay();
}
uchar read_byte()
{
uchar i,j,k;
SCL=0;
delay();
SDA=1;
delay();
for(i=0;i<8;i++)
{
SCL=1;
delay();
if(SDA==1)
j=1;
else
j=0;
k=(k<<1)|j;
SCL=0;
delay();
}
return k;
}
void main()
{
init();
start();
write_byte(0xa0);
respons();
write_byte(3);
respons();
write_byte(0x55);
respons();
stop();
delay1(100);
init();
start();
write_byte(0xa0);
respons();
write_byte(3);
respons();
start();
write_byte(0xa1);
respons();
a=read_byte();
urespons();
stop();
P1=a;
while(1);
}