Pages: [1]   Go Down
Author Topic: PCF8574 Expander Library  (Read 4682 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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:
// 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:
#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
}


« Last Edit: June 09, 2012, 07:19:48 pm by wlewis » Logged

Québec
Offline Offline
God Member
*****
Karma: 17
Posts: 673
Hey bien!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

As we fight our way northward into the great unknown, only that one thing remains certain...

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Prox seems to write values .


Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: