程序如下:
void twi_init()
{
TWBR = 0x02;
TWAR = 0xD1;
TWCR = 0x44;
TWSR = 0x00;
}
uchar iic_write(uchar wdata, uchar addr)
{
start();
wait();
if(test_ack()!=START) return 1;
write_8bit(WD_DEVICE_ADDR);
wait();
if(test_ack()!=MT_SLA_ACK) return 2;
write_8bit(wdata);
wait();
if(test_ack()!=MT_DATA_ACK) return 3;
write_8bit(addr);
wait();
if(test_ack()!=MT_DATA_ACK) return 4;
write_8bit(wdata);
wait();
if(test_ack() != MT_DATA_ACK) return 5;
stop();
return 0;
}
uchar iic_read(uchar addr)
{
uchar temp;
start();
wait();
if((test_ack()!=START))
return 1;
write_8bit(WD_DEVICE_ADDR);
wait();
if(test_ack() != MT_SLA_ACK)
return 2;
write_8bit(addr);
wait();
if(test_ack()!=MT_DATA_ACK) return 3;
start();
wait();
if(test_ack()!= RE_START) return 4;
write_8bit(RD_DEVICE_ADDR+1);
wait();
if(test_ack() != MR_SLA_ACK) return 5;
twi();
wait();
if(test_ack()!= MR_DATA_NOACK) return 6;
temp = TWDR;
stop();
PORTC = 0x02;
return temp;
}
用读函数读取数据的时候 都是返回 1