How the printf() function works?
I have wrote a simple program to use the c51's function printf(). as belows:
void main (void) { //initial_UART: SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 0xFD; /* TH1: reload value for 9600 baud @ 11.0592MHz */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */ // TI is my question location!! // main while(1) { printf("1234\n"); } }
When i set the TI=0 in the part of "//initial_UART", the printf() function will always waiting for the the condition "TI=1", so ,it will not run out of the printf() function. this can be see in the assembly mode: C:0x1234 JNB(TI(0x98,1),C:0x1234).
But when i set the TI=1 in the part of "//initial_UART" , this program will run well and serialed out "1234" continually as i want.
My question is coming: when the TI is set to 1 first, after the printf() run first time ,when "4" was sent out of SBUT ,the TI bit will be CLEAR by the printf() herself (the assemble line is : CLR TI(0x98,1)),when printf() want to run the second time ,it find the TI is 0,so printf() will always waiting for TI to 1 and halted, NOTHING will be serialed out i think, but the fact is the printf() function was run sucessfully again and again.
What's the matter? there is someting set the TI to 1 after send out the last data from the SBUF? or the printf() have not RESET the TI to 0 when the last data send out ???But as we know, TI should be reset to 0 when the TI was became to 1. am I right?