I have a bidirectional Rs485 bus with up to 4 arduino's. I have not built in any fail-safe collision prevention because the amount of messages is very low (1 message per 10s or so), it is unlikely that a collision ever occurs, messages aren't that critical and the master arduino in this setup is the sender for 98% of the time. There are 2 whole inputs on slaves which sometimes send a message.
Anyways when thinking about it, I got me a question. I do have some interlocking built in.
In order to drive the RE/DE lines I continously monitor the serial buffer with these lines of code. As soon as availableForWrite reaches 63 I am 100% all bytes in the buffer are transmitted and I can release the bus.
if( RS485->availableForWrite() == 63
&& digitalRead( rs485dir ) )
{
digitalWrite( rs485dir, LOW) ;
}
I also have a simple interlocking mechanism with which a node will not try to transmitt a message when he is receiving a message at that time (the baudrate is set at 9600 and a transmission may easily be 20 bytes)
This detection uses Serial.available()
. As soon as it returns 1, a node knows there is a message incomming and the node shall not transmitt himself.
Serial.avaible()
returns 1 after the first bytes is received. This means that during reception of the first byte of a message there is a small window (roughly 1 ms @9600bps) in which a node may attempt to transmitt and cause a collision.
The one thing I want to know* is how to get a signal in software when a start bit is received. Knowing this, may minimize this collision window.
I have been digging through chapter 19 of the atmega328 manual but a flag in the USART registers for start bit detection seems to be lacking.
Is this correct? or have I overread something?
Is it possible to also enable the pin change ISR (PCINT16 for Rx) while using the USART. Or does the one exclude the other?
If not. Would it be feasible to hook up an an extra IO pin and use that pin's pin change interrupt to detect start bits.
Regards,
Bas
.*
I am fully aware of every other method to prevent or solve collisions. Time windows, checksums, tokens passing, even modbus etc. I am not seeking information about this. I only want start bit detection.