Page 2 of 2

Re: NXC: PCF8574 read 8 values all at once ?

Posted: 30 Apr 2013, 18:17
by HaWe
ok, with the following it works :

Code: Select all

#define BitRead(_Value, _Bit) !(0x01 & (_Value >> (_Bit - 1 ) )) // 1...8 instead of 0...7
:ugeek:

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);
  }

}
thanks, Matt!
:mrgreen:

Re: NXC: PCF8574 read 8 values all at once ?

Posted: 30 Apr 2013, 18:28
by mattallen37
If the sensor is pressed, the IO state should be 0. The touch sensor pulls the line low when pressed, and when not pressed, the internal pullup of the PCF8574 IO pulls the line back high.

Inverting the value like you did is a good way to fix the "problem". Note however that the ! symbol means logic NOT, and the ~ symbol means bit NOT. It's not common practice to use ! when dealing with bits/data, rather than logic. Along the lines of more common programming practice, I would have done something like this instead:

Code: Select all

#define BitRead(_Value, _Bit) (0x01 & ((~_Value) >> (_Bit - 1 ) )) // 1...8 instead of 0...7

Re: NXC: PCF8574 read 8 values all at once ?

Posted: 30 Apr 2013, 18:29
by mattallen37
I'm glad it's working for you ;)

Re: NXC: PCF8574 read 8 values all at once ?

Posted: 30 Apr 2013, 18:33
by HaWe
I admittedly don't understand the "level pull" thing because I don't understand the electronical side
- but -
yes, thanx, works quite as well! :)
- and insanely quick! :D