Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

Is it possible to implement an interrupt handler in C++, which is triggered by change of Input pin value?

1395 Views - Created 10/05/2021

10/05/2021

Posted by:
eirik

eirik Avatar

Hi,

I have a working solution that polls the value on a pin connected to flow senser. An interrupt based implementation would be much nicer.

There are references to "interrupt" in the sdk, but I can not find a way to specify the callback.

Thank you for the support.

/eirik

11/05/2021

Posted by:
andrew

andrew Avatar

Location:
United Kingdom

andrew Twitter  andrew Website  

Hi eirik

I have uploaded a new interrupt demo to GitHub at https://github.com/abelectronicsuk/ABElectronics_CPP_Libraries/blob/master/ExpanderPi/demos/demo-io-interrupts.cpp which shows the basics of how interrupts work on the Expander Pi.

The interrupt hardware built into the MCP23017 controller allows you to trigger an output on the IA and IB pins when an event occurs on the input pins. You still need to regularly check the status of the interrupts to see if an event has occurred but it does allow you to check at less frequent intervals without missing out on a change on one of the input pins.

We have two interrupt tutorials for the IO Pi which use the same IO chip as the Expander Pi.

https://www.abelectronics.co.uk/kb/article/1087/io-pi-tutorial-3---introducing-interrupts

https://www.abelectronics.co.uk/kb/article/1088/io-pi-tutorial-4---more-interrupts

Both tutorials use Python instead of C++ but I have written our libraries to be similar across the different programming languages so you should be able to use the tutorials to get a good idea of how interrupts work in C++.

For a more detailed explanation of how the interrupts work in the MCP23017, I would recommend reading the datasheet for the chip. Page 27 has a description of the interrupt logic.

12/05/2021

Posted by:
eirik

eirik Avatar

Hi,



thank you again for a quick response.

I wondered if there was a way to implement a solution where I pass a pointer to a callback on my side, to avoid the loop() with the sleeps()?

Is there a huge difference between polling for interrupts, and getting/comparing the value of a pin?

Will read the docs you referred to.

/eirik

12/05/2021

Posted by:
andrew

andrew Avatar

Location:
United Kingdom

andrew Twitter  andrew Website  

You could connect the IA or IB pin on the Expander Pi to one of the Raspberry Pi GPIO pins, via a voltage divider to drop the voltage down to 3.3V. It should then be possible to use an interrupt event on the GPIO pin to call a function when the pin changes state.

Our IO Pi tutorial https://www.abelectronics.co.uk/kb/article/1088/io-pi-tutorial-4---more-interrupts explains how this works in Python. I have never worked with GPIO interrupts in C++ but I believe that the WiringPi library includes that functionality.

The main difference between polling for interrupts and getting the value of a pin is that with interrupts you can catch state changes that occur between polling. For example, if you are checking a pin every 100ms for a button press if the button is pressed for 50ms between each check you may miss the event. By polling for interrupts instead of the pin changes the interrupt will tell you that an event occurred since you last checked so you could poll the interrupt every second and not miss a button press. The downside of using interrupts this way is if two button presses occurred between polling checks the interrupt would only register one event.

Connecting the interrupt pins to the GPIO header and using an interrupt event on the Raspberry Pi side will get around this problem as your interrupt function would be called as soon as a button is pressed.

12/05/2021

Posted by:
eirik

eirik Avatar

Got it,

thank you. Excellent explanation.

/eirik

Sign in to post your reply

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.