started BiDi (for bi-directional communication)
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
95
BiDi/BiDi.ino
Normal file
95
BiDi/BiDi.ino
Normal file
@@ -0,0 +1,95 @@
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
85
ManualSend/ManualSend.ino
Normal file
85
ManualSend/ManualSend.ino
Normal file
@@ -0,0 +1,85 @@
|
||||
/*// This sketch allows manual setting of the RS485-connected pins via serial terminal commands
|
||||
|
||||
Commands are:
|
||||
r+ – enable read mode (^RE ← LOW)
|
||||
r- – disable read mode (^RE ← HIGH)
|
||||
w+ – enable write mode (DE ← HIGH)
|
||||
w- – disable write mode (DE ← LOW)
|
||||
h – send HIGH level on DI
|
||||
l – send LOW level on DI
|
||||
|
||||
//*/
|
||||
|
||||
#if defined(ARDUINO_AVR_UNO)
|
||||
#define RS485_RO 5
|
||||
#define RS485_RE 4
|
||||
#define RS485_DE 3
|
||||
#define RS485_DI 2
|
||||
#define BOARD "Arduino Uno"
|
||||
#endif
|
||||
#if defined(ARDUINO_AVR_NANO)
|
||||
#define RS485_RO 3
|
||||
#define RS485_RE 2
|
||||
#define RS485_DE 4
|
||||
#define RS485_DI 5
|
||||
#define BOARD "Arduino Nano"
|
||||
#endif
|
||||
|
||||
String command = "";
|
||||
bool doPrint = false;
|
||||
// the setup function runs once when you press reset or power the board
|
||||
void setup() {
|
||||
// initialize digital pin LED_BUILTIN as an output.
|
||||
pinMode(RS485_RO, INPUT);
|
||||
pinMode(RS485_RE, OUTPUT);
|
||||
pinMode(RS485_DE, OUTPUT);
|
||||
pinMode(RS485_DI, OUTPUT);
|
||||
digitalWrite(RS485_RE,LOW);
|
||||
digitalWrite(RS485_DE,LOW);
|
||||
digitalWrite(RS485_DI,LOW);
|
||||
Serial.begin(115200);
|
||||
Serial.println(F("this is Projekte/Arduino/MAX485/ManualSend"));
|
||||
Serial.println(F("send any of the followning commands:"));
|
||||
Serial.println(F("- r+ (enable read)\n- r- (disable read)\n- w+ (enable write)\n- w- (disable write)\n- h (for high)\n- l (for low)"));
|
||||
}
|
||||
|
||||
// the loop function runs over and over again forever
|
||||
void loop() {
|
||||
while (Serial.available()){
|
||||
char inChar = (char)Serial.read();
|
||||
if (inChar == '\n') {
|
||||
process(command);
|
||||
command = "";
|
||||
doPrint = true;
|
||||
} else {
|
||||
command += inChar;
|
||||
}
|
||||
}
|
||||
if (doPrint){
|
||||
printState();
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void printState(){
|
||||
Serial.print("RE = ");
|
||||
Serial.print(1-digitalRead(RS485_RE));
|
||||
Serial.print(", DE = ");
|
||||
Serial.print(digitalRead(RS485_DE));
|
||||
Serial.print(", DI = ");
|
||||
Serial.print(digitalRead(RS485_DI));
|
||||
Serial.print(", RO = ");
|
||||
Serial.println(digitalRead(RS485_RO));
|
||||
}
|
||||
|
||||
void process(String command){
|
||||
Serial.print(F("received:"));
|
||||
Serial.println(command);
|
||||
if (command == "r+") digitalWrite(RS485_RE,LOW);
|
||||
if (command == "r-") digitalWrite(RS485_RE,HIGH);
|
||||
if (command == "w+") digitalWrite(RS485_DE,HIGH);
|
||||
if (command == "w-") digitalWrite(RS485_DE,LOW);
|
||||
if (command == "h") digitalWrite(RS485_DI,HIGH);
|
||||
if (command == "l") digitalWrite(RS485_DI,LOW);
|
||||
printState();
|
||||
}
|
||||
4
ManualSend/Readme.md
Normal file
4
ManualSend/Readme.md
Normal file
@@ -0,0 +1,4 @@
|
||||
Erkenntnisse:
|
||||
|
||||
1. Wenn alle Treiber inaktiv sind, also bei allen Teilnehmer DI = Low ist, kommt vom RO ein High-Signal
|
||||
2. das High-Signal ist dominant, d.h. wenn mehrere Teilnehmer senden, und mindestens ein Teilnehmer eine 1 auf den Bus legt, erhalten alle am RO einen HIGH-Pegel
|
||||
Reference in New Issue
Block a user