PCF8575 expander with inputs unstable
Posted: Tue Jun 27, 2017 3:48 am
Hi all,
Trying to integrate in my program a PCF8575 i2c GPIO expander to read inputs from 3 buttons.
I followed and slightly modified an example I found online: http://www.instructables.com/id/Using-T ... d-Inputs-/
I created a C++ class named Buttons with a pressed method to indicate, depending on the byte value read from the expander, which pin of the expander the signal comes from. As a signal, get an int = 2^n (where n is the pin number as indicated on the GPIO expander).
Here is a snippet from my code (Buttons.cpp):
I then call these methods every time I want to read from the buttons. For instance here to trigger some state in a state machine:
I'm having an issue with this: at some point in the program execution (I can't determine why because it varies from one execution to the other), the error message "error reading PF8575, check connections" keeps triggering even though the hardware connection is still there. I have other things connected to the i2c bus so I thought it might be that, but this happens even when I disconnect everything else from the i2c bus.
Any ideas how to fix this? I was looking at interrupts too, unable to make it work yet with this expander.
Thanks!
Trying to integrate in my program a PCF8575 i2c GPIO expander to read inputs from 3 buttons.
I followed and slightly modified an example I found online: http://www.instructables.com/id/Using-T ... d-Inputs-/
I created a C++ class named Buttons with a pressed method to indicate, depending on the byte value read from the expander, which pin of the expander the signal comes from. As a signal, get an int = 2^n (where n is the pin number as indicated on the GPIO expander).
Here is a snippet from my code (Buttons.cpp):
Code: Select all
#include "Buttons.h"
#include <Arduino.h>
#include <Wire.h>
#include "../defines.h"
void Buttons::init() {
pcf8575_write(word(B00000000, B00000000));
DBG("Buttons initialized");
}
bool Buttons::pressed(int button) {
uint16_t dataReceive;
if (pcf8575_read(dataReceive))
return (dataReceive == button);
else
return false;
}
void Buttons::pcf8575_write(uint16_t dt) {
uint8_t error;
Wire.beginTransmission(PF_ADDR);
Wire.write(lowByte(dt));
Wire.write(highByte(dt));
error = Wire.endTransmission();
if (error == ku8TWISuccess)
DBG("success initializing PF8575");
else {
DBG("error writing to PF8575, check connections");
}
}
bool Buttons::pcf8575_read(uint16_t &result) {
uint8_t hi, lo, error;
Wire.beginTransmission(PF_ADDR);
error = Wire.endTransmission();
if (error == ku8TWISuccess) {
Wire.requestFrom(PF_ADDR, 2);
if (Wire.available()) {
lo = Wire.read();
hi = Wire.read();
result = (word(hi, lo));
return true;
} else {
DBG("error: Wire not available");
return false;
}
} else {
DBG("error reading PF8575, check connections");
return false;
}
}
Code: Select all
if (buttons.pressed(MINUS) && buttons.pressed(OK)) {
sm.trigger("ADMIN");
}
Any ideas how to fix this? I was looking at interrupts too, unable to make it work yet with this expander.
Thanks!