Allego il codice V2 e V3:
V2 codice con bug e eeprom esterna
V3 codice funzionante con eeprom interna
File A V2:
#include "Wire.h"
#define EEPROM_I2C_ADDRESS 0x50
#define Num_In 8
#define Num_Out 6
#define Debug_Led 13
byte Pin_In[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
byte Pin_Out[] = { 11, 12, A0, A1, A2, A3};
bool In[Num_In], Out[Num_Out];
#define Num_Mem 32
#define Num_Timer 32
#define Num_Counter 16
#define Num_Edge 32
bool Mem[Num_Mem];
bool Timer_Out[Num_Timer];
int Timer_Data[Num_Timer];
bool Counter_Out[Num_Counter], Counter_Mem[Num_Counter];
byte Counter_Data[Num_Counter];
bool Edge_Out[Num_Edge], Edge_Mem[Num_Edge];
#define Num_Cell 150
bool Cell_Mem[Num_Cell];
//DATA_DTA
// 1 ST, 2 AND, 3 OR, 4 OUT
//DATA_DTB
// 1 IN, 2 OUT, 3 MEM, 4 TM, 5 CT, 6 CR, 7 EG
//NOT (VALUE)
// 0 BUF, 1 NOT
byte DATA_DTA[Num_Cell];
byte DATA_DTB[Num_Cell];
byte DATA_ADR[Num_Cell];
byte DATA_VAL[Num_Cell];
String Message;
int Message_Int;
byte MCell_ADR, Selector;
#define Counter_Led_Value 20
int Counter_Led;
void setup() {
delay(10);
for (byte i = 0; i < Num_In; i++) {
pinMode(Pin_In[i], INPUT);
}
for (byte i = 0; i < Num_Out; i++) {
pinMode(Pin_Out[i], OUTPUT);
}
pinMode(Debug_Led, OUTPUT);
Wire.begin();
Serial.begin(115200);
for (byte i = 0; i < Num_Cell; i++) {
DATA_DTA[i] = EEPROM_Read(i);
DATA_DTB[i] = EEPROM_Read(i + Num_Cell);
DATA_ADR[i] = EEPROM_Read(i + (Num_Cell * 2));
DATA_VAL[i] = EEPROM_Read(i + (Num_Cell * 3));
}
delay(100);
}
void loop() {
delay(1);
Read_Input();
if (Serial.available() > 0) {
while (Serial.available() > 0) {
Message = Serial.readStringUntil('x');
Message_Int = Message.toInt();
//Serial.print(Message_Int);
if (Selector == 1) {
DATA_DTA[MCell_ADR] = Message_Int;
//Serial.print("DATA A -> ");
//Serial.println(DATA_DTA[MCell_ADR]);
Selector = 0;
} else if (Selector == 2) {
DATA_DTB[MCell_ADR] = Message_Int;
//Serial.print("DATA B -> ");
//Serial.println(DATA_DTB[MCell_ADR]);
Selector = 0;
} else if (Selector == 3) {
DATA_ADR[MCell_ADR] = Message_Int;
//Serial.print("DATA A -> ");
//Serial.println(DATA_ADR[MCell_ADR]);
Selector = 0;
} else if (Selector == 4) {
DATA_VAL[MCell_ADR] = Message_Int;
//Serial.print("DATA A -> ");
//Serial.println(DATA_VAL[MCell_ADR]);
Selector = 0;
} else if (Selector == 5) {
MCell_ADR = Message_Int;
//Serial.print("CELL -> ");
//Serial.println(MCell_ADR);
Selector = 0;
}
if (Selector == 6) {
//ST
DATA_DTA[MCell_ADR] = 1;
Selector = 0;
} else if (Selector == 7) {
//AND
DATA_DTA[MCell_ADR] = 2;
Selector = 0;
} else if (Selector == 8) {
//OR
DATA_DTA[MCell_ADR] = 3;
Selector = 0;
} else if (Selector == 9) {
//OUT
DATA_DTA[MCell_ADR] = 4;
Selector = 0;
}
if (Selector == 10) {
//IN
DATA_DTB[MCell_ADR] = 1;
Selector = 0;
} else if (Selector == 11) {
//Q
DATA_DTB[MCell_ADR] = 2;
Selector = 0;
} else if (Selector == 12) {
//MEM
DATA_DTB[MCell_ADR] = 3;
Selector = 0;
} else if (Selector == 13) {
//TM
DATA_DTB[MCell_ADR] = 4;
Selector = 0;
} else if (Selector == 14) {
//CT
DATA_DTB[MCell_ADR] = 5;
Selector = 0;
} else if (Selector == 15) {
//CR
DATA_DTB[MCell_ADR] = 6;
Selector = 0;
} else if (Selector == 16) {
//ET
DATA_DTB[MCell_ADR] = 7;
Selector = 0;
}
if (Message == "DA") {
Selector = 1;
} else if (Message == "DB") {
Selector = 2;
} else if (Message == "AD") {
Selector = 3;
} else if (Message == "VA") {
Selector = 4;
} else if (Message == "CL") {
Selector = 5;
} else if (Message == "ST") {
Selector = 6;
} else if (Message == "AN") {
Selector = 7;
} else if (Message == "OR") {
Selector = 8;
} else if (Message == "OT") {
Selector = 9;
} else if (Message == "IN") {
Selector = 10;
} else if (Message == "QT") {
Selector = 11;
} else if (Message == "ME") {
Selector = 12;
} else if (Message == "TM") {
Selector = 13;
} else if (Message == "CT") {
Selector = 14;
} else if (Message == "CR") {
Selector = 15;
} else if (Message == "ET") {
Selector = 16;
}
if (Message == "WRITE") {
//Sezione non funzionante
EEPROM_Write(DATA_DTA, Num_Cell, 0);
EEPROM_Write(DATA_DTB, Num_Cell, Num_Cell);
EEPROM_Write(DATA_ADR, Num_Cell, (Num_Cell * 2));
EEPROM_Write(DATA_VAL, Num_Cell, (Num_Cell * 3));
Serial.println("ROM OK");
}
}
}
// S I O M T C CR AN OR NT V AD CI CADR
//Cell(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
//Cell(3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[0], 1);
//Cell(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[1], 2);
//Cell(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[2], 3);
//Cell(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[3], 4);
//Cell(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[4], 5);
//Cell_PRG(1, 1, 0, 0, 0, 0, 0);
//Cell_PRG(4, 2, 0, 0, 0, Cell_Mem[0], 1);
//Cell_PRG(0, 0, 0, 0, 0, Cell_Mem[1], 2);
//Cell_PRG(0, 0, 0, 0, 0, Cell_Mem[2], 3);
//Cell_PRG(0, 0, 0, 0, 0, Cell_Mem[3], 4);
//Cell_PRG(0, 0, 0, 0, 0, Cell_Mem[4], 5);
for (byte i = 0; i < Num_Cell; i++) {
if (i < 1) {
Cell_PRG(DATA_DTA[0], DATA_DTB[0], DATA_ADR[0], DATA_VAL[0], 0, 0);
} else {
Cell_PRG(DATA_DTA[i], DATA_DTB[i], DATA_ADR[i], DATA_VAL[i], Cell_Mem[i - 1], i);
}
}
Write_Output();
if (Counter_Led < Counter_Led_Value) {
Counter_Led++;
} else {
Counter_Led = 0;
digitalWrite(Debug_Led, !digitalRead(Debug_Led));
}
}
File B V2 (Funzioni):
void Cell_PRG(byte CODE_A, byte CODE_B, byte PADR, byte PVALUE, bool PCELL_IN, byte PCELL_ADR) {
bool PEN_In = 0, PEN_Out = 0, PEN_Mem = 0, PEN_Timer = 0, PEN_Counter = 0, PEN_Counter_R = 0, PEN_Edge = 0, PEN_And_Or = 0, PEN_Not_MM = 0;
byte PSTRT = 0;
if (CODE_A == 1) {
//START
PSTRT = 1;
} else if (CODE_A == 2) {
//AND
PSTRT = 2;
PEN_And_Or = 1;
} else if (CODE_A == 3) {
//OR
PSTRT = 2;
PEN_And_Or = 0;
} else if (CODE_A == 4) {
//OUT
PSTRT = 3;
}
if (CODE_B == 1) {
//IN
PEN_In = 1;
} else if (CODE_B == 2) {
//OUT
PEN_Out = 1;
} else if (CODE_B == 3) {
//MEM
PEN_Mem = 1;
} else if (CODE_B == 4) {
//TM
PEN_Timer = 1;
} else if (CODE_B == 5) {
//CT
PEN_Counter = 1;
} else if (CODE_B == 6) {
//CR
PEN_Counter_R = 1;
} else if (CODE_B == 7) {
//EG
PEN_Edge = 1;
}
Cell(PSTRT, PEN_In, PEN_Out, PEN_Mem, PEN_Timer, PEN_Counter, PEN_Counter_R, PEN_Edge, PEN_And_Or, PVALUE, PADR, PCELL_IN, PCELL_ADR);
}
void Cell(byte STRT, bool EN_In, bool EN_Out, bool EN_Mem, bool EN_Timer, bool EN_Counter, bool EN_Counter_R, bool EN_Edge, bool EN_And_Or, byte VALUE, byte ADR, bool CELL_IN, byte CELL_ADR) {
bool SEL_OUT = 0, SEL_AND = 0, SEL_OR = 0, SEL_NOT = 0;
if (STRT == 1) {
//START
SEL_OUT = (In[ADR] and EN_In) or (Out[ADR] and EN_Out) or (Mem[ADR] and EN_Mem) or (Timer_Out[ADR] and EN_Timer) or (Counter_Out[ADR] and EN_Counter) or (Edge_Out[ADR] and EN_Edge);
if (VALUE == 0) {
Cell_Mem[CELL_ADR] = SEL_OUT;
} else if (VALUE == 1) {
Cell_Mem[CELL_ADR] = !SEL_OUT;
}
} else if (STRT == 2) {
//LOGIC OPERTOR AND - OR
SEL_OUT = (In[ADR] and EN_In) or (Out[ADR] and EN_Out) or (Mem[ADR] and EN_Mem) or (Timer_Out[ADR] and EN_Timer) or (Counter_Out[ADR] and EN_Counter) or (Edge_Out[ADR] and EN_Edge);
if (VALUE == 0) {
SEL_NOT = SEL_OUT;
} else if (VALUE == 1) {
SEL_NOT = !SEL_OUT;
}
SEL_AND = (SEL_NOT and CELL_IN);
SEL_OR = (SEL_NOT or CELL_IN);
Cell_Mem[CELL_ADR] = ((SEL_AND and EN_And_Or) or (SEL_OR and !EN_And_Or));
} else if (STRT == 3) {
//OUTPUT LOGIC
if (EN_Out == 1) {
Out[ADR] = CELL_IN;
}
if (EN_Mem == 1) {
Mem[ADR] = CELL_IN;
}
if (EN_Timer == 1) {
Timer(CELL_IN, VALUE, ADR);
}
if (EN_Counter == 1) {
Counter(CELL_IN, VALUE, ADR);
}
if (EN_Counter_R == 1) {
Counter_Reset(CELL_IN, ADR);
}
if (EN_Edge == 1) {
Edge(CELL_IN, VALUE, ADR);
}
Cell_Mem[CELL_ADR] = CELL_IN;
}
}
void Timer(bool SET, byte VALUE, byte ADR) {
if (SET == 1) {
if (Timer_Data[ADR] < VALUE * 10) {
Timer_Data[ADR]++;
} else {
Timer_Out[ADR] = 1;
}
} else {
Timer_Out[ADR] = 0;
Timer_Data[ADR] = 0;
}
}
void Counter(bool SET, byte VALUE, byte ADR) {
if (SET != Counter_Mem[ADR]) {
if (SET == 1) {
if (Counter_Data[ADR] < VALUE) {
Counter_Data[ADR]++;
}
if (Counter_Data[ADR] >= VALUE) {
Counter_Out[ADR] = 1;
}
}
Counter_Mem[ADR] = SET;
}
}
void Counter_Reset(bool RESET, byte ADR) {
if (RESET == 1) {
Counter_Out[ADR] = 0;
Counter_Data[ADR] = 0;
}
}
void Edge(bool IN_EDGE, int FUC, byte ADR) {
bool Rising_Edge = 0, Falling_Edge = 0;
if (FUC == 0 or FUC == 0) {
Rising_Edge = 1;
Falling_Edge = 0;
} else if (FUC == 1 or FUC == 1) {
Rising_Edge = 0;
Falling_Edge = 1;
} else if (FUC == 2 or FUC == 2) {
Rising_Edge = 1;
Falling_Edge = 1;
}
Edge_Out[ADR] = (IN_EDGE and !Edge_Mem[ADR] and Rising_Edge) or (!IN_EDGE and Edge_Mem[ADR] and Falling_Edge);
Edge_Mem[ADR] = IN_EDGE;
return Edge_Out;
}
//Lettura su EEPROM (ESTERNA)
byte EEPROM_Read(int ADR) {
byte rData = 0xFF;
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(ADR >> 8)); // MSB
Wire.write((int)(ADR & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);
rData = Wire.read();
return rData;
}
//Scrittura su EEPROM (ESTERNA)
void EEPROM_Write(byte DATA[], byte DATA_ADR, int ADR) {
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(ADR >> 8)); // MSB
Wire.write((int)(ADR & 0xFF)); // LSB
for (byte i = 0; i < DATA_ADR; i++) {
Wire.write(DATA[i]);
}
Wire.endTransmission();
delay(10);
}
void Read_Input() {
for (byte i = 0; i < Num_In; i++) {
In[i] = digitalRead(Pin_In[i]);
}
}
void Write_Output() {
for (byte i = 0; i < Num_Out; i++) {
digitalWrite(Pin_Out[i], Out[i]);
}
}
File A V3:
#include <EEPROM.h>
byte Pin_In[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
byte Pin_Out[] = { 11, 12, A0, A1, A2, A3, A4, A5 };
#define Debug_Led 13
bool In[8], Out[8];
#define Num_Mem 32
#define Num_Timer 32
#define Num_Counter 16
#define Num_Edge 32
bool Mem[Num_Mem];
bool Timer_Out[Num_Timer];
int Timer_Data[Num_Timer];
bool Counter_Out[Num_Counter], Counter_Mem[Num_Counter];
byte Counter_Data[Num_Counter];
bool Edge_Out[Num_Edge], Edge_Mem[Num_Edge];
#define Num_Cell 100
bool Cell_Mem[Num_Cell];
//DATA_DTA
// 1 ST, 2 AND, 3 OR, 4 OUT
//DATA_DTB
// 1 IN, 2 OUT, 3 MEM, 4 TM, 5 CT, 6 CR, 7 EG
//NOT (VALUE)
// 0 BUF, 1 NOT
byte DATA_DTA[Num_Cell];
byte DATA_DTB[Num_Cell];
byte DATA_ADR[Num_Cell];
byte DATA_VAL[Num_Cell];
String Message;
int Message_Int;
byte MCell_ADR, Selector;
#define Counter_Led_Value 20
int Counter_Led;
void setup() {
delay(10);
for (byte i = 0; i < 8; i++) {
pinMode(Pin_In[i], INPUT);
pinMode(Pin_Out[i], OUTPUT);
}
pinMode(Debug_Led, OUTPUT);
//Wire.begin();
Serial.begin(115200);
for (byte i = 0; i < Num_Cell; i++) {
DATA_DTA[i] = EEPROM.read(i);
DATA_DTB[i] = EEPROM.read(i + Num_Cell);
DATA_ADR[i] = EEPROM.read(i + (Num_Cell * 2));
DATA_VAL[i] = EEPROM.read(i + (Num_Cell * 3));
}
delay(100);
}
void loop() {
delay(1);
Read_Input();
if (Serial.available() > 0) {
while (Serial.available() > 0) {
Message = Serial.readStringUntil('x');
Message_Int = Message.toInt();
//Serial.print(Message_Int);
if (Selector == 1) {
DATA_DTA[MCell_ADR] = Message_Int;
//Serial.print("DATA A -> ");
//Serial.println(DATA_DTA[MCell_ADR]);
Selector = 0;
} else if (Selector == 2) {
DATA_DTB[MCell_ADR] = Message_Int;
//Serial.print("DATA B -> ");
//Serial.println(DATA_DTB[MCell_ADR]);
Selector = 0;
} else if (Selector == 3) {
DATA_ADR[MCell_ADR] = Message_Int;
//Serial.print("DATA A -> ");
//Serial.println(DATA_ADR[MCell_ADR]);
Selector = 0;
} else if (Selector == 4) {
DATA_VAL[MCell_ADR] = Message_Int;
//Serial.print("DATA A -> ");
//Serial.println(DATA_VAL[MCell_ADR]);
Selector = 0;
} else if (Selector == 5) {
MCell_ADR = Message_Int;
//Serial.print("CELL -> ");
//Serial.println(MCell_ADR);
Selector = 0;
}
if (Selector == 6) {
//ST
DATA_DTA[MCell_ADR] = 1;
Selector = 0;
} else if (Selector == 7) {
//AND
DATA_DTA[MCell_ADR] = 2;
Selector = 0;
} else if (Selector == 8) {
//OR
DATA_DTA[MCell_ADR] = 3;
Selector = 0;
} else if (Selector == 9) {
//OUT
DATA_DTA[MCell_ADR] = 4;
Selector = 0;
}
if (Selector == 10) {
//IN
DATA_DTB[MCell_ADR] = 1;
Selector = 0;
} else if (Selector == 11) {
//Q
DATA_DTB[MCell_ADR] = 2;
Selector = 0;
} else if (Selector == 12) {
//MEM
DATA_DTB[MCell_ADR] = 3;
Selector = 0;
} else if (Selector == 13) {
//TM
DATA_DTB[MCell_ADR] = 4;
Selector = 0;
} else if (Selector == 14) {
//CT
DATA_DTB[MCell_ADR] = 5;
Selector = 0;
} else if (Selector == 15) {
//CR
DATA_DTB[MCell_ADR] = 6;
Selector = 0;
} else if (Selector == 16) {
//ET
DATA_DTB[MCell_ADR] = 7;
Selector = 0;
}
if (Message == "DA") {
Selector = 1;
} else if (Message == "DB") {
Selector = 2;
} else if (Message == "AD") {
Selector = 3;
} else if (Message == "VA") {
Selector = 4;
} else if (Message == "CL") {
Selector = 5;
} else if (Message == "ST") {
Selector = 6;
} else if (Message == "AN") {
Selector = 7;
} else if (Message == "OR") {
Selector = 8;
} else if (Message == "OT") {
Selector = 9;
} else if (Message == "IN") {
Selector = 10;
} else if (Message == "QT") {
Selector = 11;
} else if (Message == "ME") {
Selector = 12;
} else if (Message == "TM") {
Selector = 13;
} else if (Message == "CT") {
Selector = 14;
} else if (Message == "CR") {
Selector = 15;
} else if (Message == "ET") {
Selector = 16;
}
if (Message == "WRITE") {
for (int i = 0; i < Num_Cell; i++) {
EEPROM.write(i, DATA_DTA[i]);
EEPROM.write(i + Num_Cell, DATA_DTB[i]);
EEPROM.write(i + (Num_Cell * 2), DATA_ADR[i]);
EEPROM.write(i + (Num_Cell * 3), DATA_VAL[i]);
}
Serial.println("ROM WRITE");
} else if (Message == "CLEAR") {
for (int i = 0 ; i < EEPROM.length() ; i++) {
EEPROM.write(i, 0);
}
Serial.println("ROM CLEAR");
}
}
}
// S I O M T C CR AN OR NT V AD CI CADR
//Cell(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
//Cell(3, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[0], 1);
//Cell(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[1], 2);
//Cell(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[2], 3);
//Cell(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[3], 4);
//Cell(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Cell_Mem[4], 5);
//Cell_PRG(1, 1, 0, 0, 0, 0, 0);
//Cell_PRG(4, 2, 0, 0, 0, Cell_Mem[0], 1);
//Cell_PRG(0, 0, 0, 0, 0, Cell_Mem[1], 2);
//Cell_PRG(0, 0, 0, 0, 0, Cell_Mem[2], 3);
//Cell_PRG(0, 0, 0, 0, 0, Cell_Mem[3], 4);
//Cell_PRG(0, 0, 0, 0, 0, Cell_Mem[4], 5);
for (byte i = 0; i < Num_Cell; i++) {
if (i < 1) {
Cell_PRG(DATA_DTA[0], DATA_DTB[0], DATA_ADR[0], DATA_VAL[0], 0, 0);
} else {
Cell_PRG(DATA_DTA[i], DATA_DTB[i], DATA_ADR[i], DATA_VAL[i], Cell_Mem[i - 1], i);
}
}
Write_Output();
if (Counter_Led < Counter_Led_Value) {
Counter_Led++;
} else {
Counter_Led = 0;
digitalWrite(Debug_Led, !digitalRead(Debug_Led));
}
}
File B V3 (Funzioni):
void Cell_PRG(byte CODE_A, byte CODE_B, byte PADR, byte PVALUE, bool PCELL_IN, byte PCELL_ADR) {
bool PEN_In = 0, PEN_Out = 0, PEN_Mem = 0, PEN_Timer = 0, PEN_Counter = 0, PEN_Counter_R = 0, PEN_Edge = 0, PEN_And_Or = 0, PEN_Not_MM = 0;
byte PSTRT = 0;
if (CODE_A == 1) {
//START
PSTRT = 1;
} else if (CODE_A == 2) {
//AND
PSTRT = 2;
PEN_And_Or = 1;
} else if (CODE_A == 3) {
//OR
PSTRT = 2;
PEN_And_Or = 0;
} else if (CODE_A == 4) {
//OUT
PSTRT = 3;
}
if (CODE_B == 1) {
//IN
PEN_In = 1;
} else if (CODE_B == 2) {
//OUT
PEN_Out = 1;
} else if (CODE_B == 3) {
//MEM
PEN_Mem = 1;
} else if (CODE_B == 4) {
//TM
PEN_Timer = 1;
} else if (CODE_B == 5) {
//CT
PEN_Counter = 1;
} else if (CODE_B == 6) {
//CR
PEN_Counter_R = 1;
} else if (CODE_B == 7) {
//EG
PEN_Edge = 1;
}
Cell(PSTRT, PEN_In, PEN_Out, PEN_Mem, PEN_Timer, PEN_Counter, PEN_Counter_R, PEN_Edge, PEN_And_Or, PVALUE, PADR, PCELL_IN, PCELL_ADR);
}
void Cell(byte STRT, bool EN_In, bool EN_Out, bool EN_Mem, bool EN_Timer, bool EN_Counter, bool EN_Counter_R, bool EN_Edge, bool EN_And_Or, byte VALUE, byte ADR, bool CELL_IN, byte CELL_ADR) {
bool SEL_OUT = 0, SEL_AND = 0, SEL_OR = 0, SEL_NOT = 0;
if (STRT == 1) {
//START
SEL_OUT = (In[ADR] and EN_In) or (Out[ADR] and EN_Out) or (Mem[ADR] and EN_Mem) or (Timer_Out[ADR] and EN_Timer) or (Counter_Out[ADR] and EN_Counter) or (Edge_Out[ADR] and EN_Edge);
if (VALUE == 0) {
Cell_Mem[CELL_ADR] = SEL_OUT;
} else if (VALUE == 1) {
Cell_Mem[CELL_ADR] = !SEL_OUT;
}
} else if (STRT == 2) {
//LOGIC OPERTOR AND - OR
SEL_OUT = (In[ADR] and EN_In) or (Out[ADR] and EN_Out) or (Mem[ADR] and EN_Mem) or (Timer_Out[ADR] and EN_Timer) or (Counter_Out[ADR] and EN_Counter) or (Edge_Out[ADR] and EN_Edge);
if (VALUE == 0) {
SEL_NOT = SEL_OUT;
} else if (VALUE == 1) {
SEL_NOT = !SEL_OUT;
}
SEL_AND = (SEL_NOT and CELL_IN);
SEL_OR = (SEL_NOT or CELL_IN);
Cell_Mem[CELL_ADR] = ((SEL_AND and EN_And_Or) or (SEL_OR and !EN_And_Or));
} else if (STRT == 3) {
//OUTPUT LOGIC
if (EN_Out == 1) {
Out[ADR] = CELL_IN;
}
if (EN_Mem == 1) {
Mem[ADR] = CELL_IN;
}
if (EN_Timer == 1) {
Timer(CELL_IN, VALUE, ADR);
}
if (EN_Counter == 1) {
Counter(CELL_IN, VALUE, ADR);
}
if (EN_Counter_R == 1) {
Counter_Reset(CELL_IN, ADR);
}
if (EN_Edge == 1) {
Edge(CELL_IN, VALUE, ADR);
}
Cell_Mem[CELL_ADR] = CELL_IN;
}
}
void Timer(bool SET, byte VALUE, byte ADR) {
if (SET == 1) {
if (Timer_Data[ADR] < VALUE * 10) {
Timer_Data[ADR]++;
} else {
Timer_Out[ADR] = 1;
}
} else {
Timer_Out[ADR] = 0;
Timer_Data[ADR] = 0;
}
}
void Counter(bool SET, byte VALUE, byte ADR) {
if (SET != Counter_Mem[ADR]) {
if (SET == 1) {
if (Counter_Data[ADR] < VALUE) {
Counter_Data[ADR]++;
}
if (Counter_Data[ADR] >= VALUE) {
Counter_Out[ADR] = 1;
}
}
Counter_Mem[ADR] = SET;
}
}
void Counter_Reset(bool RESET, byte ADR) {
if (RESET == 1) {
Counter_Out[ADR] = 0;
Counter_Data[ADR] = 0;
}
}
void Edge(bool IN_EDGE, int FUC, byte ADR) {
bool Rising_Edge = 0, Falling_Edge = 0;
if (FUC == 0 or FUC == 0) {
Rising_Edge = 1;
Falling_Edge = 0;
} else if (FUC == 1 or FUC == 1) {
Rising_Edge = 0;
Falling_Edge = 1;
} else if (FUC == 2 or FUC == 2) {
Rising_Edge = 1;
Falling_Edge = 1;
}
Edge_Out[ADR] = (IN_EDGE and !Edge_Mem[ADR] and Rising_Edge) or (!IN_EDGE and Edge_Mem[ADR] and Falling_Edge);
Edge_Mem[ADR] = IN_EDGE;
return Edge_Out;
}
void Read_Input() {
for (byte i = 0; i < 8; i++) {
In[i] = digitalRead(Pin_In[i]);
}
}
void Write_Output() {
for (byte i = 0; i < 8; i++) {
digitalWrite(Pin_Out[i], Out[i]);
}
}