#include <Wire.h>
// Dirección I2C del TCA6408A
#define TCA6408A_ADDRESS 0x20
// Definición de los registros del TCA6408A
#define INPUT_PORT 0x00
#define OUTPUT_PORT 0x01
#define POLARITY_INVERSION 0x02
#define CONFIGURATION 0x03
// Pines de comunicación I2C
#define SDA_PIN 4
#define SCL_PIN 5
void setup() {
Wire.begin(SDA_PIN, SCL_PIN); // Inicializa I2C
Wire.setClock(100000); // Configurar el bus I2C a 100 kHz
Serial.begin(9600);
// Configurar el TCA6408A
configureTCA6408A(0b11000000); // P7, P6, P5, P4, P3, P2, P1, P0
}
void loop() {
byte outputData = 0;
bool pinState = LOW;
// Encender los pines de salida uno a uno
for(int i = 0; i < 2; i++) {
pinState = !pinState;
for(int j = 0; j < 6; j++) {
if(pinState) {
outputData |= (1 << j); // Activar el bit correspondiente al pin i
} else if(!pinState) {
outputData &= !(1 << j); // Desactivar el bit correspondiente al pin i
}
writeTCA6408A(outputData); // Enviar el dato al registro de salida
// Leer y mostrar el estado actual del registro de salida
byte currentState = readTCA6408A(OUTPUT_PORT);
Serial.print("Pin ");
Serial.print(j);
Serial.print(" configurado, estado actual: ");
Serial.println(currentState, BIN); // Imprime el estado en binario
delay(2000); // Delay entre encendido de cada pin
}
delay(1000);
}
}
void configureTCA6408A(byte pinConfig) {
// Configurar los pines como entrada(1) o salida(0)
Wire.beginTransmission(TCA6408A_ADDRESS);
Wire.write(CONFIGURATION); // Registro de configuración
Wire.write(pinConfig); // Configuración de los pines
Wire.endTransmission();
}
void writeTCA6408A(byte data) {
// Escribe en el registro de salida
Wire.beginTransmission(TCA6408A_ADDRESS);
Wire.write(OUTPUT_PORT); // Registro de salida
Wire.write(data); // Datos a escribir
Wire.endTransmission();
}
byte readTCA6408A(byte regist) {
// Lee del TCA6408A (ya sea el registro de entradas u otro)
Wire.beginTransmission(TCA6408A_ADDRESS);
Wire.write(regist); // Registro a leer
Wire.endTransmission();
Wire.requestFrom(TCA6408A_ADDRESS, 1); // Solicitar un byte del registro
if (Wire.available() >= 1) {
return Wire.read(); // Retorna el byte leído
}
return 0;
}Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1