Go Down

Topic: RS485 setting DE/RE bit on TX completetion (Read 357 times) previous topic - next topic

HenryHerman

I have being working on an RS485 library and wrote a class that inherits from hardwareserial.  I wrote this method to control the DE/RE pin on the RS485 transceiver during the transmission of a character array.  From the Atmega168 datasheet I should be able to look at the TXC bit on the ucsra register to tell when the TX buffer is empty.  For some reason it always chops the last byte of the transmission.  I am thinking that maybe the TXC flag is no behaving like I am expecting and going high before the last bit of the TX buffer has been written out.

On the AVR freak forum I found someone else having the same issue.
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=70150&start=0

There was no obvious solution other than adding a delay (which I hate doing because it is far from optimal).

Code: [Select]

void HardwareSerialRS485::write(const char *str)
{


 digitalWrite(_pin,HIGH); // Set DE/RE bit for TX
 while(!(*str=='\0')) {
   *_ucsra |=(_BV(_txc));  // Do I need to reset this bit?
   write(*str++);
 }
 while(!(*_ucsra & _BV(_txc)));  //This exits before I  want it to?
 *_ucsra |= (_BV(_txc));
 delayMicroseconds(1800); //Delay to make it work
 digitalWrite(13,LOW);
 digitalWrite(_pin,LOW);  // Set DE/RE bit for RX
 
}


I would appreciate any help or ideas why this is not working.

Go Up