Why is it that I have to set TI from software before I can get my printf() to execute ?
I seems printf() busy waits in TI ?
Why is this so ?
Is not TI supposed to be set by the hardware ?
"What I don't get is why I have to set it the first time to get my first printf() get out of busy wait."
To answer any such question about the operation of a Library function, you should consult the documentation for that function in the specific compiler Manual: http://www.keil.com/support/man/docs/c51/c51_printf.htm
It says, "This function is implementation-specific and is based on the operation of the _getkey and putchar functions. These functions, as provided in the standard library, read and write characters using the microcontroller's serial port. Custom functions may use other I/O devices."
So your next step would be to look up putchar in the Manual Unhelpfully, Keil don't provide a clickable link, but it's easy to find in the navigation pane to the left of the screen: http://www.keil.com/support/man/docs/c51/c51_putchar.htm
It says, "Source code is provide in the LIB folder."
So you can look in the LIB folder, and see what it does - which includes:
while (!TI); TI = 0; return (SBUF = c);
Now you can see why you need to "manually" set TI the very first time...
This is clearly illustrated in the "Hello, world" example program:
void main (void) { /*------------------------------------------------ Setup the serial port for 1200 baud at 16MHz. ------------------------------------------------*/ #ifndef MONITOR51 SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 221; /* TH1: reload value for 1200 baud @ 16MHz */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */ #endif /*------------------------------------------------ Note that an embedded program never exits (because there is no operating system to return to). It must loop and execute forever. ------------------------------------------------*/ while (1) { P1 ^= 0x01; /* Toggle P1.0 each time we print */ printf ("Hello World\n"); /* Print "Hello World" */ } }
For an example of customising serial IO (specifically, using interrupts), see: http://www.keil.com/support/docs/788.htm http://www.keil.com/download/docs/71.asp http://www.keil.com/download/docs/200.asp