MCP23017 Help

06 Mar 2012

Hey guys, I have a MCP23017 chip and I'm trying to understand the syntax. I have the most updated library found here:

http://mbed.org/users/wim/libraries/MCP23017/m5cz32

What do I do for a basic initialization, and write High/Low? That's all I really need. I don't understand what is going on in the program below:

    // Port A is databus - Output
    mcp23017.direction(PORT_A, PORT_DIR_OUT);
    // Port B is controlbus - Output       
    mcp23017.direction(PORT_B, PORT_DIR_OUT); 
    
    myled2 = 1;
    pc.printf("MPC Init done\n");           
    
    while(1) {
        myled1 = 1;
        mcp23017.write(PORT_A, 0xFF);
        mcp23017.write(PORT_B, 0xFF);        
        wait(0.2);
           
        myled1 = 0;
        mcp23017.write(PORT_A, 0x00);
        mcp23017.write(PORT_B, 0x00);        
        wait(0.2);
        pc.printf("*");
    }

What's the basic initialization and method for writing high/low? Thanks.

09 Mar 2012

The example code should do what you need: the direction commands configure all 8 bits of both ports to outputmode, the loop then sequentially switches all bits of each port to high level by writing 0xFF and to low by writing 0x00. The value that you write shows up on the port. So 0xF0 will result in the 4 msb at high level and the 4 lsb at low level. The port is selected by using either PORT_A or PORT_B.

09 Mar 2012

Have you initialised I2C,

You might need to define I2C pins

Ceri

13 Mar 2012

Quote:

I'm about to test some of this in the next 30 minutes. I'm only using these as an output so I don't believe I need the pullups due to the fact I'll never read any pins. If I were to read the pins, then I would need a pullup, correct? & if that was the case, I could activate them internally? Thanks for all the help again! I do appreciate your time. & is it okay to use > 3.3k pullups on the data lines? Just not less?

Hi Jesse, Pull ups are optional. You would use them when the ports are in input mode to get a defined level. For example, a pushbutton connected between GND and one of the inputpins would result in a '0' when you push the switch, but without a pullup the pinlevel is undefined when you dont push the switch. So a pullup provides a solid '1' when the switch is not operated. You still get a solid '0' on the pin when you push the switch, the max current between 3V3 and GND is limited by the pullup.

It is OK to use >3K3 pullups on the SDA and SCL, but beware that the values should not really be above 4K7. Remember that the I2C bus has open collector or open drain portpins. That means the mbed will only pull the level to '0'. It can not force a '1' on the bus. Instead it just lets the bus float. The pullup causes it to go high. A large pullup will result in slow rise times to overcome the capacitance of the wires. That leads to a slow bus, flaky levels, noise problems etc. On the other hand, the pullup value can not be too low to avoid a high current when the I2C port wants to pull it low. That leads to short circuits, power dissipation etc. So, normally 3K3 is a good value.

15 Mar 2012

Quote:

Thanks Wim! For one final confirmation (So I don't blow anything up :p), I'm going to be using 2 MCP23017s as of now. I'll have the address hardwired as 000 & 001. I'm going to have a 3.3k resistor as a pull-up to a 3.3V source for each data line. I'm also assuming I share the same data line for the 2 chips. One chip will control 16 outputs for the pins which I can handle the bits, and the other will be constantly reading. May I just activate the internal resistors on this 2nd chip and read with no problems? I want to make sure I'm not about to short something in the chip. Other than that, I think I'm good to go. You've been such a great help Wim, I appreciate it.

SDA and SCL are shared by all devices on the bus. The slaveaddress has to be different for every device, 000 and 001 is fine. SDA and SCL need a pullup. You can activate the internal pullups on the MCP that is used for reading. The values are rather high so you will never short the device that way. Test your code for both devices separately simply by temporarily disconnecting the SDA and SCL.