Hot!Read and Write i2C PCF8574 chip

Author
donpantof
New Member
  • Total Posts : 16
  • Reward points : 0
  • Joined: 2013/06/26 00:29:46
  • Location: 0
  • Status: offline
2018/10/04 01:35:13 (permalink)
0

Read and Write i2C PCF8574 chip

Hi,
I have connected over I2C bus a I2C-IN830M0 expander and a PIC32MX795F512L microcontroller. I2C-IN830M0 expander uses a PCF8574 I2C chip. For developing i used MPLAB x IDE v4.20. I want to read or write some addresses of expander. How can i do that? I've trying something, but  it doesn't work.... I used functions for reading and writing from pic I2c lib, functions like: MasterWriteI2C1, MasterReadI2C1. Can someone send me an working example that read and write one address of expander?
Thanks!
#1

9 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 7139
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Read and Write i2C PCF8574 chip 2018/10/04 01:54:59 (permalink)
    +1 (1)
    You're unlikely to get someone to write some custom code just for you.
    Why not show us the actual code you tried, rather than just describing it in vague terms?
     

    Worst forum problems are now fixed, but the damn firewall is still there.
    #2
    donpantof
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2013/06/26 00:29:46
    • Location: 0
    • Status: offline
    Re: Read and Write i2C PCF8574 chip 2018/10/04 03:11:13 (permalink)
    0
    Hi,
    I cant send u entire code, but i send u this sample:
    //is my attempt to read
    //#define I2C_ADD   (0x27) // expander I2C address
     
           unsigned char i2cbyte;
          
            StartI2C1();       
            IdleI2C1();        
            MasterWriteI2C1((I2C_ADD << 1) | 1);
            IdleI2C1();      
           
    // I do not know if it's the correct address '0x41'
            MasterWriteI2C1(0x41);
            IdleI2C1();
           
            i2cbyte = MasterReadI2C1();    

            StopI2C1();       
            IdleI2C1();        
            
           printf("\ni2cbyte: %u\n", i2cbyte); // i want to display value that i read
     
    OBS: I forgot to tell u, but i connected a push button on  + IN1, - IN1 of expander and that is what i want to read, if button was pressed or not.
    OBS2: Jumpers ( A0, A1, A2 ) of expander board are close.
    OBS3:  It display value 255, even if button is pressed or not.
     
    P.S. I attached u a link: https://www.ereshop.com/s...e/I2C-IN830M_SHEET.pdf
          On page 5/7, Figure7: Addressing, i used address 0x41, i thought that is what i need.
     
    #3
    qhb
    Superb Member
    • Total Posts : 7139
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Read and Write i2C PCF8574 chip 2018/10/04 04:32:50 (permalink)
    +1 (1)
    I haven't used those library functions, so not sure if you're using them correctly, but your I2C addressing is certainly wrong.
    You should be using 0x41 as the "I2C_ADD" value, not 0x27.
    You can only send one address after the START cycle, the very next cycle should be a read.
    That library should give you some way to check if the Slave sent an ACK pulse after the slave address has been sent.
     

    Worst forum problems are now fixed, but the damn firewall is still there.
    #4
    donpantof
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2013/06/26 00:29:46
    • Location: 0
    • Status: offline
    Re: Read and Write i2C PCF8574 chip 2018/10/09 01:00:41 (permalink)
    0
    Hi,
    I found another demo, just for reading, but it doesn't work either. Do u have any suggestions?
     
     I2CStart(I2C1);      
    while ( !(I2CGetStatus(I2C1) & I2C_START) );     
    I2CSendByte(I2C1, 0x41);     
    while(!I2CTransmissionHasCompleted(I2C1));     
    if(I2CByteWasAcknowledged(I2C1) && I2CReceiverEnable(I2C1, TRUE) != I2C_RECEIVE_OVERFLOW)     
    {           
         while(!I2CReceivedDataIsAvailable(I2C1));           
        I2CAcknowledgeByte (I2C1, TRUE);           
        i2cbyte = I2CGetByte(I2C1);
        while( !I2CAcknowledgeHasCompleted(I2C1));     
    }     
    else                              
         i2cbyte = 0xff;     
    I2CStop(I2C1);

     
     
    PS: Always value from i2cbyte  is 255 and it doesn't enter in else state.
    post edited by donpantof - 2018/10/09 01:26:17
    #5
    Antipodean
    Super Member
    • Total Posts : 1645
    • Reward points : 0
    • Joined: 2008/12/09 10:19:08
    • Location: Didcot, United Kingdom
    • Status: offline
    Re: Read and Write i2C PCF8574 chip 2018/10/09 08:18:38 (permalink)
    0
    Is your chip a PCF8574 or a PCF8574A. They have different address ranges - check the data sheet.
     

    Do not use my alias in your message body when replying, your message will disappear ...

    Alan
    #6
    donpantof
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2013/06/26 00:29:46
    • Location: 0
    • Status: offline
    Re: Read and Write i2C PCF8574 chip 2018/10/10 00:33:06 (permalink)
    0
    Hi,
    My chip is PCF8574, but never mind, i solved it. It works! Thanks!
    #7
    qhb
    Superb Member
    • Total Posts : 7139
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Read and Write i2C PCF8574 chip 2018/10/10 05:24:43 (permalink)
    +1 (1)
    It's good manners to mention what the problem was, so anyone coming along later with the same problem can see where they may have gone wrong.
     

    Worst forum problems are now fixed, but the damn firewall is still there.
    #8
    donpantof
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2013/06/26 00:29:46
    • Location: 0
    • Status: offline
    Re: Read and Write i2C PCF8574 chip 2018/10/10 23:19:09 (permalink)
    +1 (1)
    I solved and it works! The code lines for reading are the same, i have not changed them. 
    It was a hardware error, witch i missed it. Like i said before, i connected a push button on  + IN1, - IN1 of expander, but i forgot to power it up.
    #9
    donpantof
    New Member
    • Total Posts : 16
    • Reward points : 0
    • Joined: 2013/06/26 00:29:46
    • Location: 0
    • Status: offline
    Re: Read and Write i2C PCF8574 chip 2018/10/10 23:19:24 (permalink)
    0
    Sorry! You are totally right! 
     
     
    #10
    Jump to:
    © 2018 APG vNext Commercial Version 4.5