USART does not release PORTD.1

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

Has anyone ever had trouble getting the USART to release the TX PORT pin?

We are using a Mega48V and take control of PORTD.1 as a port pin first. We then initialise the USART and send some bytes. Works fine.

After that we set all USART registers to 0x00 (which is supposed to release control of PORTD.1), but it doesn't! Writing anything to PORTD.1 (even after re-initialising the port) shows no output at all. If we SLEEP the micro at that point and wake it up again with a pin-change interrupt all works fine again (until the USART gets control again and we lose PORTD.1 once again).

We use PORTD.0 as a normal port pin. We don't use RX (so UCSR0B=0x08 for TX only), but there seems to be an interaction between the 2 pins. When PORTD.0 is written to, the USART doesn't release. If PORTD.0 is left alone, the USART releases PORTD.1

The whole process is repeatable.

The sequence:

PORTD.0=0;
Toggle PORTD.1 a few times
Initialise USART
Send some bytes
Write all USART registers to 0x00
Toggle PORTD.1 a few times <== doesn't work

Leave the first line out and all works fine.

Any thoughts?

Cheers - Frank

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

I think, you must wait until the last stop bit was finished, otherwise disabling the UART not take place.

Instead a delay loop it may work, if you set and read back the UCSR0 until disabling take place.

Peter

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

After you disable the UART, are you updating ddrd to make portd.1 an output?

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

If you are using USART interrupts then disable them first (RXCIE0 and TXCIE0 in the UCSR0B register). Doing this first will allow any interrupt event that might occur just before the USART is disabled to be answered first while the USART is still running in a normal configuration.

Page 178 of the data sheet said that disabling the USART Tx will not take effect until all ongoing and pending transmissions are complete. Page 182 said the USART Rx disable takes immediate effect and flushes the USART Rx buffer.

After waiting for the last USART Tx to clear (the TXC0 flag in the UCSR0A register) all you should have to do is clear the TXEN0 bit of the UCSR0B register to release USART Tx pin 31 (TXD0). Then you need to setup the PORTD and DDRD registers PD1 bit for whatever you want pin 31 (PD1) to do.

Clear the RXEN0 bit of the UCSR0B register to release USART Rx pin 30 (RXD0). Next you need to setup the PORTD and DDRD registers PD0 bit for whatever you want pin 30 (PD0) to do.

Page 84 of the data sheet explains the alternate pin functions for pin 30 and 31 in more detail.

If you zero the UBBR baud rate register all you will do is set it for maximum baud rate.