# AVR ATMEGA168 UART behaving strangely

I am new to AVR and microcontrollers and I am trying to get the UART working correctly on my Atmega68 chip. When I transmit data from my chip my PC terminal program receives them as weird symbols. I believe this is a problem with the baud rate but I am unsure how to fix it.

My F_CPU should be 1MHZ and the baud rate should be 9600. When I checked the fuse settings on the chip they were 0xdf and 0x62 which I believe is correct for a 1MHZ clock..

I am using a USBasp to program which has a jumper in position JP3 to reduce its clock speed - could this affect the uart?

I am using a USB to serial converter which I have tested using loopback.

My code is as follows, any help would be much appreciated:

void initUSART(void) {
UBRR0H = UBRRH_VALUE;                 /* defined in setbaud.h */
UBRR0L = UBRRL_VALUE;

#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif

UCSR0B = (1 << TXEN0) | (1 << RXEN0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);   /* 8 data bits, 1 stop bit */
}

int main(void) {
// -------- Inits ---------
initUSART();

while (1)
{
transmitByte('a');
}
return 0;
}

void transmitByte(uint8_t data) {
/* Wait for empty transmit buffer */
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = data;                                            /* send data */
}

• What does transmit function do? Have you checked the UBRR values ? – Abel Tom Oct 1 at 19:39
• Indeed, we can't work with that. What are the UBRR values, is U2X used or not, and how does transmitbyte works? Since you use the internal oscillator, what supply voltage you have? How have you connected it to PC, is it TTL level USB interface or RS232 level? – Justme Oct 1 at 19:51
• And have you tried programming a "hello world" (blink one LED) program first, to make sure the device is actually running properly? (And not at some unusual speed, etc.) – rdtsc Oct 2 at 0:41
• I have managed to get LEDs to work with this chip so i know its running properly. I previously had a USB - Serial cable that was transmitting RS232 instead of TTL levels. I have bought a new serial cable that is meant for USB -> TTL and i am still getting strange symbols through the port. I have also connected the TX and RX pins of my chip together to light an LED if the correct string is received which it does. UBBR values should be 6 - i am unsure how to check this value without being able to transmit it through the UART. – Noj911 Oct 3 at 17:05
• As explained yesterday, for people to help you you need to provide the actual values determined for UBRR, U2X, etc. If this were only a baud rate issue, guessing at different rates on the PC side would probably work as you are likely off by a factor of two or 8 in one direction or the other. Drop the desired baud to say 2400 and you could couple it with a capacitor into a computer sound card and look at the audio in audacity like a bad oscilloscope. – Chris Stratton Oct 3 at 17:09