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 CCS Technical Support

Timed Out Get C

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



Joined: 02 Jun 2004
Posts: 16

View user's profile Send private message Send e-mail MSN Messenger

Timed Out Get C
PostPosted: Mon May 22, 2006 12:56 pm     Reply with quote

I am trying to use the EX_TGETC.C example in my code.
All is working well but i need some help with the timer.

The ccs help states

"Note that without a hardware UART the delay_us should be less than a tenth of a bit time (10 us at 9600 baud). With hardware you can make it up to 10 times the bit time. (1000 us at 9600 baud). Use two counters if you need a timeout value larger than 65535."

and the example code is

Code:

short timeout_error;
char timed_getc()
{
   long timeout;
   timeout_error=FALSE;
   timeout=0;
   while(!kbhit&&(++timeout<50000))   // 1/2 second
   delay_us(10);
   if(kbhit())
   return(getc());
   else
   {
      timeout_error=TRUE;
      return(0);
   }

}



I have two main problems
1) How is the 50000 derived to be 1/2 sec.
2) How do i use two counters to increase the timeout durration.

My header info is as follows:

Code:

#include <16F877.h>
#device ADC=10;
#use delay (CLOCK=4000000)
#fuses XT,NOWDT,NOPUT,NOLVP,BROWNOUT
#use rs232(baud=19200, parity=N, xmit=PIN_C6, rcv=PIN_C7)

#include <input.c>


any help or comments are appreciated as always

thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 22, 2006 3:30 pm     Reply with quote

You are using the hardware UART, so the 2nd sentence that begins with
"With hardware" applies to your program.

Your baud rate is 19200, so the time for one character is 10/19200,
which is approximately 521 us. (8 data bits, plus 1 start and 1 stop bit).
So theoretically, you could set the delay_us() value to 500, based on
the CCS recommendation. However, your oscillator speed is much
slower at only 4 MHz, compared to 20 MHz shown in the CCS example.
It takes at least 20 instruction cycles to execuite the while() loop.
That's 20 us, with a 4 MHz clock. So let's use a much safer value
for the delay_us(), such as 200 us. Then a timeout count of 50000
will give a 10 second timeout. (50000 x 200 = 10,000,000 usec =10 sec)
In fact, because the loop itself takes 20 us, the actual timeout is more
like 220 x 50,000 = 11 seconds. Example:
Code:
while(!kbhit&&(++timeout<50000))   // 10 second timeout (nominal)
   delay_us(200);
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