Created
December 27, 2018 22:08
-
-
Save chrisstubbs93/77490f800e1906211e20c22d90dec2a9 to your computer and use it in GitHub Desktop.
TCA9548A set and check multiplexer bus for arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#define TCAADDR 0x70 | |
void setup() { | |
Wire.begin(); | |
Serial.begin(115200); | |
} | |
void loop() { | |
for (int bus = 0; bus <= 7; bus++) { | |
Serial.print("Selecting "); Serial.println(bus); | |
tcaselect(bus); | |
delay(1000); | |
} | |
} | |
void tcaselect(uint8_t i) { //sets the bus and checks it, retrying if it's not correct | |
do { | |
if (i > 7) return; | |
Wire.beginTransmission(TCAADDR); | |
Wire.write(1 << i); | |
Wire.endTransmission(); | |
} while (!(tcaget() == i)); | |
} | |
int tcaget() { //returns the current bus of the mux 0...7 | |
Wire.requestFrom(TCAADDR, 1); | |
byte c = Wire.read(); | |
for (int i = 0; i <= 7; i++) { | |
if (c == 0x01) return i; | |
c = c >> 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment