Author Topic: IO Expander example  (Read 3261 times)

Aerospacesmith

  • Newbie
  • *
  • Posts: 5
IO Expander example
« on: May 22, 2015, 11:49:59 pm »
Hello,

I have read through the example of the IO expander and been comparing it to the datasheet of the PCF8574P. I have found a discrepancy in the example ( https://github.com/digistump/DigistumpArduino/blob/master/hardware/digistump/avr/libraries/Digispark_Examples/Expander/Expander.ino ) and the datasheet.

The example states that a '1' on the pin bits signifies a low status. The code also says to turn a pin HIGH, assign a 0 to the bit.
Code: [Select]
void expanderWrite(byte pinNumber, boolean state){
  if(state == HIGH)
    expanderStatus &= ~(1 << pinNumber);
  else
    expanderStatus |= (1 << pinNumber);
   
  expanderWrite(expanderStatus);
}


I believe the code should be:
Code: [Select]
void expanderWrite(byte pinNumber, boolean state){
  if(state == LOW)
    expanderStatus &= ~(1 << pinNumber);
  else
    expanderStatus |= (1 << pinNumber);
   
  expanderWrite(expanderStatus);
}

Is this a mistake in the example code, or am I overlooking something?

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: IO Expander example
« Reply #1 on: May 23, 2015, 11:41:26 am »
That looks correct to me. The '~' in front of the expression is a Boolean 'NOT' operator, which flips all the bits.

So, for example, let's say we're talking about pinNumber=3 for state=HIGH:

(1 << 3) Will result in 0b00001000
~(0b00001000) Will result in 0b11110111
expanderStatus &= 0b11110111 Will set pinNumber 3 LOW, which turns into a HIGH signal due to the negative logic of the signal.

Aerospacesmith

  • Newbie
  • *
  • Posts: 5
Re: IO Expander example
« Reply #2 on: May 23, 2015, 12:33:36 pm »
Oh, I see. That's assuming that your sinking current. So the 5V goes the load then to the expander which when LOW closes the circuit to GNDThat makes sense now.

So then, when reading the state of a pin, would a 1 mean the pin was HIGH? Or would that probably depend if you used a pullup or pulldown resistor?
« Last Edit: May 23, 2015, 12:46:31 pm by Aerospacesmith »