回复【9楼】DADONG:
---------------------------------
void IIC_Start(void)
{
SDA_OUT();
IIC_SDA = 1;
IIC_SCL = 1;
delay_us(4);
IIC_SDA = 0;
delay_us(4);
IIC_SCL = 0;
}
void IIC_Stop(void)
{
SDA_OUT();
IIC_SCL = 0;
IIC_SDA = 0;
delay_us(4);
IIC_SCL = 1;
IIC_SDA = 1;
delay_us(4);
}
uchar IIC_Wait_Ack(void)
{
uchar errtime=0;
SDA_IN();
IIC_SDA = 1;
delay_us(1);
IIC_SCL = 1;
delay_us(1);
while(READ_SDA)
{
errtime++;
if(errtime>250)
{
//IIC_Stop();
return 1;
}
}
IIC_SCL = 0;
return 0;
}
void IIC_Ack(void)
{
//IIC_SCL = 0;
SDA_OUT();
IIC_SDA = 0;
delay_us(5);
IIC_SCL = 1;
delay_us(5);
IIC_SCL = 0;
delay_us(5);
}
void IIC_NAck(void)
{
//IIC_SCL = 0;
SDA_OUT();
IIC_SDA = 1;
delay_us(5);
IIC_SCL = 1;
delay_us(5);
IIC_SCL = 0;
delay_us(5);
}
void IIC_Send(uchar IICbyte)
{
uchar t=0;
SDA_OUT();
IIC_SCL = 0;
for(t=0;t<8;t++)
{
IIC_SDA = (IICbyte&0x80)>>7;
IICbyte <<= 1;
delay_us(2);
IIC_SCL = 1;
delay_us(2);
IIC_SCL = 0;
delay_us(2);
}
}
uchar IIC_Read(void)
{
uchar i,receive=0;
SDA_IN();
for(i=0;i<8;i++)
{
IIC_SCL = 0;
delay_us(2);
IIC_SCL = 1;
receive <<= 1;
if(READ_SDA)
receive++;
delay_us(1);
}
//if(!ack)
//IIC_NAck();
//else
//IIC_Ack();
return receive;
}
void Write_Onebyte(uint addr,uchar data)
{
IIC_Start();
IIC_Send(0xa0);
IIC_Wait_Ack();
IIC_Send(addr>>8);
IIC_Wait_Ack();
IIC_Send(addr%256);
IIC_Wait_Ack();
IIC_Send(data);
IIC_Wait_Ack();
IIC_Stop();
delay_ms(5); //???????±10ms?????????????ò??·¨????10ms???????±5ms???é
}
uchar Read_Onebyte(uint addr)
{
uchar temp = 0;
IIC_Start();
IIC_Send(0xa0);
IIC_Wait_Ack();
IIC_Send(addr/256);
//IIC_Send(addr>>8);
IIC_Wait_Ack();
IIC_Send(addr%256);
IIC_Wait_Ack();
IIC_Start();
IIC_Send(0xa1);
IIC_Wait_Ack();
temp = IIC_Read();
IIC_Stop();
return temp;
}
void Write_Longbytes(uint addr,uchar *table,uchar len)
{
uchar t=0;
IIC_Start();
IIC_Send(0xa0);
IIC_Wait_Ack();
IIC_Send(addr>>8);
IIC_Wait_Ack();
IIC_Send(addr%256);
IIC_Wait_Ack();
for(t=0;t
{
IIC_Send(table[t]);
IIC_Wait_Ack();
delay_us(1);
}
IIC_Stop();
delay_ms(8);
}
这个在网上应该资料挺多的啊,上面是我AT24C512的关于IIC的函数,至于IO口根据自己硬件配置一下,程序在512可以使用,对于256、64等应该兼容>