0
\$\begingroup\$

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

}
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Neil_UK Nov 25 '16 at 13:53
  • 1
    \$\begingroup\$ Almost the same problem so if not already read, please check this question: electronics.stackexchange.com/questions/269658/… \$\endgroup\$ – Bence Kaulics Nov 25 '16 at 13:57
  • \$\begingroup\$ Try with lower baud rates too, 1200 is a good start. \$\endgroup\$ – Bence Kaulics Nov 25 '16 at 14:02
  • \$\begingroup\$ 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. \$\endgroup\$ – Michael Nissen Nov 26 '16 at 16:53
  • 1
    \$\begingroup\$ 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 :) \$\endgroup\$ – Michael Nissen Nov 27 '16 at 13:52
1
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Read the question carefully. The OP uses an Arduino Nano to program an Atmega328p. The code runs on the Atmega328p. \$\endgroup\$ – Bence Kaulics Nov 25 '16 at 18:00
  • \$\begingroup\$ I agree with this, Arduino nano or not, they dont use that clock speed \$\endgroup\$ – KyranF Nov 25 '16 at 18:18
  • \$\begingroup\$ Thanks a lot for the response guys! Regarding the defined clock speed, I am not sure about it at all, but it was the template code provided by our teacher, and the other students (using windows) dont seem to have this problem. I tried it on my friends Mac the other day, and he has the same problem, but the same code works when done on windows. I tried removing the line, but still got the same problem. I also tried lowering the baud rate, but then avrdude says that the programmer wount respond. \$\endgroup\$ – Michael Nissen Nov 26 '16 at 16:50
  • \$\begingroup\$ If you are using internal RC oscillator take a look at these link \$\endgroup\$ – Mustafa Ali Mutlu Nov 27 '16 at 10:21
  • \$\begingroup\$ avrfreaks.net/forum/internal-rc-oscillator-uart \$\endgroup\$ – Mustafa Ali Mutlu Nov 27 '16 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.