Using both USART0 and USART1 on ATmega324PA

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

Can someone confirm that USART0 and USART1 cannot be active at the same time on the ATmega324PA?

 

I'm playing around with the ATmega324PA for a diy project. It has two USARTs and I've been experimenting with using both. It seems that if USART0 is activated then USART1 is deactivated so both cannot be active at the same time. This was disappointing since I'd hoped I could listen for incoming data on both simultaneously.

 

I could not find this explicitly pointed out in the datasheet so I just wanted to make sure that I'm not doing something wrong.

 

Here's the initialization function for USART0. The one for USART1 is identical but 0's are replaced with 1's obviously.

 

/*************************************************************************
Function: UART0_Init()
Purpose:  initialize UART0 and set baudrate
Input:    baudrate, stop bits, parity
Returns:  none
**************************************************************************/
void UART0_Init(unsigned int baudrate, unsigned char stopbit, unsigned char parity)
{
	UART0_TxHead = 0;
	UART0_TxTail = 0;
	UART0_RxHead = 0;
	UART0_RxTail = 0;

	UART0_RX_Flag = 0;

	baudrate = UART_BAUD_SELECT(baudrate, F_CPU);

	/*Set baud rate */
	UBRR0H = (unsigned char)(baudrate >> 8);
	UBRR0L = (unsigned char)baudrate;

	/*Enable receiver and transmitter */
	UCSR0B |= ((1 << RXEN0) | (1 << TXEN0));

	/* Set USART Mode */
	UCSR0C &= ~((1 << UMSEL01) | (1 << UMSEL00));		// Asynchronous USART

	/* Set Stop Bit(s) */
	if (stopbit == UART_2_STOP_BITS)
		UCSR0C |= (1 << USBS0);				// 2 Stop bits
	else
		UCSR0C &= ~(1 << USBS0);			// 1 Stop bit

	/* Set Parity */
	if (parity == UART_EVEN_PARITY)
	{
		UCSR0C |= (1 << UPM01);				// Even parity
	}
	else if (parity == UART_ODD_PARITY)
	{
		UCSR0C |= ((1 << UPM01) | (1 << UPM00));	// Odd parity
	}
	else
		UCSR0C &= ~((1 << UPM01) | (1 << UPM00));	// No parity

	/* Set Data Bits */
	UCSR0C |= ((1 << UCSZ01) | (1 << UCSZ00));		// 8 Data bits

	/* Enable interrupts */
	UCSR0B |= ((1 << RXCIE0) | (1 << UDRIE0));
}

 

Last Edited: Sun. Oct 13, 2019 - 07:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hybrid_Child wrote:
Can someone confirm that USART0 and USART1 cannot be active at the same time on the ATmega324PA?

I think you meant the opposite:

Can someone confirm that USART0 and USART1 can be active at the same time on the ATmega324PA?

 

Of course they can - Sit down and think for a minute how you possibly could enforce "only one at a time" if your connected devices could start transmitting at the same time.

 

It's always advisable to first disable the UART - setup the registers - setup interrupts - then enable the UART. Your code does not follow this pattern.

 

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

Hi N.Winterbottom. Thank you for the feedback. I moved /*Enable receiver and transmitter */ to the end of the init function. :)

 

I found the problem. I was not enabling/disabling the UDRE interrupts correctly. All works neatly now.