Reply To: I2C communication PCF8574AP

HomeForumsMonoBrick EV3 FirmwareI2C communication PCF8574APReply To: I2C communication PCF8574AP

#4871
Author Image
Helmut Wunder
Participant

hi,
you don’t need to address a register for R/W (it’s just register 0 then) –
maybe this helps:

I2C Interface

I2C communication with this device is initiated by a master sending a start condition, a high-to-low transition on the SDA I/O while the SCL input is high.
After the start condition, the device address byte is sent, most-significant bit (MSB) first, including the data direction bit (R/W).
This device does not respond to the general call address.
After receiving the valid address byte, this device responds with an acknowledge, a low on the SDA I/O during the high of the acknowledge-related clock pulse.
The address inputs (A0–A2) of the slave device must not be changed between the start and the stop conditions.

The data byte follows the address acknowledge. If the R/W bit is high, the data from this device are the values read from the P port. If the R/W bit is low, the data are from the master, to be output to the P port.
The data byte is followed by an acknowledge sent from this device. If other data bytes are sent from the master, following the acknowledge, they are ignored by this device. Data are output only if complete bytes are received and acknowledged.

this is the (quite simple) NXC Code you might have a look at (it reads single muxed sensors, passed by it’s number “input”; nevertheless, you might also read and return the whole bitpattern by 1 reading instead of course for quicker readings :


int ReadPCF8574(char PCF8574Port, byte PCF8574ID, char input)
{
    byte cnt = 0x02;
    byte I2CMsg[];
    byte inbuf[];
    byte Address = PCF8574ID;
    byte nByteReady = 0;
    int  result = -1;
    int loop;

    ArrayBuild(I2CMsg, Address);
    Wait(8);
    while (loop == STAT_COMM_PENDING)
    {
          loop = I2CStatus(PCF8574Port, nByteReady);
    }

    if (I2CBytes(PCF8574Port, I2CMsg, cnt, inbuf))
    {
        result = inbuf[1];
    }

    if( result == (result | (1<<input-1)) )
    {
       result = 0;
    }
    else
    {
       result = 1;
    }

    if(result==-1){
     TextOut(0, LCD_LINE1, "Error: Check the");
     TextOut(0, LCD_LINE2, "   connection!");
     Wait(500);
     ClearScreen();
    }
    return result;
}
  • This reply was modified 9 years, 6 months ago by Author ImageHelmut Wunder.
Posted in

Make a donation