I've been trying for a few days now to get my AVR to communicate with my computer via RS-232.
For the most part this works fine, the computer is getting a constant stream of serial data as I intended however this data is often received as garbage, by reconnecting the cable or reopening the serial device I can eventually get it to receive the data correctly.
I have tried multiple baud rates, voltage levels, parity, data and stop bits to little effect, I should also point out my AVR is being clocked by a 20MHz crystal so there is approximately a 0.2% baud rate error.
This is the code I'm currently using:
#define F_CPU 20000000UL
#define BAUD 600
#include <avr/io.h>
#include <util/setbaud.h>
#include <stdint.h>
int main()
{
// Set baud rate
UBRR0L = UBRRL_VALUE;
UBRR0H = UBRRH_VALUE;
// 8 Data bits, no parity, 1 stop bit
UCSR0C = 0x00;
UCSR0C |= (1 << UCSZ01);
UCSR0C |= (1 << UCSZ00);
// Enable TX and RX
UCSR0B |= (1 << TXEN0);
UCSR0B |= (1 << RXEN0);
uint8_t msg[] = { 0x41, 0x56, 0x52, 0x0D, 0x0A };
uint8_t i = 0;
while (1)
{
if (i == 5) { i = 0; }
if (UCSR0A & (1 << UDRE0))
{
UDR0 = msg[i];
i++;
}
}
}
And the circuit:

Any suggestions on how to fix this?