Problem with printf on mega168 [solved]

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm sure I must have missed something obvious here but I have been trying to get printf working for several hours now and I just can't figure out what's wrong.
Development platform is Ubuntu 10.04 using avr-gcc and avrdude, target mcu is mega168. I'm using PuTTy as serial monitor.

#include 
#include 
#include 

#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

static int uart_putchar(char c, FILE *stream);

static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

static int uart_putchar(char c, FILE *stream)
{
	if (c == '\n')
		uart_putchar('\r', stream);

	while ((UCSR0A & (1 << UDRE0)) == 0);
	UDR0 = c;
	return 0;
}

int main (void)
{
	UCSR0B |= (1 << RXEN0) | (1 << TXEN0);
	UCSR0C |= (1 << UCSZ00) | (1 << UCSZ01);
	
	UBRR0L = BAUD_PRESCALE;
	UBRR0H = (BAUD_PRESCALE >> 8);

	stdout = &mystdout;
	printf("Hello, world!\n");
   
	while (1) {
		while ((UCSR0A & (1 << UDRE0)) == 0);
		UDR0 = 'U';
		_delay_ms(100);
	} 
}

When I run the code as is nothing happens, ie. no serial output to the monitor unless I out-comment the printf line then a get the stream of UUUU's I expected. Oddly, removing the newline character from the printf statement has the same effect although Hello World! is not printed.
I have studied the stdiodemo example and read library reference and my only guess right now is it could be something to do with my Makefile (always blame the thing you understand least). This is what I have been using, I adapted it from one found on the net which has served my needs until now.

# AVR-GCC Makefile

AVRDUDE=avrdude -p m168 -c usbasp -v
PROJECT=sd_test
SOURCES=main.c
MCU=atmega168
HZ=8000000

CC=avr-gcc
OBJCOPY=avr-objcopy
CFLAGS=-mmcu=$(MCU) -DF_CPU=$(HZ)UL -Os -fpack-struct -fshort-enums -funsigned-bitfields -funsigned-char -Wall
LDFLAGS=-Wl,-Map,$(PROJECT).map -Wl,-u,vfprintf -lprintf_flt

$(PROJECT).hex: $(PROJECT).out
	$(OBJCOPY) -j .text -O ihex $(PROJECT).out $(PROJECT).hex

$(PROJECT).out: $(SOURCES)
	$(CC) $(CFLAGS) $(LDFLAGS) -I./ -o $(PROJECT).out $(SOURCES)

install: $(PROJECT).hex
	$(AVRDUDE) -v -U flash:w:$(PROJECT).hex:i

clean:
	rm -f $(PROJECT).out
	rm -f $(PROJECT).hex

Any idea what could be wrong?

Last Edited: Mon. Nov 21, 2011 - 08:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You aren't doing "-j .data" when you create the .hex so you "Hello, world!" is not part of the code image.

This kind of highlights why these roll-your-own makefiles are often a waste of time. Why not give in and use Mfile as an AWFUL lot of thought has gone into its template to cover all bases such as this one.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Adding -j .data fixed it :D

$(PROJECT).hex: $(PROJECT).out
	$(OBJCOPY) -j .text -j .data -O ihex $(PROJECT).out $(PROJECT).hex

However I will take your advice and look at using Mfile in future.
Thank you for the prompt and helpful reply.