I2C数据传输的这个程序是什么地方错了啊
时间:10-02
整理:3721RD
点击:
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^0;
sbit scl=P2^1;
uchar b,temp1,temp2;
void delay()
{
uint i;
while(i==50) i--;
}
void delay1(uint m)
{
uint i,j;
for(i=m;i>0;i--)
for(j=1000;j>0;j--);
}
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 write(uchar m)//写入数据
{
uchar j;
temp1=m;
for(j=0;j<8;j++)
{
temp1=temp1<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
// scl=0;
// delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar output()//读出数据
{
uchar i;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
temp2=(temp2<<1)|sda;
delay();
scl=0;
delay();
}
return temp2;
}
void responed()//应答
{
uint i;
scl=1;
delay();
while((sda==1)&&(i==250))
i++;
scl=0;
delay();
}
void main()
{
init();
start();
write(0xa0);
responed();
write(3);
responed();
write(0xaa);
responed();
stop();
delay1(500);
start();
write(0xa0);
responed();
write(3);
responed();
start();
write(0xa1);
responed();
b=output();
stop();
P0=b;
delay1(10);
while(1);
}
大神们!求解啊~~~
#define uchar unsigned char
#define uint unsigned int
sbit sda=P2^0;
sbit scl=P2^1;
uchar b,temp1,temp2;
void delay()
{
uint i;
while(i==50) i--;
}
void delay1(uint m)
{
uint i,j;
for(i=m;i>0;i--)
for(j=1000;j>0;j--);
}
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 write(uchar m)//写入数据
{
uchar j;
temp1=m;
for(j=0;j<8;j++)
{
temp1=temp1<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
// scl=0;
// delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar output()//读出数据
{
uchar i;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
temp2=(temp2<<1)|sda;
delay();
scl=0;
delay();
}
return temp2;
}
void responed()//应答
{
uint i;
scl=1;
delay();
while((sda==1)&&(i==250))
i++;
scl=0;
delay();
}
void main()
{
init();
start();
write(0xa0);
responed();
write(3);
responed();
write(0xaa);
responed();
stop();
delay1(500);
start();
write(0xa0);
responed();
write(3);
responed();
start();
write(0xa1);
responed();
b=output();
stop();
P0=b;
delay1(10);
while(1);
}
大神们!求解啊~~~
那知道什么地方错了,对照时序看啊!
void responed()//应答
{
uint i;
scl=1;
delay();
while((sda==1)&&(i<250))
i++;
scl=0;
delay();
}
这样试试
哈哈
时序应该没错的~~~~我是按照郭天祥的视频里复制出来的但是他的行我的却不行啊,他电路板的原理图里面的24c02和我的那块电路板的原理图里的线路接法都差不多~~~
这个和我那里面写的有区别吗
程序号麻烦额~~~错了都不知道哪里错了额~~~
我写的IIC操作。
http://bbs.elecfans.com/jishu_270523_1_1.html
我写的EEPROM全系列驱动。
http://bbs.elecfans.com/jishu_270766_1_1.html
你试试看啊,真的没区别么?
个风格的话