CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Two questions about printf

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
notbad



Joined: 10 Jan 2013
Posts: 68

View user's profile Send private message

Two questions about printf
PostPosted: Fri Aug 16, 2013 7:20 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 16, 2013 10:22 pm     Reply with quote

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: 19469

View user's profile Send private message

PostPosted: Sat Aug 17, 2013 1:31 am     Reply with quote

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. Smile

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

View user's profile Send private message

PostPosted: Sat Aug 17, 2013 5:26 am     Reply with quote

Thanks.
Very informative.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sat Aug 17, 2013 6:59 am     Reply with quote

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 Very Happy Very Happy Very Happy Very Happy
i encourage THAT approach above all others.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group