Re: NXC: PCF8574 read 8 values all at once ?
Posted: 30 Apr 2013, 18:17
ok, with the following it works :
thanks, Matt!
Code: Select all
#define BitRead(_Value, _Bit) !(0x01 & (_Value >> (_Bit - 1 ) )) // 1...8 instead of 0...7
Code: Select all
#define BitRead(_Value, _Bit) !(0x01 & (_Value >> (_Bit - 1 ) )) // 1...8 instead of 0...7
int PCF8574ReadPins(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];
}
if(result==-1){
TextOut(0, LCD_LINE1, "Error: Check the");
TextOut(0, LCD_LINE2, " connection!");
Wait(500);
ClearScreen();
}
return result;
}
task main() {
byte PCF8574array[9]; // just to have slots 1...8 to access
byte PCF8574port=S4, PCF8574addr=0x4E, result;
SetSensorLowspeed(S4);
while(true) {
result=PCF8574ReadPins(PCF8574port, PCF8574addr); // poll the PCF byte once
for (char i=1; i<9; ++i) {
PCF8574array[i]= BitRead(result, i); // bit read the PCF byte
NumOut( 0, 56-(8*i), i);
NumOut(18, 56-(8*i), PCF8574array[i]);
}
Wait(5);
}
}