[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] FDEV_SETUP_STREAM problem on an ATMega8

From: James Pascoe
Subject: [avr-gcc-list] FDEV_SETUP_STREAM problem on an ATMega8
Date: Tue, 26 Jun 2007 20:41:44 +0100
User-agent: Thunderbird (X11/20060909)

Dear All,

I am new to this list, so I am sorry if this post is OT.

I am trying to run the following program on my ATMega8:

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

#define FOSC 16000000
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1

static int uart_putchar(char c, FILE *stream);
int uart_getchar(FILE *);
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, uart_getchar,

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

  loop_until_bit_is_set(UCSRA, UDRE);
  UDR = c;

  return 0;

int uart_getchar(FILE *stream)
  while( !(UCSRA & (1<<RXC)) );

//General short delays
void delay_ms(uint16_t x)
  uint8_t y, z;
  for ( ; x > 0 ; x--){
    for ( y = 0 ; y < 90 ; y++){
      for ( z = 0 ; z < 6 ; z++){
        asm volatile ("nop");

void uart_init (void)
  //1 = output, 0 = input
  DDRB = 0xEF; //PB4 = MISO
  DDRC = 0xFF; //All outputs
  DDRD = 0xFE; //PORTD (RX on PD0)

  //USART Baud rate: 9600
  UBRRH = (MYUBRR) >> 8;
  UCSRB = (1<<RXEN)|(1<<TXEN);
  UCSRC = (1<<URSEL)|(3<<UCSZ0);
  stdout = &mystdout;

int main(void)

  while (1)
    PORTC = 0x01;

    printf("Hello world");

    PORTC = 0x00;
  return 0;

I am using avr-gcc version 4.2.0, binutils 2.17 and avr-libc-1.4.6
(built from source).
I am using PonyProg2000 (v 2.07a beta) to program my ATMega8 and my
development host is a Thinkpad T41 running Slackware 11.0.

I am building my code at the command line with the following:

avr-gcc -mmcu=atmega8 jsp.c -o jsp.hex

The code builds without warning and everything seems fine. I have an LED
hooked up to the first line of portc. When I flash jsp.hex to the device, I see
my LED illuminate but not flash (as I would expect). I can substitute the 
printf line with
the following code:


and I can type characters into minicom and see them echoed back.

Does anybody have any idea why I can not print to stdout ?

Any help would be very much appreciated.

Thanks in advance,


reply via email to

[Prev in Thread] Current Thread [Next in Thread]