I have a half duplex RS485 Master - Slave network using MAX485 ICs. I have found that I need to keep the Driver Enable pin HIGH after Serial.write(), otherwise the ATmega328 hangs after 5 minutes or so. I have experienced this behavior when using a delay of 1, but If I change it to 20, it seems to be OK (17+ hours and counting).
What is the correct value I should be using? and how is this worked out?
Also why would the ATmega328 crash like this? I would just see the TX_LED stay on, and the slave stop responding.
#define TX_LED 13
#define DRIVER_ENABLE 2
byte address1 = 0x31; //1
byte address2 = 0x32; //2
void setup()
{
pinMode(TX_LED, OUTPUT);
pinMode(DRIVER_ENABLE, OUTPUT);
Serial.begin(9600);
}
void loop()
{
sendPacket(address1);
delay(1000);
sendPacket(address2);
delay(1000);
}
void sendPacket(byte address)
{
digitalWrite(TX_LED, HIGH);
digitalWrite(DRIVER_ENABLE, HIGH);
Serial.write(address);
delay(20); //Delay to allow transmission.
digitalWrite(TX_LED, LOW);
digitalWrite(DRIVER_ENABLE, LOW);
}