Pages: [1]   Go Down
Author Topic: Yet another printf() hack  (Read 873 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The avr-gcc stdio.h gives specific advice.
printf() needs a valid stdout descriptor,
which needs a generic  putc() function.
Code:
// ---- place globally,  above setup() ---

FILE serialStdout = {0} ;

static int uart_write(char c, FILE *stream)
{
   loop_until_bit_is_set(UCSR0A, UDRE0) ;
   UDR0 = c;
   return 0;
}//end of global stuff

// ---- place after Serial.begin() invocation  ----

   /* fill in the FILE structure  */
   fdev_setup_stream( &serialStdout, uart_write,
                            NULL,_FDEV_SETUP_WRITE);

   /* reassign stdout */
   stdout = &serialStdout ;

   /* ... and test the result */
   printf("stdout's address is %p\n", stdout) ;

bon appetit.


Logged

Pages: [1]   Go Up
Jump to: