From 5fe0c22abb8679f6f7b401e15dba7ab0edfaa09a Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sun, 19 Nov 2023 22:45:17 +0100 Subject: [PATCH] working version? Signed-off-by: Stephan Richter --- SoftRS485.cpp | 175 +++++++++++++-------- examples/ReceiveDemo/ReceiveDemo.ino | 1 - examples/SendAndReceive/SendAndReceive.ino | 10 +- examples/TempSend/TempSend.ino | 35 +++++ 4 files changed, 149 insertions(+), 72 deletions(-) create mode 100644 examples/TempSend/TempSend.ino diff --git a/SoftRS485.cpp b/SoftRS485.cpp index 2ecb200..51df65e 100644 --- a/SoftRS485.cpp +++ b/SoftRS485.cpp @@ -6,60 +6,112 @@ #include "Arduino.h" #include "SoftRS485.h" -//#define DEBUG +//#define SEND #define MSG_LEN 64 // Pins int _RO, _RE, _DE, _DI; -long _pulse_len = 105; - +long _pulse_len = 616;// 1232; // Pulslänge von 1250µs - 18µs Zeit für Operationen +long _break = _pulse_len * 20; // Receive vars -boolean _recv_bit = false; -long _last_flip; // this is the time when the bus switched to logical zero +boolean _line = LOW; +long _last_flip = 0; // this is the time when the bus switched to logical zero byte _recv_msg[MSG_LEN]; -int _recv_pos = 0; -int _recv_idx = 0; boolean _avail = false; +long _diff = 0; +long _now = 0; +int _pos = 0; +int _idx = 0; +long _count = 0; +boolean _reset = false; + + + + + void interrupt485(){ - _recv_bit = !digitalRead(_RO); // HIGH level = logical zero and vice versa - long now = micros(); - long diff = now - _last_flip; - _last_flip = now; - if (diff > 20*_pulse_len){ // we received a start bit -#ifdef DEBUG - Serial.print(F("State changed to ")); Serial.println(_recv_bit); - Serial.println(F("received start bit")); + _line = !digitalRead(_RO); // HIGH level = logical zero and vice versa + _now = micros(); + _diff = _now - _last_flip; + _last_flip = _now; + +#ifdef RECV + Serial.print(!_line); + Serial.print("-("); + Serial.print(_diff); + Serial.print("µs)→"); + Serial.print(_line); + Serial.print(" "); +#endif + + if (_diff > _break){ + _pos = 0; + _idx = 0; + _reset = true; +#ifdef RECV + Serial.println("==RESET=="); #endif - _recv_pos = 0; - _recv_idx = 0; - _last_flip+=_pulse_len; } else { - long count = diff/_pulse_len; - for (int i=0; i>1; + while (_count < _diff){ + _count += _pulse_len; + if (_idx){ + _recv_msg[_pos] |= !_line << _idx; + } else { + _recv_msg[_pos] = !_line; + } + if (_reset){ + _reset = false; } else { - _recv_msg[_recv_pos] |= (!_recv_bit)<<_recv_idx; + _idx++; } - _recv_idx++; - if (_recv_idx == 8){ - _recv_idx = 0; - if (_recv_msg[_recv_pos] == 0x00){ - _avail = _recv_pos != 0; - return; +#ifdef RECV + Serial.print(_line); +#endif + if (_idx == 8){ + if (_recv_msg[_pos] == 0x00){ + _avail = true; } - _recv_pos++; + _pos++; + _idx=0; } } +#ifdef RECV + Serial.println(); +#endif } } + + + void init485(int RO, int nRE, int DE, int DI){ _RO=RO; _RE=nRE; _DE=DE; _DI=DI; + _avail = false; + + +#if defined SEND || defined RECV + Serial.begin(115200); + Serial.print(F("RO = ")); + Serial.println(_RO); + Serial.print(F("^RE = ")); + Serial.println(_RE); + Serial.print(F("DE = ")); + Serial.println(_DE); + Serial.print(F("DI = ")); + Serial.println(_DI); +#endif + +#ifdef RECV + Serial.println("RECV enabled"); +#endif +#ifdef SEND + Serial.println("SEND enabled"); +#endif pinMode(_RO,INPUT); @@ -73,18 +125,6 @@ void init485(int RO, int nRE, int DE, int DI){ digitalWrite(_DI,LOW); // output line = LOW attachInterrupt(digitalPinToInterrupt(_RO),interrupt485,CHANGE); - -#ifdef DEBUG - Serial.begin(115200); - Serial.print(F("RO = ")); - Serial.println(_RO); - Serial.print(F("^RE = ")); - Serial.println(_RE); - Serial.print(F("DE = ")); - Serial.println(_DE); - Serial.print(F("DI = ")); - Serial.println(_DI); -#endif _last_flip = micros(); } @@ -100,56 +140,57 @@ String get485message(){ boolean writeBit(boolean bit){ digitalWrite(_DI,!bit); delayMicroseconds(5); - if (_recv_bit != bit){ // collision! + if (_line != bit){ // collision! digitalWrite(_DE,LOW); -#ifdef DEBUG +#ifdef SEND Serial.println(F("Collision!")); #endif return false; } - delayMicroseconds(_pulse_len-12); + delayMicroseconds(_pulse_len-5); return true; } +boolean writeByte(byte b){ + return writeBit(b & 0x01) + && writeBit(b & 0x02) + && writeBit(b & 0x04) + && writeBit(b & 0x08) + && writeBit(b & 0x10) + && writeBit(b & 0x20) + && writeBit(b & 0x40) + && writeBit(b & 0x80); +} + boolean send485(char message[]){ -#ifdef DEBUG +#ifdef SEND Serial.print(F("preparing to send ")); Serial.print(message); Serial.print(F(": line state = ")); - Serial.print(_recv_bit); + Serial.print(_line); Serial.print(F(" / last_flip = ")); Serial.print(_last_flip); Serial.print(F(" / micros = ")); Serial.println(micros()); #endif - while (_recv_bit || (micros() - _last_flip) < (20 * _pulse_len)) { + while (_line || (micros() - _last_flip) < (20 * _pulse_len)) { delayMicroseconds(20*_pulse_len); } -#ifdef DEBUG +#ifdef SEND Serial.println(F("enabling driver…")); #endif + digitalWrite(_DI,LOW); digitalWrite(_DE,HIGH); // enable driver - if (!writeBit(HIGH)) return false; - int pos = -1; - boolean good = true; + int pos = -1; // increment pos prior to sending char! + boolean sync = writeBit(0); do { pos++; -#ifdef DEBUG - Serial.print(F("sending character ")); Serial.println(message[pos]); -#endif - good = writeBit(message[pos] & 0x01) - && writeBit(message[pos] & 0x02) - && writeBit(message[pos] & 0x04) - && writeBit(message[pos] & 0x08) - && writeBit(message[pos] & 0x10) - && writeBit(message[pos] & 0x20) - && writeBit(message[pos] & 0x40) - && writeBit(message[pos] & 0x80); - } while (good && message[pos]); - if (good) writeBit(1); + sync = sync && writeByte(message[pos]); + } while (sync && message[pos]); + digitalWrite(_DI,LOW); digitalWrite(_DE,LOW); // disable driver -#ifdef DEBUG +#ifdef SEND Serial.println(F("disabled driver…")); #endif - return good; + return sync; } diff --git a/examples/ReceiveDemo/ReceiveDemo.ino b/examples/ReceiveDemo/ReceiveDemo.ino index 9a77383..7889c5d 100644 --- a/examples/ReceiveDemo/ReceiveDemo.ino +++ b/examples/ReceiveDemo/ReceiveDemo.ino @@ -14,7 +14,6 @@ void setup() { // connect pin 3 to ^RE of Max485 // connect pin 4 to DE of Max485 // connect pin 5 to DI of Max485 - } void loop(){ diff --git a/examples/SendAndReceive/SendAndReceive.ino b/examples/SendAndReceive/SendAndReceive.ino index a7f6382..15b31d1 100644 --- a/examples/SendAndReceive/SendAndReceive.ino +++ b/examples/SendAndReceive/SendAndReceive.ino @@ -1,5 +1,7 @@ #include +int count = 0; + void setup() { Serial.begin(115200); Serial.println("This is SendAndReceive 0.1"); @@ -8,11 +10,11 @@ void setup() { // connect pin 3 to ^RE of Max485 // connect pin 4 to DE of Max485 // connect pin 5 to DI of Max485 - - while (send485("Hello World!")) {} } void loop() { - // put your main code here, to run repeatedly: - if (available485()) Serial.println("Received "+get485message()); + delay(5000); + count++; + String s = "Test "+String(count); + send485(s.c_str()); } diff --git a/examples/TempSend/TempSend.ino b/examples/TempSend/TempSend.ino new file mode 100644 index 0000000..87487ba --- /dev/null +++ b/examples/TempSend/TempSend.ino @@ -0,0 +1,35 @@ + +#include +#include + +int pinDHT11 = 6; +int count = 0; + +SimpleDHT11 dht11(pinDHT11); + +void setup() { + Serial.begin(115200); + Serial.println("This is TempSend 0.1"); + init485(2,3,4,5); // library initialization: + // connect pin 2 to RO of Max485 + // connect pin 3 to ^RE of Max485 + // connect pin 4 to DE of Max485 + // connect pin 5 to DI of Max485 + while (!send485("Hello world!")) {} +} + +void loop() { + // read without samples. + byte temperature = 0; + byte humidity = 0; + int err = SimpleDHTErrSuccess; + if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { + Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err)); + Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000); + return; + } + count++; + String s = String(count)+" - Sample OK: "+ String(temperature)+" *C, "+String(humidity)+"% rel"; + send485(s.c_str()); + delay(5000); +}