Browse Source

working version?

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
main
Stephan Richter 1 year ago
parent
commit
5fe0c22abb
  1. 175
      SoftRS485.cpp
  2. 1
      examples/ReceiveDemo/ReceiveDemo.ino
  3. 10
      examples/SendAndReceive/SendAndReceive.ino
  4. 35
      examples/TempSend/TempSend.ino

175
SoftRS485.cpp

@ -6,60 +6,112 @@ @@ -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
_recv_pos = 0;
_recv_idx = 0;
_last_flip+=_pulse_len;
if (_diff > _break){
_pos = 0;
_idx = 0;
_reset = true;
#ifdef RECV
Serial.println("==RESET==");
#endif
} else {
_count = _pulse_len>>1;
while (_count < _diff){
_count += _pulse_len;
if (_idx){
_recv_msg[_pos] |= !_line << _idx;
} else {
long count = diff/_pulse_len;
for (int i=0; i<count; i++){
if (_recv_idx == 0) {
_recv_msg[_recv_pos] = (!_recv_bit);
_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){ @@ -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(){ @@ -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;
}

1
examples/ReceiveDemo/ReceiveDemo.ino

@ -14,7 +14,6 @@ void setup() { @@ -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(){

10
examples/SendAndReceive/SendAndReceive.ino

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
#include <SoftRS485.h>
int count = 0;
void setup() {
Serial.begin(115200);
Serial.println("This is SendAndReceive 0.1");
@ -8,11 +10,11 @@ void setup() { @@ -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());
}

35
examples/TempSend/TempSend.ino

@ -0,0 +1,35 @@ @@ -0,0 +1,35 @@
#include <SoftRS485.h>
#include <SimpleDHT.h>
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);
}
Loading…
Cancel
Save