This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem with printf()

Hi,
when I use

printf("12345");
on the serial window #1 , I can only see
1234
so I wonder
(1)where is the character '5',
(2)why only "1234" is printed.
(3)Has '5' already been sent by serial port while not shown on the serial window #1
If I add '\n' to the pritf(), I mean to use
prinf("12345\n")
, then I could see the "12345" on the serial window #1 . Why?
(4)Does it also mean that the character '\n' is not printed!
(5)Does it mean that if we use printf() to print something, the last one character is always omitted?

Thanks for your help!

  • Have you tried this on a real target?

    I think this is to do with the way the simulator handles the serial window - the '\n' completes a line, and causes it to be displayed immediately; without the '\n', it has only a partial line - maybe it then waits til it has 4 characters to display?

  • The simulator actually simulates the real timing of the device.

    If you simulate the single instruction (SBUF = 'a') you won't see the letter 'a' appear in the serial window at the next instruction. The reason is that it takes 10 baud time for the letter 'a' to get 'shifted' out the serial port. So, the 'a' will not appear until many instructions later.

    Any time you invoke printf, immediately following the printf return you will see the output minus the last character. The reason for this is simple. If you look at the way the putchar function (which is how printf outputs characters) works, you'll find the following logic:

    while (!TI);
    TI=0;
    SBUF = char_to_send;
    

    If you set your baud rate sufficiently low enough, printf can print the last character (using putchar) and wrap up before that character is transmitted.

    If you don't want the simulator to accurate simulate serial port timing, you may set the STIME VTREG to a value of 0. See http://www.keil.com/support/docs/1943.htm for more information.

    Jon