// Programma voor het sturen van patronen benodigd voor LATCH-UP. // I2C : LETOP Little Endian ! , I2C is normaalgesproken Big Endian // Benodigd : OpzetBord met Logic Level Converter TXS108E. // : Relay Bord 8-CH // SENCURE SPECIFIEK // Direct na startup is REG 0x15 = 0x00003; // Wanneer 0x00001 naar REG 0xBF word gestuurd zal de chip rond de ~60uA gaan trekken (geverifieerd!). // Na het script zal de chip rond de 1.5mA trekken. //Duur van het script in DEBUG_MODE met serial op 115200 baud ~220ms + 20ms opstarttijd chip = 240ms ==> 300ms settling time scimitar //Duur van het script NOT in DEBUG_MODE mode ~40ms + 20ms opstarttijd chip = 60ms ==> 100ms settling time scimitar #include #define DEBUG_MODE // Uncomment this line to enable debug mode const int deviceAddress = 0x68; // I2C address of the device (change as needed) const uint8_t relay1 = 2; // LOW ACTIVE const uint8_t relay2 = 3; // LOW ACTIVE const uint8_t oe = 5; const uint8_t d6 = 6; // Define pin D6 bool actionPerformed = false; // Flag to track if the action has been performed // I2C Register Write Structure struct I2C_Write { uint8_t reg; uint16_t data; }; // Configuration Patterns I2C_Write pattern1[] = { {0xBF, 0x0001}, {0x00, 0x0143}, {0x01, 0x0143}, {0x02, 0x0143}, {0x03, 0x0143}, {0x04, 0x0143}, {0x05, 0x0143}, {0x06, 0x0143}, {0x07, 0x0143}, {0x11, 0x00C0}, {0x12, 0x0001}, {0x14, 0x0028}, {0x15, 0x000D}, {0x16, 0x0100}, {0x1F, 0x0007}, {0x53, 0x0170} }; I2C_Write pattern2[] = { {0x46, 0x0902}, {0x47, 0x0021}, {0x48, 0x0023}, {0x49, 0x0023}, {0x4A, 0x0023}, {0x4B, 0x0023}, {0x4C, 0x0023}, {0x4D, 0x0023}, {0x4E, 0x0023}, {0x4F, 0x005F}, {0x44, 0x0FF8}, {0x52, 0x0001}, {0x2B, 0x0001} }; I2C_Write intermediateData[] = { {0x55, 0x0000}, {0x56, 0x0000}, {0x57, 0x0000}, {0x58, 0x0000} }; void relaysOn() { //Relays are low active digitalWrite(relay1,LOW); digitalWrite(relay2,LOW); delay(5); } void relaysOff() { //Relays are low active digitalWrite(relay1,HIGH); digitalWrite(relay2,HIGH); delay(5); } // Function to transmit 16-bit data in little-endian format void transmitData(uint8_t registerAddress, uint16_t data) { digitalWrite(oe,HIGH); Wire.beginTransmission(deviceAddress); Wire.write(registerAddress); // Send the register address Wire.write(data & 0xFF); // Send the low byte (LSB) Wire.write((data >> 8) & 0xFF); // Send the high byte (MSB) Wire.endTransmission(); digitalWrite(oe,LOW); } // Function to read back data from the register uint16_t readData(uint8_t registerAddress) { uint16_t receivedData = 0; digitalWrite(oe,HIGH); // First, send the register address to read from Wire.beginTransmission(deviceAddress); Wire.write(registerAddress); // Send the register address Wire.endTransmission(); if (Wire.endTransmission() != 0) { #ifdef DEBUG_MODE Serial.println("Device not found"); #endif digitalWrite(oe,LOW); return 0; // Return 0 or some error code } // Now request 2 bytes from the device Wire.requestFrom(deviceAddress, 2); if (Wire.available() == 2) { receivedData |= Wire.read(); // Read low byte (LSB) receivedData |= Wire.read() << 8; // Read high byte (MSB) } digitalWrite(oe,LOW); return receivedData; } void setup() { #ifdef DEBUG_MODE Serial.begin(115200); #endif Wire.begin(); // Initialize I2C pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); pinMode(oe, OUTPUT); pinMode(d6, INPUT); // Set D6 as input digitalWrite(oe,LOW); for(int i = 8; i <= 13; i++) { pinMode(i,OUTPUT); digitalWrite(i,LOW); } } void sendPattern1() { sendPattern(pattern1, sizeof(pattern1) / sizeof(pattern1[0])); } void sendPattern2() { sendPattern(pattern2, sizeof(pattern2) / sizeof(pattern2[0])); } void sendPattern(const I2C_Write* pattern, size_t size) { for (size_t i = 0; i < size; i++) { transmitData(pattern[i].reg, pattern[i].data); #ifdef DEBUG_MODE // Print the register address and data being transmitted using Serial Serial.print("Transmitting to Register: 0x"); Serial.print(pattern[i].reg, HEX); // Print register address in hexadecimal Serial.print(", Data: "); printHex(pattern[i].data); #endif } } void readPatternData(const I2C_Write* pattern, size_t size) { for (size_t i = 0; i < size; i++) { uint16_t receivedData = readData(pattern[i].reg); #ifdef DEBUG_MODE //Print the register address and received data Serial.print("Reading from Register 0x"); Serial.print(pattern[i].reg, HEX); Serial.print(": "); printHex(receivedData); #endif } } void readPattern1Data() { readPatternData(pattern1, sizeof(pattern1) / sizeof(pattern1[0])); } void readPattern2Data() { readPatternData(pattern2, sizeof(pattern2) / sizeof(pattern2[0])); } void readIntermediateData() { readPatternData(intermediateData, sizeof(intermediateData) / sizeof(intermediateData[0])); } void printHex(uint16_t value) { Serial.print("0x"); if (value < 0x10) { Serial.print("000"); } else if (value < 0x100) { Serial.print("00"); } else if (value < 0x1000) { Serial.print("0"); } Serial.println(value, HEX); // Print the value in hexadecimal } unsigned long lastDebounceTime = 0; // Last time the pin state changed unsigned long debounceDelay = 5; // Debounce delay in milliseconds int lastD6State = LOW; // Last stable state of D6 int currentD6State; // Current state of D6 void loop() { // Read the current state of D6 int reading = digitalRead(d6); // Check if the state has changed if (reading != lastD6State) { // Reset the debounce timer lastDebounceTime = millis(); } // If the state has been stable for the debounce delay, update the state if ((millis() - lastDebounceTime) > debounceDelay) { // Only proceed if the state has changed if (reading != currentD6State) { currentD6State = reading; // Only act if the new state is HIGH and the action hasn't been performed yet if (currentD6State == HIGH && !actionPerformed) { // Switch Relay ON relaysOn(); // Send Pattern 1 sendPattern1(); // Read Data Pattern 1 readPattern1Data(); // Read back intermediat registers readIntermediateData(); // Send Pattern 1 sendPattern2(); // Read Data Pattern 1 readPattern2Data(); // Switch Relay OFF relaysOff(); // Set the action performed flag to true actionPerformed = true; } } } // If the state is LOW, reset the action performed flag if (currentD6State == LOW) { actionPerformed = false; } // Update the last stable state lastD6State = reading; }