Problem about using PCF8574 as an input without PCF8574 library.


hello, i'm trying learn twi inteface pcf8574 , testing on proteus. want use 1 pcf8574 read button status , show onto 3 pieces 7 segment digits connected 3 pieces pcf8574. problem when press button not show result or show strange values. can problem ?
my code , schematic attached message.

code: [select]

#include <wire.h>
#define ones_pcf8574_addr 0b0100000
#define tens_pcf8574_addr 0b0100001
#define hundreds_pcf8574_addr 0b0100010
#define button_pcf8574_addr 0b0100100

byte seg[10] = {
              0b01111110, 0b00110000, 0b01101101, 0b01111001, 0b00110011, 0b01011011,
              
              0b01011111, 0b01110000, 0b01111111, 0b01111011
          };
volatile byte _data = 0xff;
boolean hasint;
byte value = 0;

void extint() {
  hasint = true;
  value++;
}

void setup() {
  wire.begin();
  pinmode(8, input);
  pinmode(9, output);
  ones(0);
  tens(0);
  hundreds(0);
  attachinterrupt(0, extint, falling);
}

void loop() {
  if(hasint) {
    readdata();
    hasint = false;
  }
}

void readdata() {
  //byte _data;
  wire.requestfrom(button_pcf8574_addr, 1);
  if (wire.available()) {
    _data = wire.read();
  }
  display(_data);
}

void ones(byte val) {
  wire.begintransmission(ones_pcf8574_addr);
  wire.write(seg[val]);
  wire.endtransmission();
  delay(5);
}

void tens(byte val) {
  wire.begintransmission(tens_pcf8574_addr);
  wire.write(seg[val]);
  wire.endtransmission();
  delay(5);
}

void hundreds(byte val) {
  wire.begintransmission(hundreds_pcf8574_addr);
  wire.write(seg[val]);
  wire.endtransmission();
  delay(5);
}

void display(byte val) {
  ones(val % 10);
  tens(val / 10);
  hundreds(val / 100);
}

one of problem wrong display function, corrected version is;
code: [select]

void display(byte val) {
  int _hundreds = val / 100; // 241 / 100 = 2
  int _tens = (val - (_hundreds * 100)) / 10; //241 - (2 * 100) = 41 / 10 = 4
  int _ones = val % 10;
  ones(_ones);
  tens(_tens);
  hundreds(_hundreds);
}


in way, can see pcf8574's input register values such 254,253,251,247,239. when release button see 255 value on display. how can reset value "000" ? external interrupt settings ?


Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Problem about using PCF8574 as an input without PCF8574 library.


arduino

Comments