I2C and MCP23016


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1

    Default I2C and MCP23016

    Hi!

    Searched around the forum but did not found a solution to my problem.

    This is what I have:
    One MCP23016 connected to PIC18F4550 via I2C.
    Pins A0,A1,A2,Vss1,Vss2 and Vss3 grounded, TP flouting...
    CLK connected to +5V with 3k9 resistor and 33pF to gnd.
    I2C pulled up with two 2k2 resistors.

    Can read and write a I2C EEPROM with I2CREAD and I2CWRITE without problems, so I think the problem is solely on the MPC23016 side.

    I'm first trying only to use half of it, that is only P1 now.
    The pins on P1 are pulled up with 10k each.

    The code is like this:
    1)
    DEFINE i2c_hold 1
    deviceIO con $40 ' MCP23016 address
    GP0 con $00 ' port GP0 all in
    GP1 con $01 ' port GP1 all in
    OLAT0 con $02 ' Output latch register
    OLAT1 con $03 '
    IPOL0 con $04 ' input polarity normal, not inverted
    IPOL1 con $05 '
    IODIR0 con $06 ' all in = 1
    IODIR1 con $07 ' 00111011 down=0,up=1,valoT=3,valoE=4,On/Off=5
    INTCAP1 con $09 ' read-only, Interrupt Captured Value
    INTCAPbyte var byte ' interrupt capture reg for buttons
    IOCON1 con $0b '
    I2Cbyte var byte
    IOEbyte var byte

    2)
    I2Creg = OLAT1 : I2Cbyte = $ff : gosub writeIOExtender: pause 20
    I2Creg = IODIR1 : I2Cbyte = $7f : gosub writeIOExtender: pause 20
    I2Creg = GP1 : I2Cbyte = $7f : gosub writeIOExtender: pause 20

    3)
    I2Creg = GP1 : gosub readIOExtender' to IOEbyte
    LCDOUT HEX2 IOEbyte

    4)
    writeIOExtender:
    i2cdevice = deviceIO
    I2CWrite SDA,SCL,i2cdevice,I2Creg,[i2cbyte],error
    return'from writeIOExtender
    '--------------------------------------------------
    readIOExtender: ' to IOEbyte
    i2cdevice = deviceIO
    I2Cread SDA,SCL, i2cdevice,I2Creg,[IOEbyte],error
    return'from readIOExtender

    You can read back every register except one (INTCAP). I do not know what fails, read or write or perhaps both, but I do not get back what I wrote, so I do not know which one fails, read or write?

    And as you can guess, buttons on GP1 does not com through

    It seems not to have any effect which one is first (2), OLAT1, IODIR1 or GP1?
    I have also the error trap if no ack received (NAK) but it jumps not there!

    Can anyone see what is wrong?... Thank you in advance ...

  2. #2
    Join Date
    Oct 2007
    Location
    The Netherlands
    Posts
    45


    Did you find this post helpful? Yes | No

    Default

    I could get the MCP23016 to work reliably only by removing the timing capacitor (33pF) all together.

  3. #3


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by eggman View Post
    I could get the MCP23016 to work reliably only by removing the timing capacitor (33pF) all together.
    Thanks Eggman!

    When I removed that capacitor the program goes to the error-label, so that does not solve my problem
    I soldered a new 33pF on the PCB and got rid of the error label jump but the original problem remains...

    I'm puzzled, I do not know what I should try next!

    Any other suggestions?

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Since you are using an 18F4550, it means you are using MPASM as the assembler.
    So, DEFINE i2c_hold 1 needs to be Uppercase.

    You can read back every register except one (INTCAP). I do not know what fails, read or write
    Not sure what you mean here. The INTCAP? registers are read-only.

    Also not sure what you are trying to do with the port.
    You write all 1's to the output latch OLAT1, then set all but 1 pin to input IODIR1, then write to GP1 which actually writes to OLAT1 again. Doesn't make sense.

    What is it you're trying to do, and what's not working.
    DT

  5. #5


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    Since you are using an 18F4550, it means you are using MPASM as the assembler.
    So, DEFINE i2c_hold 1 needs to be Uppercase.

    Not sure what you mean here. The INTCAP? registers are read-only.

    Also not sure what you are trying to do with the port.
    You write all 1's to the output latch OLAT1, then set all but 1 pin to input IODIR1, then write to GP1 which actually writes to OLAT1 again. Doesn't make sense.

    What is it you're trying to do, and what's not working.
    Thank you Darrel!

    I changed it, so now I have DEFINE I2C_HOLD 1...
    but that did not change anything.

    With INTCAP I just wanted to say that it is the only register that is read-only, besides it was kind of irrelevant from me to mention that in the first place

    The final goal is to use the device as an input extender, that is only inputs. However, one should also be able to read and write all registers, except one. Nothing seems to work, as earlier told, I can't tell if it is the write or read that fails. I have tried all kind of values and also changing the sequence of those three initializing lines but without any sign of life. Well, of course maybe some kind of quiet life when not the error label is activated.

    What values would you suggest to use in the initialization section and what sequence would you use for those three init lines?

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I think it's probably working, but you don't realize it.

    In your code, the only thing you're reading is GP1, and prior to that it writes to GP1, so you're thinking you should read the same value back.

    But when you write to GP1, the value is actually written to OLAT1.
    Reading GP1 reads the state of the pins, not what was written to GP1.
    <br>
    DT

Similar Threads

  1. MCP23016 I2C serial I/O expander chip
    By GeoJoe in forum Serial
    Replies: 7
    Last Post: - 31st October 2007, 15:39
  2. MCP23016 missing D7 on I2CRead
    By TWSK in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th June 2007, 18:37
  3. Using I2C with FullSpeed USB
    By Demon in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 8th July 2006, 04:52
  4. MCP23016 I/O expander
    By Demon in forum mel PIC BASIC Pro
    Replies: 42
    Last Post: - 24th October 2005, 01:01
  5. MCP23016 I/O expander with PBP?
    By Jon Chandler in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd August 2005, 09:32

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts