IIC连续读的问题,请大家帮忙看一下这个程序为什么只能读一个字节,哪里错了,,还有程序有什么不太好的地方可以修改,不要重新写的,要在我原有的程序修改并注释,这样我才能看的懂,谢谢
#include"STC89C5xRC.H"
#define uchar unsigned char
#define uint unsigned int
uchar buf[4];
sbit scl=P3^6;
sbit sda=P3^7;
void start();
void Delay1(uchar);
void delay5us();
//void IIC_write(uchar ,uchar );
uchar IIC_read(uchar );
void smg(uint);
void AT24C02_writeData(uchar ,uchar ,uchar*);
void AT24C02_readData(uchar ,uchar );
void main (void)
{
uchar arr[4]={3,4,5,6};
start();
AT24C02_writeData(0x00,sizeof(arr),arr);
Delay1(5);
AT24C02_readData(0,2);
smg(buf[1]);
}
void start()
{
sda=1;
scl=1;
delay5us();
sda=0;
delay5us();
scl=0;
}
void stop()
{
sda=0;
delay5us();
scl=1;
delay5us();
sda=1;
delay5us();
}
void ack()
{
uchar i=0;
scl=1;
delay5us();
while((sda==1)&&(i<255))
{
i++;
}
delay5us();
scl=0;
}
void Send_ack()
{
scl=0;
sda=0;
delay5us();
scl=1;
delay5us();
scl=0;
}
void nack()
{
scl=1;
delay5us();
sda=1;
delay5us();
scl=0;
delay5us();
}
void write_1(uchar i)
{
uchar j,temp;
temp=i;
for(j=8;j>0;j--)
{
temp=temp<<1;
scl=0;
delay5us();
sda=CY;
delay5us();
scl=1;
delay5us();
}
scl=0;
delay5us();
sda=1;
delay5us();
}
uchar read_1()
{
uchar i,j,k;
scl = 0;
delay5us();
for(i=0; i<8; i++)
{
scl = 1;
delay5us();
if(sda==1) j = 1;
else j = 0;
k = (k<<1)|j;
scl = 0;
delay5us();
}
delay5us();
return(k);
}
void IIC_write(uchar address,uchar date)
{
start();
write_1(0xa0);
ack();
write_1(address);
ack();
write_1(date);
ack();
stop();
Delay1(5);
}
/*uchar IIC_read(uchar address)
{
uchar i;
start();
write_1(0xa0);
ack();
write_1(address);
ack();
start();
write_1(0xa1);
ack();
i = read_1();
nack();
stop();
Delay1(1);
return(i);
} */
void delay5us()
{
; ; ;
}
void Delay1(uchar k) //Ñóê±5ms
{
uchar i, j;
for(i=(18*k);i>0;i--)
{
for(j=255;j>0;j--)
{}
}
}
void AT24C02_writeData(uchar address,uchar numBytes,uchar* buf)
{
while(numBytes--)
{
IIC_write(address++,*buf++);
}
}
void AT24C02_readData(uchar beginAddr, uchar dataSize)
{
uchar i=0;
start();
write_1(0xa0);
ack();
write_1(beginAddr);
ack();
start();
write_1(0xa1);
ack();
while(dataSize--)
{
*(buf+i)=read_1();
i++;
Send_ack();
}
nack();
stop();
Delay1(1);
}
|