Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "TCA9555.h"
- TCA9555 TCA1(0x20);
- TCA9555 TCA2(0x21);
- byte pin_tca[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
- #define TCA9555_INPUT_PORT_REGISTER_0 0x00 // read(0-7)
- #define TCA9555_INPUT_PORT_REGISTER_1 0x01 // read(8-15)
- byte data_tca1_port0 = 0;
- byte data_tca1_port1 = 0;
- unsigned long cur_time, old_time;
- bool port0 = 0;
- bool port1 = 1;
- byte addr_tca1 = 0x20;
- byte addr_tca2 = 0x21;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Wire.begin();
- TCA1.begin();
- TCA2.begin();
- for (int i = 0; i < sizeof(pin_tca); i++) {
- Serial.print(i);
- Serial.print(",");
- TCA1.pinMode(pin_tca[i], INPUT_PULLUP);
- TCA2.pinMode(pin_tca[i], INPUT_PULLUP);
- }
- }
- void loop() {
- // put your main code here, to run repeatedly:
- cur_time = millis();
- if (cur_time - old_time > 100) {
- byte _retry = 0;
- while (!baca_input1(addr_tca1, port0) && _retry < 10) {
- _retry++;
- Serial.print("retry: ");
- Serial.println(_retry);
- delay(20);
- }
- _retry = 0;
- while (!baca_input1(addr_tca1, port1) && _retry < 10) {
- _retry++;
- Serial.print("retry: ");
- Serial.println(_retry);
- delay(20);
- }
- Serial.print("tca1 port0: ");
- Serial.println(data_tca1_port0,BIN);
- Serial.print("tca1 port1: ");
- Serial.println(data_tca1_port1,BIN);
- old_time = millis();
- }
- }
- bool baca_input1(byte addr, byte mask) {
- bool flag_tca = 0;
- Wire.beginTransmission(addr);
- switch (mask) {
- case 0:
- Wire.write(TCA9555_INPUT_PORT_REGISTER_0);
- break;
- case 1:
- Wire.write(TCA9555_INPUT_PORT_REGISTER_1);
- break;
- }
- int rv = 0;
- rv = Wire.endTransmission();
- if (rv != 0) {
- flag_tca = 0;
- Serial.println("gagal baca");
- } else {
- Wire.requestFrom(addr, (uint8_t)1);
- switch (mask) {
- case 0:
- data_tca1_port0 = Wire.read();
- break;
- case 1:
- data_tca1_port1 = Wire.read();
- break;
- }
- flag_tca = 1;
- }
- return flag_tca;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement