Go Down

Topic: PCF8574 Expander Library (Read 5094 times) previous topic - next topic

wlewis

Jun 10, 2012, 02:15 am Last Edit: Jun 10, 2012, 02:19 am by wlewis Reason: 1
Here's a library for the PCF8574 in case anybody wants it. This works in v1.00 - if anybody wants to alter it for v1.01 please do so.

The .h file >

Code: [Select]

// pcf8574 lib by william lewis
#ifndef PCF8574_h
#define PCF8574_h

#include <inttypes.h>

class PCF8574Class
{
 public:
   void Prox(int, int, int);
   void Set(int, int);
   
 private:    
   int read(int);      
   void write(int, int);    
};

extern PCF8574Class PCF8574;

#endif



The .cpp file >>

Code: [Select]

// pcf8574 lib by william lewis
#include <Wire.h>

#include "PCF8574.h"

static int current_data;
static int state;

int pin_array[2][8] = {
 0B01111111, 0B10111111, 0B11011111, 0B11101111, 0B11110111, 0B11111011, 0B11111101, 0B11111110,
 0B10000000, 0B01000000, 0B00100000, 0B00010000, 0B00001000, 0B00000100, 0B00000010, 0B00000001
};

void PCF8574Class::Prox(int addr, int pin, int x) {
 state = PCF8574.read(addr);  

 if (x == 1)  
 state &= pin_array[0][pin];                      
 else if (x == 0)
 state |= pin_array[1][pin];                        
 
 PCF8574.write(addr, state);       // ON
}

void PCF8574Class::Set(int addr, int bits){
   write(addr, bits);
}


int PCF8574Class::read(int addr) {
 Wire.requestFrom(addr, 1);

 if (Wire.available()) {
 return Wire.read();
 }
}

void PCF8574Class::write(int addr, int data) {
 current_data = data;  
 
 Wire.beginTransmission(addr);
 Wire.write(data);
 Wire.endTransmission();
}

PCF8574Class PCF8574;


The .pde >>>

Code: [Select]

#include "PCF8574.h"
#define EXPANDER_A B00100000 //0x20 //PCF8574
#define EXPANDER_B B00100001 //0x21 //PCF8574

void setup() {
PCF8574.Set(EXPANDER_B, 0x7F);  

void loop(){
PCF8574.Prox(EXPANDER_B, 1, 1);  //this turns ON bit 1 on your secondary expander B
PCF8574.Prox(EXPANDER_B, 1, 0);  //this turns OFF bit 1 on your secondary expander B

PCF8574.Prox(EXPANDER_A, 1, 1);  //this turns ON bit 1 on your primary expander A
PCF8574.Prox(EXPANDER_A, 1, 0);  //this turns OFF bit 1 on your primary expander A
}




Frédéric_Plante

Yeah I got this:

http://www.ebay.ca/itm/221250141730?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649

Comming by post.

Tel me, is your libraries bidirectional?

Ill check on that as soon as i receive the piece. Thank fo you work.
As we fight our way northward into the great unknown, only that one thing remains certain...

robtillaart


Prox seems to write values .


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up