1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

NXC: PCF8574 read 8 values all at once ?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.

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

Postby doc-helmut » 30 Apr 2013, 18:17

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:
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2424
Joined: 29 Sep 2010, 14:25

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

Postby mattallen37 » 30 Apr 2013, 18:28

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 ;)
User avatar
mattallen37
 
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA

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

Postby mattallen37 » 30 Apr 2013, 18:29

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 ;)
User avatar
mattallen37
 
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA

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

Postby doc-helmut » 30 Apr 2013, 18:33

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
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2424
Joined: 29 Sep 2010, 14:25

Previous

Return to Mindstorms Software

Who is online

Users browsing this forum: No registered users and 0 guests

cron