NXC: PCF8574 read 8 values all at once ?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post 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:
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

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

Post 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
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

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

Post by mattallen37 »

I'm glad it's working for you ;)
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

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

Post 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
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 5 guests