各们大哥帮帮忙,昨天花了一天时间都没有找出错误
时间:10-02
整理:3721RD
点击:
I2C总线关键是什么地方不能出错啊
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
sbit sda=P2^0;
sbit scl=P2^1;
void nop()
{
_nop_();_nop_();
}
void delay(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=125;j>0;j--);
}
void init()
{
scl=1;
nop();
sda=1;
nop();
}
void start()
{
scl=1;
nop();
sda=1;
nop();
sda=0;
nop();
scl=0;
}
void stop()
{
sda=0;
nop();
scl=1;
nop();
sda=1;
nop();
scl=0;
}
void respond()
{
uchar i;
scl=1;
nop();
while(sda==1&&i<255)i++;
scl=0;
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp<<=1;
scl=0;
nop();
sda=CY;
nop();
scl=1;
nop();
}
scl=0;
nop();
sda=1;
nop();
}
uchar read_byte()
{
uchar i,k,j;
scl=0;
nop();
sda=1;
for(i=0;i<8;i++)
{
scl=1;
nop();
if(sda==1)
j=1;
else
j=0;
k=(k<<1)|j;
scl=0;
nop();
}
return k;
}
void main()
{
uchar y;
init();
start();
respond();
write_byte(0xa0);
respond();
write_byte(8);
respond();
write_byte(0xd7);
respond();
stop();
delay(100);
start();
write_byte(0xa0);
respond();
write_byte(8);
respond();
start();
write_byte(0xa1);
respond();
y=read_byte();
stop();
delay(100);
P1=y;
while(1);
}
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
sbit sda=P2^0;
sbit scl=P2^1;
void nop()
{
_nop_();_nop_();
}
void delay(uchar x)
{
uchar i,j;
for(i=x;i>0;i--)
for(j=125;j>0;j--);
}
void init()
{
scl=1;
nop();
sda=1;
nop();
}
void start()
{
scl=1;
nop();
sda=1;
nop();
sda=0;
nop();
scl=0;
}
void stop()
{
sda=0;
nop();
scl=1;
nop();
sda=1;
nop();
scl=0;
}
void respond()
{
uchar i;
scl=1;
nop();
while(sda==1&&i<255)i++;
scl=0;
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp<<=1;
scl=0;
nop();
sda=CY;
nop();
scl=1;
nop();
}
scl=0;
nop();
sda=1;
nop();
}
uchar read_byte()
{
uchar i,k,j;
scl=0;
nop();
sda=1;
for(i=0;i<8;i++)
{
scl=1;
nop();
if(sda==1)
j=1;
else
j=0;
k=(k<<1)|j;
scl=0;
nop();
}
return k;
}
void main()
{
uchar y;
init();
start();
respond();
write_byte(0xa0);
respond();
write_byte(8);
respond();
write_byte(0xd7);
respond();
stop();
delay(100);
start();
write_byte(0xa0);
respond();
write_byte(8);
respond();
start();
write_byte(0xa1);
respond();
y=read_byte();
stop();
delay(100);
P1=y;
while(1);
}