I've read the UART datasheet a million times but this one has me stumped. It seems that at the end of my serial communication the data gets distorted for some reason.
I'm using UART 0 of the ATmega128. Configured for 9600 baud / 8 data bits/ no parity.
When I set UMSEL0 to high in UCSR0C I get no serial output on the comm port.
When UMSEL0 is low I get the below result using the following code:
for( index= 0; index < 10 ; index++)
{
_delay_loop_2(999999999);
_delay_loop_2(999999999);
_delay_loop_2(999999999);
USART_Transmit0(0xAA);
}
for( index= 0; index < 10 ; index++)
{
_delay_loop_2(999999999);
_delay_loop_2(999999999);
_delay_loop_2(999999999);
USART_Transmit0(0xBB);
}
USART_Transmit0(0xCC);
....
USART_Transmit0(0xDD);
....
USART_Transmit0(0xEE);
...
Output:
AA AA AA AA AA AA AA AA AA AA BB BB BB BB BB BB BB BB BB BB CC CC CC CC CC CC CC EC EC EC ED ED ED ED ED ED EF EF EE EE 77 CF CF CF CF CF CF CF CF EF FF
What!? The output starts off good but then turns bad at the end! Parts of the CC's, the D'ss and EE's are screwed up!
// init the UART void InitUART0(unsigned char baud) { /* Set baud rate */ UBRR0H = (unsigned char)(baud>>8); UBRR0L = (unsigned char)baud; /* Enable receiver and transmitter */ UCSR0B = (1<<RXEN)|(1<<TXEN); /* Set frame format: 8data, 1 stop bit */ // This will allow for some of my data to be valid. UCSR0C = (3<<UCSZ0); // If I do the below I see no data at all. // UCSR0C = (3<<UCSZ0)| (1<< UMSEL0); }
I'm using the internal RC oscillator and initialzing my uart with:
InitUART0(51);
UCSR0A = 0x20
UCSR0B = 0x98
UCSR0C = 0x86
Anyone have any ideas on this one?
-Henk