8 years, 6 months ago.

MCP23017 Help!

Sorry ahead of time, as I am newer to the microcontroller world.

So I am using an lpc1768 and a MCP23017 connected via I2C. Basicly all I want help with is setting up the code to communicate properly.

I am using this library https://developer.mbed.org/users/jimherd/code/MCP23017/

I am having a hard time finding examples that use the IO expander as an input. Everything I find shows it being setup and an output. I don't need all the code, just the necessary parts for getting the chip communicating and an example of how to check for changes on one of the pins, and maybe how to attach a function to an interrupt. If it makes it any easier I am planning on connecting momentary push buttons to the mcp23017.

Thanks!

1 Answer

8 years, 6 months ago.

OK so just reading the port should be straightforward. Call config to set direction to in and set pull-up. The internal pull-up is enough for push button use, and if you are using grounding push buttons you could set reverse polarity so pressed returns 1. Then you have read_bit and read_mask.

Due to the overhead involved with I2C I'd advise using read_mask and grabbing all the inputs in one operation rather than bit operations.

I never bothered with interrupts, I relied on regular polling. I2C can be a bit slow to put in an ISR and if you are doing anything else on the same bus you might need flags to make sure the ISR doesn't break into another I2C operation.

In my view the best use for the INT pins is for a wake-from-sleep function.

Awesome, thanks for the heads up. Do you have an example of a wake from sleep function?

posted by Anthony P 01 Jun 2016

Only for PIC series parts not LPC, on the PIC series you could set up some pins to detect transitions then execute a "sleep" instruction and it would halt until it detected a transition. Most examples were pushbuttons or keypads. I don't know how it works on LPC parts but it probably works via the interrupt handler so you'd attach a "dummy" interrupt to a pin to turn it into a "wake-up" pin.

posted by Oliver Broad 01 Jun 2016