1

I'm trying to program an ATmega328P Arduino Uno "bare metal" to print to serial over the USB connection to my computer, however I only see replacement characters (�) being printed, both when using screen /dev/ttyACM0 9600 and when using the Arduino IDE serial monitor. I've tried different baud rates to no avail, although the baud rate should be 9600. The TX LED flashes regularly, and the � are appearing at the same rate as expected.

I was following the serial print part of the guide: https://archive.md/wqcw6. Using the make instructions from the same guide resulted in no transmission at all, so I used a different make (shown below) which resulted in transmission but just the � being received. I suspect there's a baud rate issue being caused somewhere.

Here is my code:

#include <avr/io.h>
#include <stdio.h>

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

#include <util/delay.h>

#define BAUD 9600

#include <util/setbaud.h>

int uart_putchar(char, FILE*);
void uart_init();

FILE output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

void uart_init(void) {
    UBRR0H = UBRRH_VALUE;
    UBRR0L = UBRRL_VALUE;
    UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); // 8-bit data
    UCSR0B = (1<<TXEN0); // enable TX
}

int uart_putchar(char c, FILE *stream) {
    if (c == '\n') {
        uart_putchar('\r', stream);
    }
    loop_until_bit_is_set(UCSR0A, UDRE0);
    UDR0 = c;
    return 0;
}

int main(void) {
    uart_init();
    stdout = &output;

    while(1) {
        printf("Hello World!\n");
        _delay_ms(500);
    }
    return 0;
}

Here's my Makefile:

default:
    avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o serial-print.o serial-print.c 
    avr-gcc -o serial-print.bin serial-print.o
    avr-objcopy -O ihex -R .eeprom serial-print.bin serial-print.hex
    sudo avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:serial-print.hex

Any tips would be greatly appreciated. Thank you.

4
  • a tip. if you have problems with the program, why not try w/o optimizations? Commented Oct 19, 2024 at 12:13
  • I just tried dropping the -Os on the avr-gcc if that's what you mean. Got a: # warning "Compiler optimizations disabled; functions from <util/delay.h> won't work as designed" and nothing transmitted at all when it was flashed to the arduino, no LED blinking on the TX Commented Oct 19, 2024 at 12:24
  • This question is similar to: Linking multiple object files into a binary AVR GCC. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 19, 2024 at 13:59
  • Parts of that question help in an indirect way but it's definitely different both in terms of what it's asking and in terms of what the solution given is. I don't see any need to edit my question. Commented Oct 19, 2024 at 15:45

1 Answer 1

0

I found the answer in this post here:

ATmega328P USART Transmit character printed repeatedly

Changing my Makefile to the following solved my problem:

avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p serial-print.c -o serial-print.bin
avr-objcopy -O ihex -R .eeprom serial-print.bin serial-print.hex
sudo avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:serial-print.hex
Sign up to request clarification or add additional context in comments.

5 Comments

maybe you just have to repeat -DF_CPU=16000000UL -mmcu=atmega328p flags to the linker? because I do not see any difference with joining everything in one command.
your answer is not an answer, but a mindless repeat of some nonsense.
adding -g -mmcu=atmega328p to the second line in the original also works
"also works..." - means it solves your problem?
-g switch means adding gnu compatible debug symbols to the output. i'm writing it so that you not just copy someones makefiles verbatim, but learn something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.