# Atmega8 USART transmitting wrong data

I'm trying to get USART working on my atmega8-16PU microcontroller. I've studied the documentation and took the examples of code from there. I've managed to transmit the data from microcontroller to PL-2303HX chip which is connected to my computer via USB

The problem is that i receive different data from what i send from mcu. Here is the code:

#define F_CPU 8000000UL // Clock Speed
#define BAUD 9600
#define MYUBRR (((F_CPU / (BAUD * 16UL))) - 1)

void USART_Init( unsigned int ubrr)
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSRB = (1 << RXEN) | (1 << TXEN);
/* Set frame format: 8 data bits, 1 stop bit */
UCSRC = (1 << URSEL) | (1 << USBS) | (1 << UCSZ0);
}

void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) );
/* Put data into buffer, sends the data */
UDR = data;
}

int main()
{
USART_Init(MYUBRR);
while(1)                      /* run continuously */
{
USART_Transmit('a');
_delay_ms(100);
}
}


I get 0x80 0x00 at the receiver's side and that is not the 'a' symbol. If it's important a 12 MHz quartz is connected to the PL-2303HX chip. What am I doing wrong?

Most common error: You forgot the (default) Clock Div8 Fuse and your MCU runs on 1 MHz instead of 8 MHz. Try setting your COM Baudrate to 1200 Baud on the PC.

Another nitpick:

#define FOSC 8000000UL // Clock Speed
...
#define MYUBRR (((F_CPU / (BAUD * 16UL))) - 1)


Notice that you defined FOSC but then used F_CPU in your macro.

• Thanks for your reply. As for the define directive it is my fault, I haven't copied the code properly. See the comment above please. I've just tried changing the baud rate of the com port to 1200 without any success. Now i get the 0xE1 from the port code and that is still not the 'a' character – roman Dec 7 '12 at 22:53
• I've used 2 stop bits with baud rate 1200 and everything seems to work properly! Thanks a lot. But I'm still interested why the data is transmitted incorrectly when using 1 stop bit? – roman Dec 7 '12 at 23:10
• where is DIV8 located? – Standard Sandun Jun 15 '13 at 17:47

You are calculating your baud rate incorrectly. you defined #define FOSC 8000000ULas your clock Clock Speed but your baud rate calculation calls out for for a F_CPU symbol. That symbol is being defined as 1MHz in the #include library. So your calculated baud rate is incorrect as well as the time in your delay. so change that FOSC to F_CPU and define it before the #include of define it as using the -D option for your compiler. Hope this helps

• Sorry, i've copied the code incorrectly, FOSC was defined earlier so it didn't get copied. – roman Dec 7 '12 at 22:46