problem of UART initialization

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

Hello,

I wrote a UART0 initialization for AT90CAN128, but somehow it doesn't work. Please see my code below:

void USART0_Init(UINT16 baudrate, UINT8 data_bit_number, UINT8 parity)
{
	// set baudrate
	UBRR0H = (UINT8)(baudrate >> 8);
	UBRR0L = (UINT8)baudrate;
	// double speed operation
	UCSR0A |= (1 << U2X0);
	// set frame format: 8 data, no parity, 1 stop bit
	if (parity == 1)
	{
		UCSR0C = (0 << UMSEL0) | (3 << UPM0) | (0 << USBS0) | ((data_bit_number - 5) << UCSZ0);
	}
	else
		UCSR0C = (0 << UMSEL0) | (parity << UPM0) | (0 << USBS0) | ((data_bit_number - 5) << UCSZ0);		
	// enable receiver and transmitter
	UCSR0B = (1 << RXEN0) | (1 << TXEN0);
}

I tried to call this function with USART0_Init(103, 8, 0) and I expected following UART0 parameters: 9600 bps, 8 data bits, 1 stop bit and no parity.

I'm not sure whether I used "doulbe speed" the right way because I didn't find such examples in the data sheet.

Thank you

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

Well it's tricky to help when you haven't said what is actually wrong but the likelihood is that your AVR isn't running at the speed you think it is.

Cliff

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

With a UBRR of 103 and U2X0 set, you would get 9600 baud with a cpu frequency of ~8Mhz. Is that what you intend to run at?

Quote:
I wrote a UART0 initialization for AT90CAN128, but somehow it doesn't work.

Exactly what is not working about it? Are you getting nothing at all or are you getting garbage?

Regards,
Steve A.

The Board helps those that help themselves.