avr-gcc-list
[Top][All Lists]
Advanced

[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 1.5.0.7 (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,
_FDEV_SETUP_RW);

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)) );
  return(UDR);
}

//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;
  UBRRL = MYUBRR;
  UCSRB = (1<<RXEN)|(1<<TXEN);
  UCSRC = (1<<URSEL)|(3<<UCSZ0);
  stdout = &mystdout;
}

int main(void)
{
  uart_init();

  while (1)
  {
    PORTC = 0x01;
    delay_ms(1000);

    printf("Hello world");

    PORTC = 0x00;
    delay_ms(500);
  }
  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:

uart_putchar(uart_getchar(&mystdout),&mystdout);

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,

James





reply via email to

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