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

Generating Time Delays Using Timer

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







Generating Time Delays Using Timer
PostPosted: Wed Jan 18, 2006 4:21 pm     Reply with quote

Hi, I'm only days into my first PIC project using C. I'm creating a system that basically runs through a task list and I need a simple way to make a task re-run itself after some period of time - from milliseconds to seconds.

The only ways I think of is to use a STATIC variable within the task, or a global variable, and somehow use a timer interrupt to decrement one of these.

There could be several of these 'timers' and I hate to tie up memory with static or global variables for each one.

Does anybody have any better ideas?

Thanks, Don
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 18, 2006 4:28 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=17189&highlight=multitasking
newguy



Joined: 24 Jun 2004
Posts: 1908

View user's profile Send private message

PostPosted: Wed Jan 18, 2006 4:34 pm     Reply with quote

Here's some information on using timers:
http://www.ccsinfo.com/forum/viewtopic.php?t=22467

If you have only one task that you want to repeat every x ms, then just outline the method in that post to create your x ms 'tick'. In the timer's interrupt service routine (ISR), set a flag to be true. For instance,

Code:
xxx_ms = TRUE;


In your main() function you'd have an infinite loop:

Code:
while (TRUE) {
// do stuff here
}


In that infinite loop, you'd have something like

Code:
if (xxx_ms) { // in other words, if xxx_ms is true (1)
   xxx_ms = FALSE; // always reset the flag first
   // now do whatever you want here
}


If you have several things that need to be repeated at different rates, either use different timers or pick the lowest common denominator (time interval) and set your timer to that interval. Then you'd have separate variables for each 'tick', and execute whatever needs to be done.

For instance, if you have something that needs to be done every 100 ms, and another thing that needs to be done every 350 ms, the lowest common denominator here is 50 ms. You'd set the timer to 'go off' or interrupt every 50 ms. Every time it does, increment two counters, count_100ms and count_350ms. When count_100ms is equal to 2, 100 ms has elapsed. Reset count_100ms to 0, and set a flag to let your main loop know that 100 ms has elapsed. Similarly, when count_350ms has reached 7, 350 ms has elapsed. Reset count_350ms to 0 and set a flag to let your main loop know that 350 ms has elapsed.

Hope this helps.
DonWare



Joined: 18 Jan 2006
Posts: 43

View user's profile Send private message

PostPosted: Wed Jan 18, 2006 4:43 pm     Reply with quote

Ok these are great. Very useful.

Thanks alot !

Don
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