avr-chat
[Top][All Lists]
Advanced

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

[avr-chat] Trouble with printf()


From: Rick Mann
Subject: [avr-chat] Trouble with printf()
Date: Sat, 27 Apr 2013 01:45:38 -0700

In my ATmega* projects, I often do something like this:

int
uart_putchar(char inC, FILE* inStream)
{
        while (!(UCSR1A & (1 << UDRE1)))
        {
        }
        
        UDR1 = inC;
        
        return 0;
}

int
main()
{
        static FILE     sStdOut;
        sStdOut.put = uart_putchar;
        sStdOut.get = __null;
        sStdOut.flags = 0x0002;
        sStdOut.udata = 0;
        
        stdout = &sStdOut;

}

So, in my XMEGA project, I did this:

inline
void
debugPutByte(uint8_t inByte)
{
        while ((USARTE0.STATUS & USART_DREIF_bm) == 0);
        
        USARTE0.DATA = inByte;
}

static
int
stdioPutChar(char inC, FILE* inStream)
{
        debugPutByte(inC);
        
        return 0;
}

int
main()
{
        static FILE     sStdOut;
        sStdOut.put = stdioPutChar;
//      sStdOut.get = __null;
        sStdOut.flags = 0x0002;
        sStdOut.udata = 0;
        
        stdout = &sStdOut;
        
        printf("Hello world");

}

A couple things went wrong. One minor one was the line assigning __null 
produces this error:

        main.c:132:16: error: expected expression before '__null'

But I figured it's nulled out as a static, anyway, so I comment it out.

The real problem is when I call printf, the output I get always mangles the 
last 3 characters in the string:

        > Hello woĆ¼

A "raw" print of a string using debugPutByte() above seems to work fine.

Any ideas?

Thanks,

-- 
Rick






reply via email to

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