const int txPin = 8;
const int txPinInput = 14;
const int rxPin = 10;
unsigned long lastTimeTx = 0; // Vorherige Millis zur Zeitmessung
unsigned long T_bitTx = 100; // Intervall in Millisekunden
unsigned long lastTimeRx = 0; // Vorherige Millis zur Zeitmessung
unsigned long T_bitRx = 101; // Intervall in Millisekunden
const int applDataSize = 8;
const int maxArraySize= 20;
int rxArraySize = 20;
bool rxArray[maxArraySize];
int currentIndexRx = 0;
int lastIndexRx = 0;
int lastRxState = 0;
int bitStuffingL = 2;
void setup() {
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
// Define Input Pins (including PullUp Resistor)
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
//Define Output Pins (LED)
pinMode(23, OUTPUT);
pinMode(25, OUTPUT);
pinMode(27, OUTPUT);
pinMode(29, OUTPUT);
pinMode(31, OUTPUT);
pinMode(33, OUTPUT);
pinMode(35, OUTPUT);
pinMode(37, OUTPUT);
//Define BUS In- and Output
pinMode(txPin, OUTPUT);
pinMode(rxPin, INPUT);
// Definitionen für Interrupt mit 50 Hz
cli();//stop all interrupts
// turn on CTC mode
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCCR1B |= (1 << WGM12);
// Set CS11 bit for prescaler 8
//TCCR1B |= (1 << CS11);
// Set CS11 bit for prescaler 256
TCCR1B |= (1 << CS12);
//initialize counter value to 0;
TCNT1 = 0;
// set timer count for 100Hz increments
//OCR1A = 19999;// = (16*10^6) / (100*8) - 1
OCR1A = 62;// = (16*10^6) / (1000*256) - 1
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
//END TIMER SETUP
}
ISR(TIMER1_COMPA_vect) {//Interrupt at frequency of 1000 Hz
//write your timer code here
//************* Physical Layer *************************************
receiveUnipolar();
if (lastIndexRx!=currentIndexRx){
Serial.println(currentIndexRx);
lastIndexRx= currentIndexRx;
//************* Data Link Layer *************************************
//************* Application Layer *************************************
if (currentIndexRx==applDataSize){
for (int i = 0; i<applDataSize;i++){
digitalWrite(23+(2*i), rxArray[i]);
}
currentIndexRx= 0;
}
}
}
void receiveUnipolar(){
unsigned long currentMillis = millis();
bool rxState = digitalRead(rxPin);
if (rxState!= lastRxState){
lastRxState= rxState;
lastTimeRx= currentMillis-(T_bitRx/2);
}
if((currentMillis-lastTimeRx)>=T_bitRx){
lastTimeRx= millis();;
rxArray[currentIndexRx]= digitalRead(rxPin);
currentIndexRx++;
}
}
void loop() {
// put your main code here, to run repeatedly:
//************* Application Layer *************************************
// Definition des dataArrays
bool txArray[maxArraySize];
for (int i =0; i< applDataSize;i++){
txArray[i] = digitalRead(txPinInput+i);
}
//************* Data Link Layer *************************************
//************* Physical Layer *************************************
sendUnipolar(txArray, applDataSize);
}
void sendUnipolar(bool* data, int dataSize){
int i= 0;
while(i<dataSize){
if((millis()-lastTimeTx)>=T_bitTx){
lastTimeTx= millis();
digitalWrite(txPin, data[i]);
i++;
}
}
}