2 changed files with 6 additions and 134 deletions
@ -1,129 +0,0 @@
@@ -1,129 +0,0 @@
|
||||
// Breadboard
|
||||
//int enable = 2;
|
||||
//int sTx = 4;
|
||||
//int sRx = 5;
|
||||
|
||||
// RS485-Nano Revision 1.0
|
||||
int enable = 9; |
||||
int sTx = 8; |
||||
int sRx = 3; |
||||
|
||||
// Für Controllino könnte man benutzen: sRx = 20, sTx = 21, enable = 53
|
||||
|
||||
int in1 = 4; // o
|
||||
int in2 = 5; // |o
|
||||
int in3 = 6; // ||o
|
||||
int in4 = 7; // |||o
|
||||
// ↓↓↓↓
|
||||
int irq = 2;// ←++++
|
||||
|
||||
long base = 1000; |
||||
|
||||
int id = 0; |
||||
int baud = 115200; |
||||
char c; |
||||
int counter = 0; |
||||
bool reg1=false,reg2=false,reg3=false,reg4=false; |
||||
|
||||
void setup(){ |
||||
Serial.begin(baud);
|
||||
pinMode(A0,INPUT); |
||||
id = analogRead(A0); |
||||
|
||||
pinMode(enable,OUTPUT); |
||||
digitalWrite(enable,LOW); |
||||
|
||||
pinMode(sTx,OUTPUT); |
||||
digitalWrite(sTx,LOW); |
||||
|
||||
pinMode(in1,INPUT); |
||||
pinMode(in2,INPUT); |
||||
pinMode(in3,INPUT); |
||||
pinMode(in4,INPUT); |
||||
|
||||
pinMode(irq,INPUT); |
||||
attachInterrupt(digitalPinToInterrupt(irq),interrupt,RISING); |
||||
|
||||
pinMode(sRx,INPUT); |
||||
Serial.print("Baud rate set to "); |
||||
Serial.println(baud); |
||||
Serial.print("Initialized Arduino #"); |
||||
Serial.println(id); |
||||
} |
||||
|
||||
void interrupt(){ |
||||
Serial.println("Interrupt"); |
||||
reg1 = digitalRead(in1); |
||||
reg2 = digitalRead(in2); |
||||
reg3 = digitalRead(in3); |
||||
reg4 = digitalRead(in4); |
||||
|
||||
} |
||||
bool sendBit(bool bt){ |
||||
digitalWrite(sTx,bt); |
||||
delayMicroseconds(80); |
||||
if (digitalRead(sTx) != bt) return false; |
||||
delayMicroseconds(base-80); |
||||
return true; |
||||
} |
||||
|
||||
bool sendChar(char c){ |
||||
return sendBit(c & 0x01) |
||||
&& sendBit(c & 0x02) |
||||
&& sendBit(c & 0x04) |
||||
&& sendBit(c & 0x08) |
||||
&& sendBit(c & 0x10) |
||||
&& sendBit(c & 0x20) |
||||
&& sendBit(c & 0x40) |
||||
&& sendBit(c & 0x80); |
||||
} |
||||
|
||||
bool sendString(String s){ |
||||
Serial.print("Sending '"+s+"'…"); |
||||
boolean busy = false; |
||||
for (int i = 0; i<11; i++){ |
||||
if (!digitalRead(sRx)) { |
||||
if (!busy){ |
||||
Serial.print("busy…"); |
||||
busy = true; |
||||
} |
||||
i = 0; // wait for free line
|
||||
} |
||||
delayMicroseconds(base); |
||||
} |
||||
digitalWrite(enable,HIGH); |
||||
bool success = sendBit(0); // erste Flanke erzeugen
|
||||
if (success) { |
||||
for (char c : s){ |
||||
if (!sendChar(c)){ |
||||
success = false; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
success = success && sendChar(13); |
||||
success = success && sendBit(1); // letzte Flanke erzeugen
|
||||
digitalWrite(enable,LOW); |
||||
Serial.println(success ? "success" : "failed"); |
||||
return success; |
||||
} |
||||
|
||||
void loop(){ |
||||
if (reg1){ |
||||
reg1 = false; |
||||
sendString("t:02:01"); |
||||
} |
||||
if (reg2){ |
||||
reg2 = false; |
||||
sendString("t:02:02"); |
||||
} |
||||
if (reg3){ |
||||
reg3 = false; |
||||
sendString("t:02:03"); |
||||
} |
||||
if (reg4){ |
||||
reg4 = false; |
||||
sendString("t:02:04"); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue