Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15217

Simple test for PCF8574 port expander

$
0
0

I just received a breakout board for the PCF8574 from Aliexpress. As is my custom when ordering from unknown sources I wrote some test code to make sure the basic functions worked when it arrived. If you want to see all the test patterns remove the error check if() statements. Here is the code.


/************************
 * Test PCF8574 I/O expander
 * 
 * The PCF8574 has quasi-bidirectional ports, which means that only low levels 
 *  are driven. A high level is a weak pullup and is treated as an input which 
 *  can be pulled low.
 * 
 * In this test the 8 pins are connected as 2 nybbles of 4 bits with one being
 *   driven with test patterns and the other read to see that the patterns match.
 *   Connections are as follows.
 *   
 *   p0 - p4
 *   p1 - p5
 *   p2 - p6
 *   p3 - p7
 *   
 * When data is written the test nybble is set to all highs (input) When it  
 *   is read back it should match the output nybble, which will have some low
 *   pins (except the the all high case)
 *   
 * You can remove a connection to generate an error message. Note that only setting bits low will generate an error.
 * Because of the weak pull up, a disconnected pin and a pin pulled high are indistinguishable. 
 *************************/

// Of the numerous libraries this is the one by Rob Tillaart - be sure you get the right one
#include <PCF8574.h>

// Use streams because I am to lazy to write yards of Serial.print() statements
#include <Streaming.h>

PCF8574 expander(0x20);  // Address 0x20 - A2, A1, A0 shorted to GND

void setup() {
  uint8_t testByte,checkByte,resultByte;
  Serial.begin(115200);
  expander.begin();
  Serial << "Starting test"<<endl;

  for (uint8_t i = 0; i<16; i++){  // test all bits in the nybble
     // first do high nybble as input and low nybble as output
     testByte = i | 0xf0;  // set upper bits to input
     checkByte = (i<<4) | i;  // set expected result to have both nybbles the same
     expander.write8(testByte); // write data byte
     resultByte = expander.read8();  // read back data - upper nybble should be the same as lower nybble
     if( checkByte != resultByte){
      Serial << "Error in test,testByte = " <<_HEX(testByte)<<" checkByte = "<<_HEX(checkByte)<<" resultByte = "<<_HEX(resultByte)<<endl;
      Serial<<"  bit(s) in error = "<<_HEX(~checkByte&resultByte)<<endl;
     }
     // then do low nybble as input and high nybble as output
     testByte = (i<<4) | 0x0f;  // set lower bits to input
     checkByte = (i<<4) | i;  // set expected result to have both nybbles the same
     expander.write8(testByte); // write data byte
     resultByte = expander.read8();  // read back date - upper nybble should be the same as lower nybble
     if( checkByte != resultByte){
      Serial << "Error in test,testByte = " <<_HEX(testByte)<<" checkByte = "<<_HEX(checkByte)<<" resultByte = "<<_HEX(resultByte)<<endl;
      Serial<<"  bit(s) in error = "<<_HEX(~checkByte&resultByte)<<endl;
     }
  }
  Serial << "Test complete"<<endl;

}
void loop() {
}  

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 15217

Trending Articles