# AVR: Serial communication gives strange characters

I am having some serious problems trying to get the serial monitor to output the correct characters. I have looked over a lot of the posts with similar problems, but nothing seems to solve it.

I am an absolute rookie when it comes to this stuff, and we are working with embedded systems for a university course and the teacher isn't at much help.

I am simply trying to print the character "a" to the serial monitor, but all I get is these wonky characters that can be seen in the picture.

I attached the code and the picture, in hopes that someone more knowledgable in the area can pinpoint me in the right direction!

Basic Info:

Programming through a Arduino nano

programmer: arduino

baud rate: 56700

chip: atmega328p

#define  F_CPU 1000000UL

#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include "usart.h"

int main(void) {

uart_init(); // open the communication to the microcontroller
io_redirect(); // redirect input and output to the communication

while(1) {

_delay_ms(1000);

printf("a");

}
return 0;
}


The usart.c and usart.h files are provided by the university. I can compile and flash to the chip no problems, just getting strange characters as output.

Every value of the baud rate under 57600 gives me this this response from avrdude.

There is one exception when I try to use 9600, I get the response:

avrdude: stk500_getsync() attempts 1 of 10: not in sync: resp=0xff
avrdude: stk500_getsync() attempts 2 of 10: not in sync: resp=0xff
avrdude: stk500_getsync() attempts 3 of 10: not in sync: resp=0xff
...


As I am totally new to this, I have no idea what it means, and google didn't help a lot. The only baud rate I can even flash to the chip, is at 57600.

Here you can see the uart_init function:

void uart_init(void) {

UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;

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

UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0);   /* Enable RX and TX */

}

• It looks like something is happening. Double check the baud rate at each end, and the claimed cpu clock rate, which may be used in a rate divider calculation. Both ends have to agree exactly on speed. – Neil_UK Nov 25 '16 at 13:53
• Almost the same problem so if not already read, please check this question: electronics.stackexchange.com/questions/269658/… – Bence Kaulics Nov 25 '16 at 13:57
• Try with lower baud rates too, 1200 is a good start. – Bence Kaulics Nov 25 '16 at 14:02
• Thanks for the responds guys! I have tried with lower baud rates, but then avrdude says the programmer wont respond. It only flashes to the chip if I use 56700 and up. I also did read that post, men to be frank, I had no idea what was going on. – Michael Nissen Nov 26 '16 at 16:53
• That is the baud of the avrdude, yes. I edited my original post to include the code from the init function, because the formatting is weird in the comments :) – Michael Nissen Nov 27 '16 at 13:52

#define  F_CPU 1000000UL