You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.0 KiB
62 lines
1.0 KiB
1 year ago
|
// Für Controllino könnte man benutzen: sRx = 20, sTx = 21, enable = 53
|
||
|
|
||
1 year ago
|
int sTx = 2;
|
||
|
int sRx = 3;
|
||
1 year ago
|
|
||
1 year ago
|
long base = 100;
|
||
1 year ago
|
long rst = base*10; // reset
|
||
|
boolean bt = false;
|
||
|
long dif = 0;
|
||
|
long lt = 0;
|
||
|
long now = 0;
|
||
|
char rcv = 0;
|
||
|
int idx = 0;
|
||
|
String message;
|
||
1 year ago
|
void setup(){
|
||
1 year ago
|
pinMode(sRx,INPUT);
|
||
|
pinMode(sTx,OUTPUT);
|
||
1 year ago
|
attachInterrupt(digitalPinToInterrupt(sRx),change485,CHANGE);
|
||
1 year ago
|
Serial.begin(115200);
|
||
|
Serial.println("Receiver ready");
|
||
|
}
|
||
|
|
||
1 year ago
|
void reset485(long dif){
|
||
1 year ago
|
rcv = 0;
|
||
|
idx = -1;
|
||
|
message.reserve(32);
|
||
|
}
|
||
1 year ago
|
void change485(){
|
||
1 year ago
|
bool bt = digitalRead(sRx);
|
||
|
now = micros();
|
||
|
dif = now - lt;
|
||
|
lt = now;
|
||
|
if (dif > rst){
|
||
1 year ago
|
reset485(dif);
|
||
1 year ago
|
} else {
|
||
1 year ago
|
handle485(!bt,dif);
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
void handle485(boolean bt, long dif){
|
||
1 year ago
|
long count = dif/base;
|
||
1 year ago
|
for (int i=0; i<count; i++) push485(bt);
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
1 year ago
|
void push485(boolean bt){
|
||
1 year ago
|
if (idx>=0) rcv |= bt<<idx;
|
||
|
idx++;
|
||
|
|
||
|
if (idx == 8){
|
||
|
if (rcv == 13) {
|
||
|
Serial.println(message);
|
||
|
message = "";
|
||
|
} else {
|
||
|
message+=rcv;
|
||
|
}
|
||
|
idx =0;
|
||
|
rcv =0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void loop(){}
|