View previous topic :: View next topic |
Author |
Message |
notbad
Joined: 10 Jan 2013 Posts: 68
|
Two questions about printf |
Posted: Fri Aug 16, 2013 7:20 pm |
|
|
1:
How long does it take after a printf call to send all the data in usart shift register?
I assumed that if the last byte is still being sent, it's going to take 10*bit time at worst case.
For baud=9600 I calculated 1041 us.
But in reality it takes about 2075 us. can anyone please explain this?
2:
which is correct? "\n\r" or "\r\n"? I searched CCS help and saw both kinds?!
Thanks in advance
v 4.130
PIC16f873 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 16, 2013 10:22 pm |
|
|
Quote: | How long does it take after a printf call to send all the data in usart shift register?
I assumed that if the last byte is still being sent, it's going to take 10*bit time at worst case.
For baud=9600 I calculated 1041 us.
But in reality it takes about 2075 us. can anyone please explain this? |
Read this section in the 16F873 data sheet:
Quote: | 10.2.1 USART ASYNCHRONOUS TRANSMITTER |
There is the TXREG register, followed by the TSR transmit shift register.
If you write two consecutive bytes with putc() or printf(), the first one
quickly goes to the TSR register, and the 2nd byte sits in the TXREG
waiting for the first one to shift out. It will take about 2.08 ms to shift out
both of these bytes.
The other question is really a Google question. There's tons of stuff on
it out there. Note especially the historical comments:
http://stackoverflow.com/questions/6539801/reminder-r-n-or-n-r |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19559
|
|
Posted: Sat Aug 17, 2013 1:31 am |
|
|
Just to go on a little further.
The _exact_ timings (if you ever need to get involved with these.....), depend a fraction on the ratio of your processor clock to the baud rate.
When you load 'TXREG', on the next processor instruction cycle (if the shift register is empty), the contents are transferred to the shift register. So there is always one instruction 'time' between the end of a byte, and the start of the next. Once the shift register is loaded, you can then load another byte to TXREG. So at this instant, you have one byte in TXREG, and one in the shift register, that has already started shifting, by one instruction time, plus the number of instructions needed to have tested the TXREG bit, and read a byte/load the TXREG. Typically about ten instructions. So if (for instance), you are running with a 4MHz clock (1uSec/instruction), you have (2083.3+1)-10uSec of data buffered = 2074.3uSec.
Agrees extremely closely with what you are seeing.
As PCM programmer says, the other question is a Google.
The real answer, depends what terminal emulation you are talking to. Some old terminals, require LF/CR, or they will give a double feed. Most don't care a jot....
Best Wishes |
|
|
notbad
Joined: 10 Jan 2013 Posts: 68
|
|
Posted: Sat Aug 17, 2013 5:26 am |
|
|
Thanks.
Very informative. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Aug 17, 2013 6:59 am |
|
|
my experience is that the receiver of your stream and
It's code, determine what terminating sequence is ideal.
for instance: in my practice ,for matlab and labview - a single \r is fine.
many non-PHYSICAL I/O devices simply swallow the \n
but if a DISPLAY device think what each character symbolizes and you will figure it out. ( Return cursor /New line )
but most of all, experiment will reveal all to you
i encourage THAT approach above all others. |
|
|
|