controlling transceiver with tiny2313

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

i am using an attiny2313 to control a half duplex uart - rs485 transceiver.
Rx and int0 on the attiny is connected to the uart rx of the transceiver to detect when data is being sent to the transceiver from a third uart unit.

The transceiver converts from rs485 to UART at default, and I'm using the tiny to switch it to convert from UART to rs485 when it detects a falling edge on rx.(data being sent to transceiver)

The problem is knowing when the data has been transmitted so that the transceiver can be set back to the default flow direction.

My approach is to check if the rx is logically high for more than 10ms, and then switch it back.

Does anyone know any better/more robust way of doing this? or do you think this will work fine?

unsigned high_timer = 0;


//Interrupt,(falling edge), on rx.
ISR(INT0_vect) {
     		//Set transceiver to channel data from UART to RS485
		TRANS_DE_high;
		TRANS_RE_high;

while((PORTD==(0<<PIND1)) && (high_timer <= 10)){ //Wait till 10ms after the stopbit has been sent.
			_delay_ms(1);
			high_timer++;
					}
		if(PORTD==(0<<PIND1)){ 	// If RX is idle.
				//Restore the transceiver to channel data from RS485 to UART
		TRANS_DE_low;
		TRANS_RE_low;
	//		}
high_timer =  0; //Resetting the timer
     }
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The problem is knowing when the data has been transmitted so that the transceiver can be set back to the default flow direction.

Isn't this what the TXC interrupt is for ?

(BTW it is NEVER a great idea to do a _delay() inside an ISR)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But TXC interrupt only tells if the transmitter is ready? Is there something similar for RXC?

edit:typo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I figured it out, here is the working code:

//Interrupt,(falling edge), on rx.
ISR(INT0_vect) {
     		//Set transceiver to channel data from UART to RS485
		TRANS_DE_high;
		TRANS_RE_high;
	
while(!(UCSRA & (1 << RXC))); //Don't do anything till the entire string has been sent.
				//Restore the transceiver to channel data from RS485 to UART
		TRANS_DE_low;
		TRANS_RE_low;
     }
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

eds_oyo,

This is taken directly from the ATtiny2313 datasheet p. 124:

Quote:

The TXC flag is useful in half-duplex communication interfaces (like the RS-485 standard), where a transmitting application must enter receive mode and free the communication bus immediately after completing the transmission.

I hope this helps.

Mark

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

here is the working code:

???
Quote:

while(!(UCSRA & (1 << RXC))); //Don't do anything till the entire string has been sent.

Sent?!? (why are you looking at RXC?)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Sent?!? (why are you looking at RXC?)

Oh thanks - I thought I was going bonkers over here - there was a reason I mentioned TXC not RXC above!

(but I wasn't suggesting to poll the status flag but to start the transmission then end the INT0 interrupt, wait for a TXC interrupt and then switch direction)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm sorry, I've written some of the comments somewhat confusing. I'll try to explain it.

First. ONLY the RX on the microcontroller(attiny) is connected to the UART unit, not TX! TX on the microcontroller is not connected to anything cause it's not needed to transmit anything.

here is the physical layout:

So, the attiny can ONLY receive data from the UART unit,
When it detects incoming data it switches the transceiver so that the data can be sent from the UART unit to the RS485 unit.
After this it checks if the RXC is idle, which happens when the _UART unit_ has stopped _sending_ data. (hence the confusing commentry)

I hope this made it clearer.

Quote:
(but I wasn't suggesting to poll the status flag but to start the transmission then end the INT0 interrupt, wait for a TXC interrupt and then switch direction)

My app works fine with just polling the flag, but i can see that this is a better solution.

Again, sorry for the confusion folks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are you doing what you are doing? Where is the UART unit and why can't IT control the data direction?

Also there are transreceiver units that have automatic flow detection. Other systems have a simple retriggerable timer that looks at the data and changes direction of the transreceiver, once data stops flowing for a certain time the timer just expires and changes direction.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

js wrote:
Why are you doing what you are doing? Where is the UART unit and why can't IT control the data direction?

The UART unit is a ConnectOne mini Socket iWifi module, and i can not use it to control the data direction of the transceiver.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
i can not use it to control the data direction of the transceiver.
The way you show the circuit you are likely to miss bits going out even with a fast processor as you will have several instructions to excecute befor it all happens.

Replace the Tiny2313 with a retriggerable monostable like a 74C221 or a humble 555.
The easiest is to use a MAX1341 with auto direction.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly