Skip to content

Instantly share code, notes, and snippets.

@chrisstubbs93
Created December 27, 2018 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisstubbs93/77490f800e1906211e20c22d90dec2a9 to your computer and use it in GitHub Desktop.
Save chrisstubbs93/77490f800e1906211e20c22d90dec2a9 to your computer and use it in GitHub Desktop.
TCA9548A set and check multiplexer bus for arduino
#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