// Simulace TCA9548 I2C multiplexeru
// prostrednictvím PCF8575 na adrese 0x20, AND členů a doplnění v "tcaselect"
// POZOR vše co je na I2C před multiplexerem je stále viditelné ze strany procesoru
// tedy by tam mělo být TCA9548, LCD, a v teto simulaci i PCF8575 adr 0x20 (000)
// a jejich adresa se nesmí vyskytovat/opakovat na žádném z kanálů
#define SDA_1 21
#define SCL_1 22
#define SDA_2 18
#define SCL_2 19
#define Wire_1_FREQ 400000
#define Wire_2_FREQ 400000
#include <Wire.h> // I2C included in ARDU ?
#include <Adafruit_PWMServoDriver.h> // PWMservo library
//#include <I2cDiscreteIoExpander.h> // PCF85xx library
#include <LiquidCrystal_I2C.h> // LCD I2C library
#define PCA9685_ADDRESS_1 0x40 // I2C PCA9685
#define PCA9685_ADDRESS_2 0x41 // I2C PCA9685
#define PCA9685_ADDRESS_3 0x42 // I2C PCA9685
#define PCA9685_ADDRESS_4 0x43 // I2C PCA9685
#define TCAADDR 0x70 // TCA9548A I2C Multiplexer
//TwoWire Wire_1 = TwoWire(0);
TwoWire Wire_2 = TwoWire(1); // využiju oba interní i2c
#define Add0 32 // PCF chip1 INToutput read
//#define RXPIN 32
#define LEDPIN 25
#define DEBOUNCE_DELAY 100 // puvodni 200
volatile bool interrupt = false; // true on interrupt
volatile bool ledState = false;
volatile uint32_t lastInterruptTime = 0;
// I2C LCD display addresses - musí být nastavena podle .json
const int lcdAddress1 = 0x30; // Replace with the actual I2C address of LCD 1
LiquidCrystal_I2C lcd(lcdAddress1, 16, 2); // LCD on Address 0x27 puvodní
void setup() {
pinMode(Add0, INPUT_PULLUP);
pinMode(34, INPUT_PULLUP);
// initialize i2c interface
Wire.begin(); // misto toho dvě samostatné i2c sběrnice : // tak to funguje o.k.
// Wire_1.begin(SDA_1, SCL_1, Wire_1_FREQ); // s tím to nějak hapruje
Wire_2.begin(SDA_2, SCL_2, Wire_2_FREQ);
// initialize serial interface
Serial.begin(115200);
tcaselect(0); // Select the PCF channel
delay(1000);
Serial.println("setup i2c test :");
I2C_scan();
Serial.println("setup i2c test done");
Serial.println("");
delay(1000);
}
void loop() {
// tcaselect(0); // Select the PCF channel
Serial.println("setup i2c test :");
I2C_scan();
Serial.println("setup i2c test done");
Serial.println("");
vTaskDelay(pdMS_TO_TICKS(2000));
}
/******************************************************************************************/
void tcaselect(uint8_t i) {
if (i > 7) return;
Wire.beginTransmission(0x20); // komunkace s PCF multiplexerem
// Wire.write(~(1 << i));
Wire.write(1 << i);
Wire.endTransmission();
// device[0].digitalWrite(~(1 << i)); // PCF inveruje výstupy
Wire_2.beginTransmission(TCAADDR); // komunkace s TCA
Wire_2.write(1 << i);
Wire_2.endTransmission();
}
/******************************************************************************************/
void I2C_scan(){
Serial.println("\nI2C Scanner");
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" I2C scan ");
lcd.setCursor(0, 1);
lcd.print(" > scanning > ");
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
Serial.println("I2C 1 : ");
for(uint8_t address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
uint8_t error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C 1 device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
// nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
for(uint8_t channel = 0; channel < 8; channel++ ) {
tcaselect(channel);
Serial.print("I2C 2 channel : ");
Serial.println(channel);
for(uint8_t address = 1; address < 127; address++ ) {
Wire_2.beginTransmission(address);
uint8_t error = Wire_2.endTransmission();
if (error == 0) {
Serial.print("I2C 2 device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
}
tcaselect(0); // nastaví výchozí kanál
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
// LCD_displ(" I2C scan ", " > finished > ");
lcd.setCursor(0, 1);
lcd.print(" finished ");
}
}
5 addr 101
pwm1 00000
pwm3 00010
pwm2 00000
pwm4 00010
27 addr 111
1 addr 001
2 addr 010
3 addr 011
4 addr 100
70 addr 000
Channel 0
Channel 1
30 i2c
0 addr 000
4 addr 100
3 addr 011
5 addr 101
4 addr 100