帮忙看一下程序哪里有问题,谢谢了,I2C的问题
时间:10-02
整理:3721RD
点击:
include<reg51.h>
#define uint unsigned int
sbit scl=P2^0;
sbit sda=P2^1;
void delay1(uint);
uint i;
void init()
{
scl=1;
sda=1;
}
void start()
{
scl=0;
sda=1;
delay1(4);
scl=1;
delay1(4);
sda=0;
delay1(2);
}
void stop()
{
scl=0;
sda=0;
delay1(4);
scl=1;
delay1(4);
sda=1;
delay1(2);
}
void reponed()
{
uint x;
delay1(2);
scl=1;
delay1(4);
while((sda==1)&&(x<250))
{
x++;
}
}
void write_ad(uint x)
{
uint z;
for(z=0;z<8;z++)
{
x=x<<1;
scl=0;
delay1(2);
sda=CY;
delay1(2);
scl=1;
delay1(2);
}
scl=0;
sda=1;
reponed();
}
unsigned long read_da()
{
uint x;
uint z;
for(z=0;z<8;z++)
{
scl=0;
delay1(2);
scl=1;
delay1(1);
x=(x<<1)|sda;
}
scl=0;
delay1(1);
sda=0;
scl=1;
delay1(1);
return x;
}
void main()
{
init();
while(1)
{
start();
write_ad(0xa0);
write_ad(2);
write_ad(0xaa);
stop();
delay1(2000);
start();
write_ad(0xa0);
write_ad(2);
start();
write_ad(0xa1);
i=read_da();
stop();
P0=i;
}
}
void delay1(uint x)
{
while(x-->>0);
}
#define uint unsigned int
sbit scl=P2^0;
sbit sda=P2^1;
void delay1(uint);
uint i;
void init()
{
scl=1;
sda=1;
}
void start()
{
scl=0;
sda=1;
delay1(4);
scl=1;
delay1(4);
sda=0;
delay1(2);
}
void stop()
{
scl=0;
sda=0;
delay1(4);
scl=1;
delay1(4);
sda=1;
delay1(2);
}
void reponed()
{
uint x;
delay1(2);
scl=1;
delay1(4);
while((sda==1)&&(x<250))
{
x++;
}
}
void write_ad(uint x)
{
uint z;
for(z=0;z<8;z++)
{
x=x<<1;
scl=0;
delay1(2);
sda=CY;
delay1(2);
scl=1;
delay1(2);
}
scl=0;
sda=1;
reponed();
}
unsigned long read_da()
{
uint x;
uint z;
for(z=0;z<8;z++)
{
scl=0;
delay1(2);
scl=1;
delay1(1);
x=(x<<1)|sda;
}
scl=0;
delay1(1);
sda=0;
scl=1;
delay1(1);
return x;
}
void main()
{
init();
while(1)
{
start();
write_ad(0xa0);
write_ad(2);
write_ad(0xaa);
stop();
delay1(2000);
start();
write_ad(0xa0);
write_ad(2);
start();
write_ad(0xa1);
i=read_da();
stop();
P0=i;
}
}
void delay1(uint x)
{
while(x-->>0);
}