-
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fast polling of PCF8574 does not need to hook the interrupt #48
Comments
Thanks for your remarks. They make sense and are valuable especially for faster processors eg Esp32. |
A separate example is of course welcome |
Need to check if the INT stays high until the device is read. If not polling might miss an INT. On the other hand, if the INT is a pulse and loop does not read before a new INT pulse comes you also miss an event. This latter can be detected with a second flag. If INT happens and the flag is still set a read has been missed.. Need to check datasheet on this. For a relative slow application it doesn't matter but it might affect some other if loop has a high load. (Faster processor needed). |
Read a datasheet again to see what behavior to expect. An interrupt is generated by any rising or falling edge of the port inputs in the input mode. After So there are three scenarios the interrupt flag is reset.
If (1) does indeed reset the INT, the polling you propose could miss a change and back if this happens between two polls. Q: As you have a setup (assumption), can you verify this? It would also mean that a boolean flag in the interrupt routine should be a counter. 0 would mean no INT has passed, or INT has been processed. |
I can confirm that the INT pin will be de-asserted if the input pins return to their original state. This means it is possible to miss an event if the input quickly returns to its original state. However even with the original interrupt based solution we would detect that an event had happened but when we queried the PCF8574 in |
Thanks for testing and sharing. |
Today I will
|
PR created with develop branch. |
PCF8575 has same behavior. |
Merged the PR, |
Thanks for including an example of how to quickly poll the PCF8574 when reading a rotary encoder. This code is really useful if you are building a front panel display board with some rotary encoders and push buttons.
I notice in the PCF8574_rotaryEncoder.ino example that the interrupt handler is just setting a
flag
variable and the actual I2C polling of the PCF8574 is done from theloop()
function. I considered moving the actual I2C reads into the interrupt handler but as I2C is relatively slow this did not seem like the smartest move. My application is also using interrupts to drive some stepper motor controllers and these are highly sensitive to jitter.The other option I ended up choosing is to just dispense with the PCF8574 interrupt handler altogether and in the
loop()
function check the GPIO for the INT pin directly. i.e.:instead of:
if (flag)
Reading from a GPIO port is almost as fast as testing the flag variable so there is not real performance penalty for just checking the value of INT in
loop()
. The INT signal from the PCF8574 is just an indication that some input has changed and it will by reset to high again when we perform the I2C read from the device.I though I would add this comment as it might be useful to others wanting to quickly poll a PCF8574 without having to resort to an interrupt handler or continuous I2C reads. I am happy to provide another example program if this is useful.
The text was updated successfully, but these errors were encountered: