|
|
View previous topic :: View next topic |
Author |
Message |
DonWare Guest
|
Generating Time Delays Using Timer |
Posted: Wed Jan 18, 2006 4:21 pm |
|
|
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
|
|
|
newguy
Joined: 24 Jun 2004 Posts: 1909
|
|
Posted: Wed Jan 18, 2006 4:34 pm |
|
|
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,
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
|
|
Posted: Wed Jan 18, 2006 4:43 pm |
|
|
Ok these are great. Very useful.
Thanks alot !
Don |
|
|
|
|
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
|