Compare commits

3 Commits

Author SHA1 Message Date
Wesley Hofman
469c5cd7a6 update 2025-09-19 10:58:01 +02:00
Wesley Hofman
7540e5d92f Verified for CASE1 2025-09-17 14:53:28 +02:00
Wesley Hofman
d1b57db444 Working writing/reading at 0XBF 2025-09-17 12:26:06 +02:00

View File

@@ -1,31 +1,91 @@
// 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 <Wire.h> #include <Wire.h>
#define DEBUG_MODE // Uncomment this line to enable debug mode
const int deviceAddress = 0x68; // I2C address of the device (change as needed) const int deviceAddress = 0x68; // I2C address of the device (change as needed)
const uint8_t relay1 = 2; const uint8_t relay1 = 2; // LOW ACTIVE
const uint8_t relay2 = 3; const uint8_t relay2 = 3; // LOW ACTIVE
const uint8_t oe = 5; const uint8_t oe = 5;
const uint8_t d6 = 6; // Define pin D6 const uint8_t d6 = 6; // Define pin D6
bool actionPerformed = false; // Flag to track if the action has been performed 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 // Function to transmit 16-bit data in little-endian format
void transmitData(uint8_t registerAddress, uint16_t data) { void transmitData(uint8_t registerAddress, uint16_t data) {
digitalWrite(oe,HIGH);
Wire.beginTransmission(deviceAddress); Wire.beginTransmission(deviceAddress);
Wire.write(registerAddress); // Send the register address Wire.write(registerAddress); // Send the register address
Wire.write(data & 0xFF); // Send the low byte (LSB) Wire.write(data & 0xFF); // Send the low byte (LSB)
Wire.write((data >> 8) & 0xFF); // Send the high byte (MSB) Wire.write((data >> 8) & 0xFF); // Send the high byte (MSB)
Wire.endTransmission(); Wire.endTransmission();
digitalWrite(oe,LOW);
} }
// Function to read back data from the register // Function to read back data from the register
uint16_t readData(uint8_t registerAddress) { uint16_t readData(uint8_t registerAddress) {
uint16_t receivedData = 0; uint16_t receivedData = 0;
digitalWrite(oe,HIGH);
// First, send the register address to read from // First, send the register address to read from
Wire.beginTransmission(deviceAddress); Wire.beginTransmission(deviceAddress);
Wire.write(registerAddress); // Send the register address Wire.write(registerAddress); // Send the register address
Wire.endTransmission(); Wire.endTransmission();
if (Wire.endTransmission() != 0) { if (Wire.endTransmission() != 0) {
Serial.println("Device not found"); #ifdef DEBUG_MODE
Serial.println("Device not found");
#endif
digitalWrite(oe,LOW);
return 0; // Return 0 or some error code return 0; // Return 0 or some error code
} }
@@ -35,28 +95,75 @@ uint16_t readData(uint8_t registerAddress) {
receivedData |= Wire.read(); // Read low byte (LSB) receivedData |= Wire.read(); // Read low byte (LSB)
receivedData |= Wire.read() << 8; // Read high byte (MSB) receivedData |= Wire.read() << 8; // Read high byte (MSB)
} }
digitalWrite(oe,LOW);
return receivedData; return receivedData;
} }
void setup() { void setup() {
Serial.begin(9600); #ifdef DEBUG_MODE
Serial.begin(115200);
#endif
Wire.begin(); // Initialize I2C Wire.begin(); // Initialize I2C
pinMode(relay1,OUTPUT); pinMode(relay1, OUTPUT);
pinMode(relay2,OUTPUT); pinMode(relay2, OUTPUT);
pinMode(oe, OUTPUT);
pinMode(d6, INPUT); // Set D6 as input pinMode(d6, INPUT); // Set D6 as input
digitalWrite(oe,LOW);
for(int i = 8; i <= 13; i++) for(int i = 8; i <= 13; i++)
{ {
pinMode(i,OUTPUT); pinMode(i,OUTPUT);
digitalWrite(i,LOW); digitalWrite(i,LOW);
} }
pinMode(oe,OUTPUT);
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(oe,HIGH);
} }
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) { void printHex(uint16_t value) {
Serial.print("0x"); Serial.print("0x");
if (value < 0x10) { if (value < 0x10) {
@@ -70,7 +177,7 @@ void printHex(uint16_t value) {
} }
unsigned long lastDebounceTime = 0; // Last time the pin state changed unsigned long lastDebounceTime = 0; // Last time the pin state changed
unsigned long debounceDelay = 50; // Debounce delay in milliseconds unsigned long debounceDelay = 5; // Debounce delay in milliseconds
int lastD6State = LOW; // Last stable state of D6 int lastD6State = LOW; // Last stable state of D6
int currentD6State; // Current state of D6 int currentD6State; // Current state of D6
@@ -92,19 +199,20 @@ void loop() {
// Only act if the new state is HIGH and the action hasn't been performed yet // Only act if the new state is HIGH and the action hasn't been performed yet
if (currentD6State == HIGH && !actionPerformed) { if (currentD6State == HIGH && !actionPerformed) {
uint8_t registerAddress = 0xBF; // Switch Relay ON
uint16_t dataToSend = 0x0000; relaysOn();
// Send Pattern 1
// Transmit the data sendPattern1();
transmitData(registerAddress, dataToSend); // Transmit data // Read Data Pattern 1
// Read back data readPattern1Data();
uint16_t receivedData = readData(registerAddress); // Read back data // Read back intermediat registers
readIntermediateData();
// Use the method to print received data in hexadecimal format // Send Pattern 1
Serial.println("Received data : "); sendPattern2();
printHex(receivedData); // Print formatted received data // Read Data Pattern 1
Serial.println(); // New line for better readability readPattern2Data();
// Switch Relay OFF
relaysOff();
// Set the action performed flag to true // Set the action performed flag to true
actionPerformed = true; actionPerformed = true;
} }