Pages: [1]   Go Down
Author Topic: RS485 setting DE/RE bit on TX completetion  (Read 553 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 209
Posts: 13027
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Could the transceiver be the problem?  I can vaguely recall that a shutdown delay was needed for certain transceivers; especially ones designed to work over long distances.
Logged

Pages: [1]   Go Up
Jump to: