Electrical – AVR: Serial communication gives strange characters

avrcembeddedmicrocontrollerserial

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.

The serial monitor output

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

enter image description here

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 */

}

Best Answer

Are you sure about this line here;

#define  F_CPU 1000000UL 

As far as i know arduino nano has 16Mhz onboard external oscillator and if you not changed default fuse settings it will work on 16Mhz.

Also try to make it working on lower baud values like 9600. And check your uart.h, make sure it has rigth UBBR value.

enter image description here