Compare commits
23 Commits
nano-revis
...
main
Author | SHA1 | Date |
---|---|---|
Stephan Richter | 4915c7bf51 | 4 weeks ago |
Stephan Richter | a681d189dc | 4 weeks ago |
Stephan Richter | 2af1db36ec | 1 month ago |
Stephan Richter | e9afd6fcce | 1 month ago |
Stephan Richter | 8bdcfb6032 | 1 month ago |
Stephan Richter | 6a2184f33d | 2 months ago |
Stephan Richter | b6723b92fc | 2 months ago |
Stephan Richter | 769786e63a | 4 months ago |
Stephan Richter | 44fd0050e3 | 4 months ago |
Stephan Richter | 6e8016e718 | 5 months ago |
Stephan Richter | d65586ba0b | 5 months ago |
Stephan Richter | ef2488363d | 6 months ago |
Stephan Richter | a54897a4b5 | 6 months ago |
Stephan Richter | fcbde966e3 | 10 months ago |
Stephan Richter | 70425f8d22 | 10 months ago |
Stephan Richter | d0528773ec | 10 months ago |
Stephan Richter | 8e02709b32 | 10 months ago |
Stephan Richter | 65501447fd | 10 months ago |
Stephan Richter | 247f061c2f | 10 months ago |
Stephan Richter | 3661309081 | 11 months ago |
Stephan Richter | 0ce4f0cd4d | 11 months ago |
Stephan Richter | ca66b1c380 | 11 months ago |
Stephan Richter | 594e8ea06c | 11 months ago |
9 changed files with 42 additions and 192 deletions
Binary file not shown.
@ -1,95 +0,0 @@ |
|||||||
/* RS 485-Implementierung, die sowohl Schreiben als auch Lesen kann */ |
|
||||||
/* Basis für andere Projekte */ |
|
||||||
|
|
||||||
#define BUTTON 2 |
|
||||||
#define RECEIVE 3 |
|
||||||
#define RECEIVE_ENABLE 4 |
|
||||||
#define SEND 5 |
|
||||||
#define SEND_ENABLE 6 |
|
||||||
|
|
||||||
#define MAX_LEN 128 |
|
||||||
|
|
||||||
void setup() { |
|
||||||
// Buttons connected to analog input lines
|
|
||||||
pinMode(A0,INPUT_PULLUP); |
|
||||||
pinMode(A1,INPUT_PULLUP); |
|
||||||
pinMode(A2,INPUT_PULLUP); |
|
||||||
pinMode(A3,INPUT_PULLUP); |
|
||||||
pinMode(A4,INPUT_PULLUP); |
|
||||||
pinMode(A5,INPUT_PULLUP); |
|
||||||
pinMode(A6,INPUT_PULLUP); |
|
||||||
pinMode(A7,INPUT_PULLUP); |
|
||||||
// Currently, all buttons are also connected via a diode to an interruptable pin, to notify on input changes.
|
|
||||||
// This interruptable pin should be assgined to BUTTON
|
|
||||||
pinMode(BUTTON,INPUT_PULLUP); |
|
||||||
attachInterrupt(digitalPinToInterrupt(BUTTON),button,FALLING); // interrupt for button change
|
|
||||||
|
|
||||||
// Set up pins for RS485 connection
|
|
||||||
pinMode(RECEIVE, INPUT); |
|
||||||
pinMode(RECEIVE_ENABLE, OUTPUT); |
|
||||||
pinMode(SEND, OUTPUT); |
|
||||||
pinMode(SEND_ENABLE, OUTPUT); |
|
||||||
attachInterrupt(digitalPinToInterrupt(RECEIVE),receive,CHANGE); // interrupt for incoming RS485 traffic
|
|
||||||
|
|
||||||
reset485(); |
|
||||||
buttons = 0x0; |
|
||||||
} |
|
||||||
|
|
||||||
void button() { |
|
||||||
buttons = 0xFF ^ (digitalRead(A7)<<7 | digitalRead(A6)<<6 | digitalRead(A5)<<5 | digitalRead(A4)<<4 | digitalRead(A3)<<3 | (!digitalRead(A2))<<2 | digitalRead(A1)<<1 | digitalRead(A0));
|
|
||||||
} |
|
||||||
|
|
||||||
void receive(){ |
|
||||||
bool received_bit = digitalRead(RECEIVE); |
|
||||||
curr = micros(); |
|
||||||
duration = curr - last; |
|
||||||
Serial.print("Input switched to "); |
|
||||||
Serial.print(received_bit?"H":"L");
|
|
||||||
Serial.print(" after "); |
|
||||||
Serial.print(duration); |
|
||||||
Serial.println(" ticks"); |
|
||||||
last = curr; |
|
||||||
if (duration > MAX_TICKS) reset485(); |
|
||||||
received_bit = !received_bit; // received_bit is the current value, the duration of the previous value (=inverted) has been measured
|
|
||||||
duration /= BASE; |
|
||||||
while (duration > 0){ |
|
||||||
byte_idx--; |
|
||||||
recv |= received_bit<<byte_idx; |
|
||||||
if (byte_idx == 0) { |
|
||||||
msg[msg_idx] = recv; |
|
||||||
msg_idx++; |
|
||||||
byte_idx = 8; |
|
||||||
if (recv == 0x00) message_received(); |
|
||||||
} |
|
||||||
duration--; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void reset485(){ |
|
||||||
byte_idx = 8; |
|
||||||
msg_idx = 0; |
|
||||||
recv=0x0; |
|
||||||
} |
|
||||||
|
|
||||||
void message_received(){ |
|
||||||
for (int i=0; i<MAX_LEN; i++) message[i]=msg[i]; |
|
||||||
reset485(); |
|
||||||
} |
|
||||||
|
|
||||||
void button_pressed(int i){ |
|
||||||
Serial.println("not implemented"); |
|
||||||
} |
|
||||||
|
|
||||||
void loop(){ |
|
||||||
if (buttons>0){ |
|
||||||
for (int i=0; i<7; i++){ |
|
||||||
if (buttons & (1<<i)) button_pressed(i);
|
|
||||||
}
|
|
||||||
} |
|
||||||
if (message[0] != 0x0){ |
|
||||||
Serial.print("Received '"); |
|
||||||
Serial.print(message); |
|
||||||
Serial.println("'");
|
|
||||||
message[0] = 0x0; |
|
||||||
} |
|
||||||
} |
|
@ -1,61 +0,0 @@ |
|||||||
// Für Controllino könnte man benutzen: sRx = 20, sTx = 21, enable = 53
|
|
||||||
|
|
||||||
int sTx = 2; |
|
||||||
int sRx = 3; |
|
||||||
|
|
||||||
long base = 100; |
|
||||||
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; |
|
||||||
void setup(){ |
|
||||||
pinMode(sRx,INPUT); |
|
||||||
pinMode(sTx,OUTPUT); |
|
||||||
attachInterrupt(digitalPinToInterrupt(sRx),change485,CHANGE); |
|
||||||
Serial.begin(115200);
|
|
||||||
Serial.println("Receiver ready"); |
|
||||||
} |
|
||||||
|
|
||||||
void reset485(long dif){ |
|
||||||
rcv = 0; |
|
||||||
idx = -1; |
|
||||||
message.reserve(32); |
|
||||||
} |
|
||||||
void change485(){ |
|
||||||
bool bt = digitalRead(sRx); |
|
||||||
now = micros(); |
|
||||||
dif = now - lt; |
|
||||||
lt = now; |
|
||||||
if (dif > rst){ |
|
||||||
reset485(dif); |
|
||||||
} else { |
|
||||||
handle485(!bt,dif); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void handle485(boolean bt, long dif){ |
|
||||||
long count = dif/base; |
|
||||||
for (int i=0; i<count; i++) push485(bt); |
|
||||||
} |
|
||||||
|
|
||||||
void push485(boolean bt){ |
|
||||||
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(){} |
|
Loading…
Reference in new issue