Instead of that, any time I wanted to check the state of an IO, instead of using PCF8574array[IO_NUMBER] I would use something like this:
Having already defined that as:
Code: Select all
#define PCF8574input(input) (0x01 & (result >> input))
Where "result" is the (non-error) value returned by my ReadPins function. Any time ReadPins is successful, update the variable called "result", and then use PCF8574input(input) to check the state of each IO.
The IOs are indexed 0-7, but if you want them to be 1-8, use this instead:
Code: Select all
#define PCF8574input(input) (0x01 & (result >> (input - 1)))
Since all you're doing is checking the bit state (not at all specific to things related to the PCF8574), I'd use this instead:
Code: Select all
#define BitRead(_Value, _Bit) (0x01 & (_Value >> _Bit))
Where the _Value is the input byte (or int, or whatever else is storing the bit-representation), and _Bit is the bit you want to check the status of (0 through (bits-1))
Here is a complete, self-contained, working example (I just tested it):
Code: Select all
void WritePins(byte port, byte address, byte byteout, bool invert = false, bool wiat = false){
if(invert){
byteout = ~byteout;
}
byte I2CMsg[];
byte nByteReady = 0;
ArrayBuild(I2CMsg, address, byteout);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING) {
Yield();
}
I2CWrite(port, 1, I2CMsg);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING && wiat) {
Yield();
}
}
int ReadPins(byte port, byte address){
byte cnt = 2;
byte I2CMsg[];
byte inbuf[];
byte nByteReady = 0;
int result = -1;
ArrayBuild(I2CMsg, address);
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING){
Yield();
}
if (I2CBytes(port, I2CMsg, cnt, inbuf)) {
result = inbuf[1];
}
return result;
}
#define BitRead(_Value, _Bit) (0x01 & (_Value >> _Bit))
byte PCF8574_INPUT;
int result;
task main()
{
SetSensorLowspeed(S1);
while (true)
{
result = ReadPins(S1, 0x40);
if(result != -1){
PCF8574_INPUT = result;
}
ClearScreen();
NumOut(50, LCD_LINE1, BitRead(PCF8574_INPUT, 0));
NumOut(50, LCD_LINE2, BitRead(PCF8574_INPUT, 1));
NumOut(50, LCD_LINE3, BitRead(PCF8574_INPUT, 2));
NumOut(50, LCD_LINE4, BitRead(PCF8574_INPUT, 3));
NumOut(50, LCD_LINE5, BitRead(PCF8574_INPUT, 4));
NumOut(50, LCD_LINE6, BitRead(PCF8574_INPUT, 5));
NumOut(50, LCD_LINE7, BitRead(PCF8574_INPUT, 6));
NumOut(50, LCD_LINE8, BitRead(PCF8574_INPUT, 7));
Wait(25);
}
}