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 fgets using Timer

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



Joined: 17 Dec 2005
Posts: 58

View user's profile Send private message

timed fgets using Timer
PostPosted: Wed Aug 30, 2006 10:01 am     Reply with quote

Hi,

Im using a pic18f1320 @ 4Mhz. Ive seen timed_getc functions but Id like to use a timer overflow to cause a timeout when using gets. Basically before calling gets, enable the timer, call gets, then when the timer overflows it can call a different part of the program.

How would I go about this, lets say for a 5 second timeout. Im not sure of the calculation.

thanks
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Wed Aug 30, 2006 1:03 pm     Reply with quote

Roughly it would be something like this:

Code:

timeoutflag = 0;
// configure and enable timer here
// includes setting up of timer values, turning on timer
// and enabling timer's ISR
while(!timeoutflag && !kbhit());
// consider adding restart_wdt()  to while loop
if(kbhit())
   returnval = getch();


Then, in the timer's ISR you set timeoutflag = 1

This version will run a little bit LONGER than the value you program into the timer since there is some delay getting into and out of the ISR and you have an extra call to kbhit() to make the decision as to why the code left the while loop.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Ttelmah
Guest







PostPosted: Wed Aug 30, 2006 2:47 pm     Reply with quote

First, I'd say 'abandon gets'. There is nothing 'special' about the internal functions (except they are there already), and gets, is a terrifyingly dangerous function on the PIC. Because it has no 'size' limit, and will carry on accepting characters until it sees the linefeed, if this one character gets omitted, or is corrupted, it can destroy all the RAM contents. There is a much 'better' I/O function, in the 'input.c' library, and a lot of the problems with some code, would disappear if this was substituted. However even this is not 'special', so the answer is to re-code this to add a time delay. If you don't use gets, then the code is not included in your program, so nothing is lost.
Something like:
Code:

int16 tick;
int1 exit;
#int_timer2
void tick_interrupt(void) {
   if (tick) --tick;
   else exit=TRUE;
}

void get_string(char* s, int max, int16 msec) {
   int len;
   char c;
   tick=msec/20;
   timeout=false;
   setup_timer_2(T2_DIV_BY_16,124,10);
   set_timer2(0);
   clear_interrupts(INT_TIMER2);
   enable_interrupts(INT_TIMER2;
   --max;
   len=0;
   do {
     if (kbhit) {
        c=getc();
        if(c==8) {  // Backspace
           if(len>0) {
              len--;
              putc(c);
              putc(' ');
              putc(c);
           }
        }
        else if ((c>=' ')&&(c<='~'))
           if(len<max) {
              s[len++]=c;
              putc(c);
           }
        if (c==13) exit=TRUE;
        //exit if string is complete
      }
   } while (!exit)
   disable_interrupts(INT_TIMER2);
   s[len]=0;
}

Then just call this with the address of the memory location to be used to hold the string, the maximum number of characters to wait for, and the the to wait (in mSec). The 'wait' has a 20mSec granularity.

Best Wishes
souravkumarkar



Joined: 31 Dec 2011
Posts: 5
Location: Baripada

View user's profile Send private message Visit poster's website

How to use this code
PostPosted: Thu Nov 01, 2012 10:11 am     Reply with quote

Sir how to use this code in simple.
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