diff --git a/SoftRS485.cpp b/SoftRS485.cpp index 3483bcf..ca68918 100644 --- a/SoftRS485.cpp +++ b/SoftRS485.cpp @@ -9,136 +9,50 @@ //#define DEBUG #define MSG_LEN 64 -#define IDLE 0 -#define START 1 -#define RECEIVING 2 -#define SENDING 3 -#define COMPLETE 4 - // Pins int _RO, _RE, _DE, _DI; -int _idle_cycles = 0; +long _pulse_len = 100; -/* INCOMING */ -char _recv_message[MSG_LEN]; -byte _recv_byte = 0x00; -byte _recv_state = IDLE; +// Receive vars boolean _recv_bit = false; -byte _recv_idx = 0; -int _recv_pos = 0; - - - -/* OUTGOING */ -char _send_message[MSG_LEN]; -boolean _send_bit = false; -byte _send_state = IDLE; -byte _send_wait = 0; -byte _send_idx = 0; -int _send_pos = 0; - -ISR(TIMER1_COMPA_vect){ - OCR1A += 3228; // Advance The COMPA Register - - if (_send_state == SENDING){ - char c = _send_message[_send_pos]; - _send_bit = c & 1<<_send_idx; - digitalWrite(_DI,!_send_bit); -#ifdef DEBUG - Serial.print("Sending bit "); Serial.print(_send_idx); Serial.print(" of "); Serial.print(c); Serial.print(": "); Serial.print(_send_bit); Serial.print(" / "); -#endif - _send_idx++; - if (_send_idx==8){ // byte completed - _send_pos++; // advance to next byte - _send_idx = 0; // start with first bit - if (c == 0x00 || _send_idx == MSG_LEN){ // we just send a 0-byte - _send_state = COMPLETE; - } - } - } - if (_send_state == START && _idle_cycles > 9){ - _send_bit = HIGH; - digitalWrite(_DE,HIGH); - digitalWrite(_DI,!_send_bit); - _send_state = SENDING; - _send_idx = 0; - _send_pos = 0; +long _last_flip; // 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; + +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("Sent start bit: "); Serial.print(_send_bit); Serial.print(" / "); + Serial.print(F("State changed to ")); Serial.println(_recv_bit); + Serial.println(F("received start bit")); #endif - } - - _recv_bit = !digitalRead(_RO); // LOW = 1 - - if (_recv_bit) { - _idle_cycles = 0; - } else if (_idle_cycles<1000) { - _idle_cycles++; - } -#ifdef DEBUG - Serial.print("idle cycles: "); Serial.print(_idle_cycles); Serial.print(" / "); - Serial.print("received: "); Serial.print(_recv_bit); Serial.print(" / "); -#endif - - if (_send_bit != _recv_bit && _send_state == SENDING){ // collision while sending - digitalWrite(_DE,LOW); // disable driver - _send_state = START; // start over - _idle_cycles = 0; -#ifdef DEBUG - Serial.println("\nCOLLISION"); -#endif - } - - if (_send_state == COMPLETE){ - digitalWrite(_DE,LOW); // disable driver - _send_message[0]=0x00; // clear message - _send_state = IDLE; - _idle_cycles =0; -#ifdef DEBUG - Serial.print("transmission complete / "); -#endif - } - - - - if (_recv_state == START){ - _recv_byte = 0x00; _recv_pos = 0; _recv_idx = 0; - _recv_state = RECEIVING; -#ifdef DEBUG - Serial.print("Start reception / "); -#endif - } - - if (_recv_bit && _recv_state == IDLE){ - _recv_state = START; - } - - if (_recv_state == RECEIVING){ - _recv_byte |= _recv_bit << _recv_idx; // set bit - _recv_idx++; // increase bit index - if (_recv_idx == 8){ // byte full -#ifdef DEBUG - Serial.print("pushing "); Serial.print((char)_recv_byte); Serial.print(" / "); -#endif - - _recv_message[_recv_pos] = _recv_byte; // push character to string - if (_recv_byte == 0x00) { // end of message - _recv_state = IDLE; -#ifdef DEBUG - Serial.print("reception complete: "); Serial.print(_recv_message); -#endif + _last_flip+=_pulse_len; + } else { + long count = diff/_pulse_len; + for (int i=0; i -int count = 0; -String message = "Message "; - void setup() { Serial.begin(115200); - - 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 - - if (send485("Test")) { - // This should send "Test" if the line is free. - Serial.println("Test sent!"); - } - if (!send485("Test2")) { - // This shoult fail, as transmission of "Test" should still be on the way - Serial.println("Test2 failed!"); + init485(2,3,4,5); + speed485(8000); + boolean sent = false; + while (!sent) { + sent = send485("This message is sent and received!"); } } -void loop(){ - if (available485()){ // check if we have received anything - Serial.print("messageReceived: "); - Serial.println(get485Message()); // read the received message - - // send another message everytime we have received one - count++; - char msg[128]; - (message+count).toCharArray(msg,128); - if (!send485(msg)){ - Serial.print("Sending "); - Serial.print(msg); - Serial.println(" failed!"); - } +void loop() { + // put your main code here, to run repeatedly: + if (available485()){ + Serial.println("Received "+get485message()); } }