MCP23017 & IPOL

I'm using an MCP to talk to a 6gang 240v/5v relay set while honouring 6 manual switches, using the interrupts - this is all working

i've also got a web interface, so if i (say) turn relay0 on over the web, i wanted to invert the polarity for that manual switch so it still works 'correctly', manually

Setting IPOL on the MCP causes an ISR storm that never ceases - 0xff is the cause and 0xff is the state

is there something esoteric i'm missing about changing polarity on a single port? line 121

i am perplexed - any pointers, gladly received

The problem is that the change if the input polarity changes the value of that port, so an interrupt is triggered. In the interrupt handler you set the switch to change, which calls the same routine, so the polarity is changed again and you're in that endless loop. I don't see why you have to change the polarity. Simple toggle the output every time you call the DoSwitch routine.

thanks for taking the time to read & answer!

The B port, and IPOL, is set from the Web handler, NOT the ISR, otherwise i'd agree with you :)

I want to change the polarity on the physical switch so it behaves ie NOT as follows

  • Human turns physical switch OFF - relay goes off
  • Web turns relay ON - physical switch is still OFF

- Human turns physical 'ON' - nothing happens, confusion ensues

you're correct, i can just handle the NOT mask in software - first time i've used this IC and wanted to take it for a run

The B port, and IPOL, is set from the Web handler, NOT the ISR, otherwise i'd agree with you

I do not agree. In the ISR you call DoSwitch and that calls SetRelay which in turn has that IPOL switching statement (OK, commented out but that was your question). I guess your interpretation error was that the IPOL setting just changes the reading output of the chip. But in fact it acts as if the incoming signal from the switch was the opposite and it therefor issues an interrupt because the signal changes when the polarity changes.

indeed - hoist/petard :)

I haven't really tested that use-case currently - i'm still beating it up with Postman scripts

i DO appreciate your help, i'll dissect your reply - i completely assume my interpretation is wrong