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

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

[avr-gcc-list] why scanf doesn't work?


From: WangGang
Subject: [avr-gcc-list] why scanf doesn't work?
Date: Mon, 04 Jun 2007 05:01:44 -0000

Hi  all

 

I want to debug my program through Rs232 interface of the PC.

But why the  “scanf” and “scanf_P” function doesn’t work while printf works?

I have googled and find only the example of using printf,but no scanf.

Does avr-gcc not support scanf very well?Or there is something wrong in the way I use scanf.

 

That is my code:

I find why run to “sanf”,the mcu seems dead.

Thanks.

 

/*****************************************************

Project : rs232_debug

Version :

Date    : 2007-6-3

Author  :

Company :

Comments:

 

 

Chip type           : atmega16

Program type        : Application

Clock frequency     : interal 8 MHz

*****************************************************/

 

# include <stdio.h>

# include <avr/io.h>

# include <avr/pgmspace.h>

 

void USART_Init(void);

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

static int usart_getchar(FILE * stream);

 

static FILE mystdio = FDEV_SETUP_STREAM(usart_putchar,usart_getchar,_FDEV_SETUP_RW);

char buffer[81];

 

int main(void)

{

    int tmp = 0;

 

    USART_Init();

    stdout = &mystdio;

    stdin = &mystdio;

 

    while(1)

    { 

        //test for hardware

        //loop_until_bit_is_set(UCSRA,RXC);

        //tmp = UDR;

        //printf("Reveive data is : %d\n",tmp);

       

        printf("Please input one string:\n");

        scanf("%s",buffer);

        printf("Your input string is:%s\n",buffer);

       

        printf_P(PSTR("Please input one number:\n"));

        scanf_P(PSTR("%d"),&tmp);

        printf_P(PSTR("Yout input number is:%d\n"),tmp);

          

    }

 

    //exit

     return (0);

}

 

int usart_putchar(char c,FILE * stream )

{

    if(c=='\n')

    {

        usart_putchar('\r',stream);

    }

    loop_until_bit_is_set(UCSRA,UDRE);

    UDR = c;

   

    return (0);

}

 

int usart_getchar(FILE * stream)

{

    loop_until_bit_is_set(UCSRA,RXC);

 

    return UDR;  

}

 

void USART_Init(void)

{

    // set baud address@hidden

    const unsigned int BAUD = 9600;

    const unsigned long XTAL = 8000000;

    UBRRH = (unsigned char) ((XTAL/16/BAUD - 1)<<8);     

    UBRRL = (unsigned char) (XTAL/16/BAUD - 1);

 

    // enable receive and transmit reg bit

    UCSRB = (1<<RXEN) | (1<<TXEN);

 

}

 

 


reply via email to

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