Hello,
I have a question about the code posted in the code forum about reading a PCF8574.
The code is ( a little simplified ) :
int IN8574(int device_addr)
{
int io_field;
i2c_write(0x40 | device_addr<<1 | I2CREAD);
io_field=i2c_read(); /* this byte contains the prior conversion result. */
io_field=i2c_read(0); /* returned data is big-endian (msbit first) */
i2c_stop();
return(io_field);
}
But, I alway have 255 for io_field.
So, I use my old code :
byte IN(byte adr)
{
byte datain;
I2C_start();
I2C_write((adr<<1)|0x41);
datain=I2C_read();
I2C_write((adr<<1)|0x41);
I2C_write(0xff);
I2C_stop();
return(datain);
}
And it is working.
Can someone help me to understand where is the problem ?
I use PIC16F877
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3,SLOW)
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
Posted: Mon May 21, 2007 2:54 pm
Quote:
int IN8574(int device_addr)
{
int io_field;
i2c_write(0x40 | device_addr<<1 | I2CREAD);
io_field=i2c_read(); /* this byte contains the prior conversion result. */
io_field=i2c_read(0); /* returned data is big-endian (msbit first) */
i2c_stop();
return(io_field);
}
Don't simplify the code. You have left out the i2c_start() from
the driver code that was posted.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum